We’ve just uploaded mypy 0.620 to the Python Package Index (
PyPI). Mypy is an optional static type checker for Python. This release includes new features, bug fixes and library stub (typeshed) updates. You can install it as follows:
python3 -m pip install -U mypy
You can read the documentation for this release on
ReadTheDocs.
New Features Support for data classes in Python 3.7The recently released Python 3.7 added a new module
dataclassesthat allows writing simple boilerplate-free classes. Mypy now supports this new feature:
from dataclasses import dataclass from typing import List @dataclass class FitResult: optimum: List[float] chi: float method: str = "TRF" FitResult([0.1, 0.2], 1.2) # OK FitResult([0.1, 0.2], 1.2, "LM") # Also OK FitResult(1, 2) # Error!Note:
there are some limitations in supported features — see
the docs. (Contributed by Bogdan Popa in PR
5010.)
Overloads on generic types and other overload improvementsMypy previously rejected certain patterns involving overloaded functions, in particular defining overloads on generic types, and calling overloads on union and optional types. These (and a few others) are now supported. In addition, the error messages for overloads are now more detailed:
from typing import List, Union, overload @overload def summarize(data: List[int]) -> float: ... @overload def summarize(data: List[str]) -> str: ... def summarize(data): # Implementation goes here ... gen_data: Union[List[int], List[str]] res = summarize(gen_data) # OK, inferred type is Union[float, str] bad_data: int summarize(bad_data) # error: No overload variant of "summarize" matches argument type "int" # note: Possible overload variants: # note: def summarize(data: List[int]) -> float # note: def summarize(data: List[str]) -> str
See
the updated docsfor more details. (Contributed by Michael Lee.)
Incomplete and partial packagesWriting complete stubs for an existing large library may be hard and sometimes impractical. To allow gradual improvements in library stubs without generating spurious errors, two mechanisms are now supported. Adding a
__getattr__function to
__init__.pyiindicates that the corresponding package (or subpackage) is incomplete, thus silencing
Missing library stuberrors for this package:
# pack/__init__.pyi from typing import Any def __getattr__(arrr: str) -> Any: ... # pack/subpack/__init__.pyi # empty # pack/subpack/mod.pyi class Test: ... # main.py from pack import other # OK, pack is incomplete other.func(1, 2) # OK, all types in incomplete packages are Any from pack.subpack import mod # OK from pack.subpack import another # Error: missing library stub file
In addition, a PEP 561 stub package can declare itself as partial, allowing fallbacks to other sources of typing information such as inline annotations and typeshed stubs. See
PEP 561for the details. (Contributed by Ethan Smith.)
Other Improvements and Notable Bugs FixedFirst of all, we’d like to thank our employer, Dropbox, for funding the mypy core team. Thanks to all mypy contributors who contributed to this release:
Additional thanks to all contributors to
typeshed:
— Ivan Levkivskyi, on behalf of the mypy team
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4