Wednesday 3 April 2019

Mypy 0.700 Released: Up To 4x Faster

We’ve just uploaded mypy 0.700 to the Python Package Index (PyPI). Mypy is a static type checker for Python. This release switches to a compiled version of mypy, which is up to 4x faster than the previous release. It also 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 Read the Docs.

Much Improved performance

We are now shipping a mypy binary compiled with mypyc by default, which is up to 4x faster than the interpreted version shipped previously. The compiled version has been available as a separate PyPI distribution for a while now, and this is first release where it’s installed by default. Mypyc is a compiler that compiles type-annotated Python modules into efficient CPython C extension modules.

Note that the legacy, optional mypy_mypyc package will not be updated any more. If you used the mypy_mypyc package for an earlier mypy release, you should uninstall it first, before updating to the new package (you can safely ignore this if don’t know what this is):

    python3 -m pip uninstall mypy_mypyc

The compiled version is supported on 64-bit macOS, Windows and Linux platforms (Python 3.5 and later). On other platforms an interpreted version is used instead, which doesn’t provide any performance improvement. The compiled version is mostly compatible with the interpreted version, but some mypy plugins may be incompatible with it. You can still install the interpreted version of 0.700 like this:

    $ python3 -m pip install --no-binary mypy -U mypy

Stricter Equality Checks

If you run mypy with --strict-equality, mypy will detect equality and in checks that are likely to always result in True or False, since the operand types are not overlapping. For example, it will warn about comparing a str value to a bytes value, which can never be equal. See the documentation for more information.

Python 3.4 Unsupported for Running Mypy

You can no longer run mypy using Python 3.4, since Python 3.4 has reached its end of life. You can still type check code that targets Python 3.4, and we have no plans to drop support for this.

Plugin System Improvements

  • Add a get_additional_deps() plugin hook to support django-stubs (PR 6598)
  • Use the get_attribute_hook() plugin hook for dynamic classes (lincolnq, PR 6371)

Other Improvements

  • Allow # type: ignore comments after type comments (Ethan Smith, PR 6591)
  • Don't resolve callable NamedTuple fields to their return type (Danny Weinberg, PR 6576)
  • Support indexing unions containing tuples (PR 6475)
  • Fix member access on generic classes (PR 6418)
  • Fix interaction of isinstance() with Type[...] (PR 6419)
  • Fixes to tuple fallback types (PR 6442)
  • Fix missing built-in type aliases (PR 6441)
  • Make processing mypy cache faster (PR 6405)
  • Add an experimental mechanism for avoiding hashing sources on mypy daemon start (PR 6389)
  • Add flag --skip-cache-mtime-checks to skip mtime checks on cache files (PR 6391)
  • Give a better error message (and don't crash) if there is bad JSON in the cache (Ethan Smith, PR 6277)
  • Stubgen: improvements to stubs generated from docstrings (Wiktor Niesiobędzki, PR 6368)

Typeshed Updates (Highlights)

Here are selected typeshed updates (there are many more):

  • orjson: Add new stub file (ijl, PR 2747)
  • bleach: Add new stub file (Sebastian Rittau, PR 2709)
  • pycurl: Support Python 3 (JinyuanShanghai, PR 2867)
  • pydoc: Support Python 3 (Rebecca Chen, PR 2873)
  • multiprocessing: Add multiprocessing.spawn submodule and multiprocessing.pool stubs (Sean McLemon, PR 2823)
  • builtins: Add isascii methods to str, bytes, and bytearray for Python 3.7 (Brandt Bucher, PR 2834)
  • builtins: Add missing BaseException.__suppress_context__ attribute (wouter bolsterlee, PR 2876)
  • platform: Add types to some cross-platform functions (Paul Dagnelie, PR 2781)
  • codecs: Support the errors argument in factory functions (Aymeric Augustin, PR 2752)
  • charset: Improve the stubs under Python 3 (Josh Morton, PR 2768)
  • asyncio: Fix signature of asyncio.create_server (Aymeric Augustin, PR 2763)
  • asyncio: Update asyncio.streams stub (Gleb Chipiga, PR 2845)
  • werkzeug: Some small tweaks (lincolnq, PR 2784)
  • urllib[2]: Add stubs for HTTP Handler classes in Python 2 urllib2 and Python 3 urllib.request (Michael Brandt, PR 2710)
  • urllib2: Add some annotations (Philipp Hahn, PR 2688)
  • cyaml: Add some missing annotations (秋葉, PR 2769)
  • calendar: Add itermonthdays3 and itermonthdays4, fix incorrect cssclass_today (Sean McLemon, PR 2825)
  • enum: Add underscore names (秋葉, PR 2822)
  • logging: Changed parameter name of several methods from 'lvl' to 'level' to match the implementation (Eric Traut, PR 2840)
  • attr: Update attr stubs to 19.1.0 (Gleb Chipiga, PR 2846)
  • ast: Support new features and node types introduced in Python 3.8 (PR 2859)
  • flask: Fix several types (Gabriel Corona, PR 2858)
  • webbrowser: Update signature of webbrowser.register for Python 3.7 (Cary Yang, PR 2865)
  • pdb: Add missing class Pdb (Rebecca Chen, PR 2872)
  • yaml: add full_load and full_load_all stubs (Christopher Dignam, PR 2892)

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:

  • Anthony Sottile
  • Bernát Gábor
  • Danny Weinberg
  • Ethan Smith
  • Jelle Zijlstra
  • lincolnq
  • Mark Mendoza
  • Michael
  • Michael Lee
  • oremanj
  • Richard Eames
  • Sean McLemon
  • Sebastian Rittau
  • Wiktor Niesiobędzki

Additional thanks to all contributors to typeshed:

  • Alun Champion
  • Andrew Svetlov
  • Antoine Fourmy
  • Aymeric Augustin
  • Benjamin Peterson
  • Bertrand Bonnefoy-Claudet
  • Brandt Bucher
  • Cary Yang
  • Chen Li
  • Christopher Dignam
  • Eric Traut
  • Gabriel Corona
  • Gleb Chipiga
  • herr kaste
  • Hynek Schlawack
  • ijl
  • Ingmar Steen
  • Jeff
  • Jelle Zijlstra
  • JinyuanShanghai
  • Josh Morton
  • Joshua Oreman
  • Lawrence Chan
  • lincolnq
  • Luke Granger-Brown
  • Mark Mendoza
  • Matt Robinson
  • Maxim Kurnikov
  • Michael Brandt
  • Nipunn Koorapati
  • Pascal Corpet
  • Paul Dagnelie
  • Philipp Hahn
  • Rebecca Chen
  • Sean McLemon
  • Sebastian Rittau
  • Tom Briggs
  • Utkarsh Gupta
  • Vasily Zakharov
  • William Ayd
  • wouter bolsterlee
  • yoshiyuho
  • Łukasz Hanuszczak
  • 秋葉
— Jukka Lehtosalo, on behalf of the mypy team