Friday 7 October 2016

Mypy 0.4.5 Released

We’ve just uploaded mypy 0.4.5 to PyPI. This release adds many new features, bug fixes and library stub updates. As always, you can install it as follows, being careful to install mypy-lang, not mypy:

    python3 -m pip install -U mypy-lang

Gitter Chat Room

We’re embarking on a customer support paradigm shift: we now have a chat room at gitter.com. Please join us for an interactive conversation. Of course, we also still encourage filing issues in our tracker!

Configuration Files

You can now put common mypy options in a configuration file, so you won’t have to remember to pass all those pesky flags each time you run mypy. Many options can also be specified per file using filename matching patterns. For example, you might put this in mypy.ini at the root of your repo:

    [mypy]
    python_version = 2.7
    fast_parser = True
    [mypy-foo/*]
    disallow_untyped_defs = True

This sets --python-version 2.7 (a.k.a. --py2 ) and --fast-parser for all mypy runs in this tree, and selectively turns on the --disallow-untyped-defs flag for all files under the foo/ subdirectory. This prints an error for function definitions without type annotations in that subdirectory only.

On a related topic, if you have a file containing a list of files and directories to check, you can now invoke mypy on that list using mypy @filenames — this reads a list of filenames from the file filenames .

PEP 526 Variable Annotations

If you like to live in the future, you may have noticed that Python 3.6b1 was recently released with syntactic support for variable annotations specified by PEP 526. But syntactic support alone wouldn’t be very useful, so we’ve got experimental type checking support for this feature in mypy! Thanks a bundle to Ivan Levkivskyi for the implementation. Example:

    def sum(a: List[float]) -> List[float]:
        total = 0.0
        cumulative: List[float] = []  # Check it out!
        for x in a:
            total += x
            cumulative.append(total)
        return cumulative

Note: you need --fast-parser to use this (it’s easy to enable in mypy.ini , see above) and you need to install typed-ast version 0.6.1 (python3 -m pip install -U typed-ast ).

New and Changed Flags

  • Renamed --suppress-error-context to --hide-error-context .
  • Fixed some nasty bugs in --incremental mode that could cause irreproducible errors.
  • Multiple -c arguments are now joined using newlines into a single multiline command.
  • New flag --show-column-numbers causes error messages to include column numbers in addition to line numbers. Note that column numbers are zero-based: the start of the line is column 0. (Ben Duffield)
  • New flag --disallow-subclassing-any reports errors when a base class has type Any .
  • New flag --strict-optional-whitelist controls strict none checking for individual files using filename matching patterns. (In mypy.ini, use show_none_errors in pattern sections, in combination with a global strict_optional flag.)
  • New flag --scripts-are-modules gives command line arguments that appear to be scripts (i.e. files whose name does not end in .py) a module name derived from the script name rather than the fixed name __main__. (This is more useful although less strictly correct.)

Deprecations and Removals

  • We no longer support using Python 3.2 to run mypy. (You can still type check Python 3.2 programs by specifying --python-version 3.2 .)
  • There used to be an undocumented feature named “weak mode”, triggered by comments of the form # mypy: weak . We now have better way to configure the strictness of checking per file, using pattern sections in mypy.ini (see above) so we have removed this feature to simplify mypy’s internals somewhat.
  • There was an undocumented package mypy.codec that was meant to support the Python 3 function annotation syntax in Python 2 using a codec hack (# coding: mypy ). We stopped using this a long time ago in favor of type comments, and we’ve finally removed the code.

Miscellaneous Other Changes in This Release

  • typing.TYPE_CHECKING : a constant that is false at runtime but true while type checking. This is the PEP 484 spelling. Previously you had to use if MYPY instead of if TYPE_CHECKING. Requires version 3.5.2.2 of the typing package.
  • Support for bytes formatting (e.g. b'foo%d' % 42) in Python 3.5 and up, per PEP 461. (Buck Baskin)
  • Several improvements to namedtuple and NamedTuple , e.g. _replace() and _fields are now supported. (Elazar Gershuni)
  • Mypy is now less aggressive about shortening types in error messages. E.g. previously a union with four or more values would often be replaced with union type (4 items), and similar for tuples. This now doesn’t happen unless the text would be over 400 characters.
  • Modest performance improvements.
  • Several miscellaneous bug fixes.
  • Lots of updates to the library stubs in typeshed.

Stub Generator

  • Improvements to the generated stubs. (Valérian Rousset)
  • Add --recursive and --ignore-errors flags. (Raphael Gaschignard)

Acknowledgements

We’d like to thank Michael Lee whose internship ended during this release cycle. He made --incremental worthy of trust and also contributed many other fixes (including documentation). Thanks Michael!

Thanks to all mypy contributors who contributed to this release:

  • Ben Duffield
  • Buck Baskin
  • David Foster
  • Elazar Gershuni
  • Ivan Levkivskyi
  • John Hagen
  • Raphael Gaschignard
  • Valérian Rousset

Additional thanks to these contributors to typeshed:

  • Alvaro Caceres
  • claws
  • Danny Weinberg
  • David Euresti
  • dlinnemeyer
  • Drew Haven
  • Elazar Gershuni
  • Evgeniy Vasilev
  • Gustavo J. A. M. Carneiro
  • Ivan Levkivskyi
  • John Hagen
  • Manuel Krebber
  • Matthias Kramm
  • Ollie Wild
  • rchen152
  • Rich Li
  • Robin Alazard
  • Roy Williams
  • Samuel Colvin
  • Sebastian Meßmer
  • Stephen Thorne
  • Tim Abbott
  • Valérian Rousset
  • Yasushi Saito

— Jukka (on behalf of the rest of the mypy team: Guido, David and Greg)