We’ve just uploaded mypy 0.560 to the Python Package Index (PyPI). 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.
I'm also happy to welcome our newest mypy team member at Dropbox, Michael Sullivan.
Iterable and Friends Are Now Protocols
Various ABCs in typing such as Iterable, Iterator and Sized are now protocols instead of regular ABCs. This means that they support structural subtyping and you no longer need to explicitly subclass from typing.Iterable if you want your class to be treated as an Iterable, for example. You only need to define an __iter__ method:
from typing import Iterator class MyCollection: # Note: no base classes ... def __iter__(self) -> Iterator[str]: ... def dump(c: MyCollection) -> None: for item in sorted(c): # Okay, sorted() takes any Iterable print(item)
Previously you had to explicitly subclass MyCollection from Iterator[str] to make mypy accept it as an argument for sorted() (and in other contexts where an iterable object is expected), even though it’d work at runtime without subclassing:
from typing import Iterable, Iterator class MyCollection(Iterable[str]): # Base class is now optional ...
The new behavior matches how duck typing works in Python, and it also avoids potential metaclass conflicts that you may encounter if you inherit from one of the typing ABCs.
Read the documentation for more details, including a full list of protocols in typing. With this change, protocols are no longer considered an experimental feature.
This was contributed by Ivan Levkivskyi.
Other New Features
- Widen type of x to Any when isinstance(x, Any) is asserted (Dominik Miedziński, PR 3751)
- Add checks for overlapping tuple types in overloads (Elazar Gershuni, PR 4238)
Notable Bugs Fixed
- Fix spurious abstract attribute errors with Any base class (PR 4230)
- Fix crash caused by lambda that is deferred, i.e. it refers to a variable whose inferred type is not available yet for mypy (PR 4232)
- Check the right operand of is expressions (Ethan Smith, PR 4243)
- When disallow_untyped_defs is enabled, correctly warn about missing return types in async def (Jelle Zijlstra, PR 4218)
- Check signatures of reverse operator methods (Ethan Smith, PR 4249)
- Fix type inference problem with tuple types (Ivan Levkivskyi, PR 4087)
- Report additional errors in lambda bodies that were previously omitted (Ethan Smith, PR 4257)
- Fix crash related to relative imports (Ivan Levkivskyi, PR 4259)
- Fix spurious errors about returning Any when --warn-return-any is used (Elazar Gershuni, PR 4295)
- Issue a blocking error early if builtins can't be found, instead of crashing or generating confusing errors (PR 4327)
Improved Error Messages
- Fix missing line number in covariance error (PR 4275)
- Improve error message for an invalid callable type (Daniel Li, PR 4213)
- Produce better error on invalid types like List(int) (Michael J. Sullivan, PR 4323)
- Give better error message for omitted parentheses in function type comment (Michael J. Sullivan, PR 4329)
- Suggest spurious trailing commas as a cause for invalid tuple types (Michael J. Sullivan, PR 4336)
Other Changes
- Speed improvements in incremental mode (PR 4294, PR 4308, PR 4331)
- Document empty tuple syntax: Tuple[()] (Ethan Smith, PR 4313)
- Document how to silence invalid errors from linters (Ethan Smith, PR 4314)
- Document how to ignore imports in specific modules only (Ethan Smith, PR 4316)
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:
- Daniel Li
- Dominik Miedziński
- Elazar Gershuni
- Ethan Smith
- Ivan Levkivskyi
- Jelle Zijlstra
- Nehal J Wani
Additional thanks to all contributors to typeshed:
- Alex Willmer
- Ben Leslie
- Brandon W Maister
- Daniel Axtens
- Elazar Gershuni
- Ethan Smith
- FichteFoll
- gossrock
- Grzegorz Śliwiński
- Henri Bai
- Ivan Levkivskyi
- Jelle Zijlstra
- Josh Staiger
- Kenny Do
- Luka Sterbic
- Masayuki Yamamoto
- Nathan Henrie
- Neil Pilgrim
- Patrick Valsecchi
- rchen152
- Sebastian Rittau
- Sebastian Steenbuck
- Semyon Proshev
- Tim Abbott