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
the updated docs 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.)
Other Improvements and Notable Bugs Fixed
- Fix running mypy from editable install directory (Ethan Smith, PR 5381)
- Support egg/setuptools packages for PEP 561 searching (Ethan Smith, PR 5282)
- Refactor and reorder search path to make it compliant with PEP 561 (Ethan Smith, PR 5256)
- Silence errors in modules in site-packages and typeshed (Ethan Smith, PR 5303)
- Fix two option handling bugs in dmypy (PR 5172)
- Fix a daemon crash bug (PR 5285)
- Fix incorrect handling of attrs attributes with init=False and default (David Euresti, PR 5154)
- Various additional overload bug fixes (PR 5236, PR 5254, PR 5224, PR 5163, PR 5166) (Michael Lee)
- Several module __getattr__ fixes (PR 5332, PR 5306, PR 5295, PR 5292)
Internal Improvements
- Test suite cleanups (PR 5142, PR 5271) (Elazar Gershuni)
- Improve PEP 561 testing infrastructure (PR 5060, PR 5225, PR 5237) (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:
- Andrew Svetlov
- Anthony Sottile
- Bertrand Bonnefoy-Claudet
- Cyril Jouve
- Daniel Li
- Ethan Smith
- Froger David
- George King
- Hanaasagi
- Ilya Konstantinov
- Jason Fried
- Jelle Zijlstra
- John Reese
- Josh Holland
- Martin DeMello
- Mathieu Leduc-Hamel
- Max
- Max R
- Michael
- Michael Hirsch, Ph.D
- Michael Lee
- NAKAMURA Yoshitaka
- Nipunn Koorapati
- Philipp Hahn
- Rebecca Chen
- Robert Collins
- Roy Williams
- Scott Belden
- Sebastian Rittau
- Sekou Diao
- Stephen Thorne
- Steven Karas
- Sushain Cherivirala
- Yusuke Miyazaki
- mbarkhau
- potykion
- strager
— Ivan Levkivskyi, on behalf of the mypy team