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.7
The recently released Python 3.7 added a new module dataclasses that 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 improvements
Mypy 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 theupdateddocs for more details. (Contributed by Michael Lee.)
Incomplete and partial packages
Writing 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__.pyi indicates that the corresponding package (or subpackage) is incomplete, thus silencing Missing library stub errors 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 561 for the details.
(Contributed by Ethan Smith.)
Support for PyCharm test debugging, and add tox environment setup (Bernát Gábor, PR 5189)
Acknowledgments
First 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:
Alex Tereshenkov
Bogdan Popa
David Euresti
Elazar Gershuni
Emil Hessman
Ethan Smith
Gábor Bernát
Herst
Jelle Zijlstra
Julian Ospald
Michael Lee
Nate White
Sebastian Rittau
Additional thanks to all contributors to typeshed: