We’ve just uploaded mypy 0.942 to the Python Package Index (PyPI). Mypy is a static type checker for Python. This release includes bug fixes only. You can install it as follows:
python3 -m pip install -U mypy
You can read the full documentation for this release on Read the Docs.
Fixes to Regressions
Let overload item have a more general return type than the implementation (Jukka Lehtosalo, PR 12435)
Fix inheritance false positives with dataclasses/attrs (Jukka Lehtosalo, PR 12411)
Support overriding dunder attributes in Enum subclass (Petter Friberg, PR 12138)
Fix small conditional overload regression (Marc Mueller, PR 12336)
Other Fixes
Fix issues related to the order of processing in the builtins import cycle (Jukka Lehtosalo, PR 12431)
Fix crash in match statement if class name is undefined (Jukka Lehtosalo, PR 12417)
Allow non-final __match_args__ and overriding (Jukka Lehtosalo, PR 12415)
I’d like to thank my employer, Dropbox, for funding the mypy core team.
We’ve just uploaded mypy 0.940 to the Python Package Index (PyPI). Mypy is a static type checker for Python. This release includes new features and bug fixes. You can install it as follows:
python3 -m pip install -U mypy
You can read the full documentation for this release on Read the Docs.
Match Statement
Mypy now has experimental support for type checking match statements introduced in Python 3.10.
Mypy can perform exhaustiveness checking to detect whether a match statement covers all possible values, if the target value has a union type or an enum type:
def empty_value(x: int | str) -> str:
match x:
case str():
# Type of "x" is "str" here
return '""'
case int():
# Type of "x" is "int" here
return "0"
# No error about missing return statement (unreachable)
Note that exhaustiveness checking is still not generally supported across class hierarchies, since mypy doesn’t support sealed classes with a fixed set of subclasses. You can use an union type or assert False as a workaround:
class Base(metaclass=ABCMeta): ...
class A(Base): ...
class B(Base): ...
def process(x: A | B) -> bool:
match x:
case A():
...
case B():
...
# Mypy knows that this is unrachable.
def process2(x: Base) -> bool:
match x:
case A():
...
case B():
...
# Use "assert False" to silence error about missing import,
# since mypy can't prove that code is unreachable.
assert False
You can also use an “assert never” function for more explicit exhaustiveness checks with match statements, as discussed in the documentation.
This feature was contributed by Adrian Freund (PR 12267, PR 10191), with additional updates by Jukka Lehtosalo.
Python 2 End-of-Life Schedule
After this release, Python 2 support is in feature freeze. Mypy won’t add new features or fixes specific to type checking Python 2 code, expect for fixes of significant regressions.
Mypy will drop Python 2 support in the second half of 2022.
Important note: Since typeshed is in the process of removing Python 2 specific stubs, you should pin all installed typeshed stub packages for third-party libraries to a version from Feb 2022 or earlier if you want to type check Python 2 code.
Miscellaneous New Features
Add support for conditionally defined overloads (Marc Mueller, PR 10712)
Give "as" variables in with statements separate scopes when it is safe to do so (Jukka Lehtosalo, PR 12254)
Add an optional error code ignore-without-code to require ignore comments to have error codes (Peter Law, PR 11633)
Add support for typing.Never and typing_extensions.Never as alternative spellings of NoReturn (Jelle Zijlstra, PR 12153)
Add support for typing.reveal_type (Jelle Zijlstra, PR 12117)
Support universal2 macOS wheels (97littleleaf11, PR 10651)
Add match_args support to attr.s() (Nikita Sobolev, PR 12111)
Enum Improvements
Check Enum definition for invalid base classes (Nikita Sobolev, PR 12026)
Understand the self-destructing nature of Enum._ignore_ (Kenny Stauffer, PR 12128)
Add StrEnum support for Python 3.11 (Nikita Sobolev, PR 12035)
Fix crash if "_" is in builtins (StefanM-TT, PR 11811)
Fixes crash on subclassing Annotated without args (Nikita Sobolev, PR 11814)
Typeshed Updates
Typeshed is now modular and distributed as separate PyPI packages for everything except the standard library stubs. Please see git log for full list of typeshed changes.
Acknowledgements
Thanks to all mypy contributors who contributed to this release:
97littleleaf11
Adrian Freund
Akuli
Alex Waygood
Alexandre Bouayad
Anthony Sottile
Bas van Beek
Brian Phillips
Chris Keefe
Dominic Davis-Foster
Gustav Gränsbo
James Braza
Jannic Warken
Jared Hance
Jelle Zijlstra
joey-laminar
Joshua Cannon
Kenny Stauffer
KotlinIsland
Marc Mueller
Mark Bell
Marti Raudsepp
Matt Bogosian
Max Rossmannek
Mehdi Drissi
Michael R. Crusoe
Nikita Sobolev
Oliver Newman
Peter Law
Purna Chandra Mansingh
Shantanu
Stanislav Levin
StefanM-TT
Tin Tvrtković
Tuomas Siipola
Vincent Perez
Štěpán Horáček
I’d also like to thank my employer, Dropbox, for funding the mypy core team.