Friday 13 April 2018

Mypy 0.590 Released

We’ve just uploaded mypy 0.590 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.

New Features

PEP 561

We are releasing experimental support for PEP 561, which allows packages installed through pip (or other packaging tools, as long as packages end up on sys.path, typically in site-packages) to indicate that they contain type annotations (either inline or in stubs), and also allows packages that just provide stubs, overriding typeshed. We’re grateful to PEP 561 author Ethan Smith for the design and implementation. (Note that normally mypy doesn’t process packages installed in site-packages or elsewhere on sys.path, because realistically most packages, whether third-party or in the standard library, cause a myriad of spurious type checking errors.)

A summary of the new functionality: 1. Packages can include a marker file named py.typed to indicate that they have inline annotations or stubs next to the sources. For packages marked this way, if foo.py and foo.pyi both exist, mypy will use the annotations from foo.pyi (ignoring foo.py completely), while if only bar.py exists, mypy will type-check and use the annotations in bar.py. 2. Instead of distributing annotations or stubs as part of a package, it’s also possible to have a separately installable package that just provides stubs; e.g. for package baz, if a package named baz-stubs is installed, mypy will use the annotations from that package.

For more details read the documentation. The full specification is in PEP 561. Note that the PEP hasn’t been accepted yet, hence the labeling of mypy’s support as experimental. (The mypy implementation is considered a reference implementation which will be used to gain experience to feed back into the PEP review process. We’re expecting minimal changes to the basics as described above though.)

Incremental mode is on by default

After over a year of “burn-in” we’re finally confident enough about incremental mode to make it the default. For larger codebases this can dramatically speed up mypy runs. If for some reason you want to turn it off, you can use --no-incremental.

Flags for always true/false

New command line flags --always-true NAME and --always-false NAME can be used to treat variables named NAME als compile-time constants. This allows you to define your own flags for things like platform checks, version checks, and other environmental checks. Example:

    ON_WINDOWS = (sys.platform == 'win32')
    def foo():
        if ON_WINDOWS:
            <do it the Windows way>
        else:
            <do it the UNIX way>

By default mypy will check both branches of the if statement. When this example is checked with --always-true ON_WINDOWS only the first branch is checked; with --always-false ON_WINDOWS only the second branch is checked. Note that in all cases mypy does not consider the initial value given to the variable. (This is by design, since mypy doesn’t assume that variables remain constant over the lifetime of the program.)

Note that mypy only checks the last component of the variable name — module and class attributes whose name matches one of these will also be affected. (This is a blessing and a curse, and a reminder to use this feature sparingly and only for the most obvious cases.)

These flags may be repeated on the command line, e.g. --always-true FOO --always-true BAR --always-true BAZ assumes FOO and BAR are always true and BAZ is always false.

These flags may also be given in the config file (e.g. mypy.ini). There the syntax is

    always_true = FOO, BAR
    always_false = BAZ

The may also occur in per-module sections, in order to limit the scope of their effect.

Add a per-module follow_imports_for_stubs options

Setting this flag to true causes follow_imports to affect stub files. Example:

    [mypy-foo]
    follow_imports = skip
    follow_imports_for_stubs = true
will force skipping the import of module foo even if a stub file (foo.pyi) is found. See the documentation for a full explanation.

Notable Bugs Fixed

  • Fix error message when assigning to read-only property (PR 4863 by Michael Lee, fixes issue 4756)
  • Respect --follow-imports=silent in --warn-unused-ignores (PR 4857, fixes issue 3949)
  • Don't produce spurious “unused type ignore” messages in incremental mode (PR 4841, fixes issue 2960)
  • Fix operator access on Type[...] (PR 4823, fixes issue 4789)
  • Remove incorrect override check on overloaded methods (PR 4810 by Michael Lee, fixes issue 4565)
  • Handle NotImplemented returned from __ne__ (PR 4770 by Peter Ludemann, fixes issue 4767)
  • Options in mypy.ini should be spelled with underscores, not hyphens (PR 4754 by Emil Hessman, fixes issue 4753)

Documentation Improvements

Stubgen Improvements

  • Emit Any type annotations for unannotated arguments (PR 4774 by Matt Gilson, fixes issue 4766)
  • Fix stubgen --recursive to only generate stubs for requested submodules. (PR 4845 by Matt Gilson, fixes issue 4844)
  • Properly format Callables (PR 4650 by Evan Hubinger, fixes issue 4640)

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:

  • Emil Hessman
  • Eric Wieser
  • Ethan Smith
  • Evan Hubinger
  • Evan Krall
  • Jelle Zijlstra
  • Matt Gilson
  • Michael Lee
  • Peter Ludemann
  • Rob Day
  • Viet Hung Nguyen

Additional thanks to all contributors to typeshed:

  • Alan Du
  • Ashwini Chaudhary
  • Bryce Guinta
  • Christopher Schramm
  • Danny Weinberg
  • David Euresti
  • dgelessus
  • Eric Wieser
  • Evan Krall
  • Jelle Zijlstra
  • Josh Holland
  • João Santos
  • Leonardo Fedalto
  • Luka Sterbic
  • Łukasz Langa
  • Matt Gilson
  • matthewfranglen
  • NODA, Kai
  • Nipunn Koorapati
  • Omar Sandoval
  • Patrick Meyer
  • Russell Cloran
  • Sebastian Rittau
  • Semyon Proshev
  • Tuomas Suutari
  • z33ky
— Guido van Rossum, on behalf of the mypy team