Wednesday, 25 May 2022

Mypy 0.960 Released

We’ve just uploaded mypy 0.960 to the Python Package Index (PyPI). Mypy is a static type checker for Python. This release includes new features, performance improvements 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.

Minimal Support for LiteralString

Recognise typing.LiteralString and typing_extensions.LiteralString as aliases to str.

This does not add proper support for LiteralString, but this allows it to be used in type annotations and stubs. LiteralString will be included in Python 3.11 (PEP 675), and it can be used to require that a value is an arbitrary literal string (once fully supported).

This feature was contributed by Shantanu (PR 12559).

Per-file Timing Stats

When mypy is run with the (currently hidden) --timing-stats FILE option, mypy will record detailed information about time spent type checking each file (microseconds). When profiling mypy runs over a large codebase, it can be useful to know which files are the slowest to type check. We are happy to receive bug reports about unexpectedly slow type checking.

This feature was contributed by Hugues (PR 12639).

Performance Improvements

  • Cache type of container literals when possible (Hugues, PR 12707)
  • Speed up type checking of nested if expressions (Hugues, PR 12700)
  • Speed up type checking of container literals with tuple entries (Hugues, PR 12706)
  • Speed up argument count check (Hugues, PR 12703)
  • Speed up processing large error counts and improve error filtering (Hugues, PR 12631)

Experimental Fast Module Lookup

Use the new --fast-module-lookup to switch to an alternative import resolution implementation that is faster when a large number of folders share a top-level namespace. We’d like to hear if you notice an improvement when using this flag, so that we can potentially prioritize work to enable this by default.

This feature was contributed by Hugues (PR 12616).

Documentation Updates

  • Add information about classes and types to “Getting started” (Michael Lee, PR 6557)
  • Mention no_type_check decorator in documentation (Shantanu, PR 12713)
  • Remove most mentions of type comments from docs (Shantanu, PR 12683)
  • Use PEP 585 syntax in "The type of class objects" (Alex Waygood, PR 12516)

Improvements to Plugin System

  • Add a class attribute hook to the plugin system (Danny Weinberg, PR 9881)
  • Add an alternative class decorator hook that is less error-prone than the old one (Jukka Lehtosalo, PR 12762)

ParamSpec Improvements

  • Friendlier errors for ParamSpec (PEP 612) (Shantanu, PR 12832)
  • Fix ParamSpec crash related to Any types (EXPLOSION, PR 12548)
  • Use tuple[object, ...] and dict[str, object] as upper bounds for ParamSpec.args and ParamSpec.kwargs (Alex Waygood, PR 12668)

Fixes to Crashes

  • Fix crash on invalid Python executable (Pranav Rajpal, PR 12812)
  • Fix crash on type alias definition inside dataclass declaration (Alex Waygood, PR 12792)
  • Fix crash related to namedtuple in unannotated function (Jukka Lehtosalo, PR 12804)
  • Fix nested namedtuple crash in incremental mode (Jukka Lehtosalo, PR 12803)
  • Fix forward references and generic inheritance in attrs classes (Jukka Lehtosalo, PR 12772)
  • mypyc: Fix TypeError in lambda argument to overloaded function (Jukka Lehtosalo, PR 12780)
  • Fix crashes related to functools.total_ordering and forward references (Jukka Lehtosalo, PR 12767)
  • Fix crashes related to dataclasses and forward references (Jukka Lehtosalo, PR 12762)
  • Fix crash on invalid TypedDict definition (Alex Waygood, PR 12741)
  • Fix crash when using decorator in class scope (dzcode, PR 12724)

Other Notable Fixes and Improvements

  • Fix type annotation support of dunder methods in stubgen (Fabian Keller, PR 12828)
  • Fix some value patterns in match statements to be non-exhaustive (Štěpán Horáček, PR 12751)
  • Generate error when using both abstractmethod and final (Tomoki Nakagawa, PR 12743)
  • Add more precise error message for Callable annotation (frerikandriessen, PR 12518)
  • Fix generic inference in functions with TypeGuard (Nikita Sobolev, PR 11797)
  • Add check if python_version was parsed as float in pyproject.toml (Marcel Otoboni, PR 12558)
  • Fix nested overload merging (Marc Mueller, PR 12607)
  • stubtest: Generate error if type alias doesn't exist at runtime (Alex Waygood, PR 12608)
  • Support typing_extensions.overload (Jelle Zijlstra, PR 12602)

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
  • Alex Waygood
  • Danny Weinberg
  • EXPLOSION
  • Ethan Smith
  • Fabian Keller
  • Hugues
  • Ikko Ashimine
  • Jelle Zijlstra
  • Marc Mueller
  • Marcel Otoboni
  • Max Jones
  • Michael Lee
  • Nikita Sobolev
  • Shantanu
  • Tomoki Nakagawa
  • Vincent Vanlaer
  • dzcode
  • eggplants
  • frerikandriessen
  • Jared Hance
  • Pranav Rajpal
  • Štěpán Horáček

I’d also like to thank my employer, Dropbox, for funding the mypy core team.