Thursday 26 September 2019

Mypy 0.730 Released

We’ve just uploaded mypy 0.730 to the Python Package Index (PyPI). Mypy is a static type checker for Python. This release includes many features, bug fixes and library stub (typeshed) updates. 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.

More Precise Error Locations (Breaking Change)

If you call a function with an incompatible argument type, mypy now points the error message to the argument with the incompatible type. Previously mypy pointed to the function being called, which could be confusing in multi-line calls.

Note: You may have to move some # type: ignore comments as a result of this change, since their behavior depends on the line number of reported errors.

Error Codes

Mypy can now optionally display error codes. They are shown within square brackets after each error message: error: "str" has no attribute "trim"  [attr-defined]

Enable error codes using --show-error-codes (or show_error_codes = True in a configuration file). See the documentation for more information (documentation on individual codes here and here).

Ignoring Specific Error Codes

You can ignore only errors with specific error codes on a particular line by using a # type: ignore[code, ...] comment. This reduces the risk of ignoring unexpected, serious errors when using # type: ignore comments that ignore (almost) all possible errors on a line.

See the documentation for more details.

Colors in Output

Mypy now uses colored, more user-friendly output by default. Example:

  $ mypy mypy
  mypy/ error: Incompatible return value type (got 
    "List[int]", expected "List[Optional[int]]")
  mypy/ note: "List" is invariant -- see
  mypy/ note: Consider using "Sequence" instead, 
    which is covariant
  mypy/ error: Argument 1 to 
    "checkers_for_type" has incompatible type "str"; expected "bytes"
  Found 2 errors in 2 files (checked 154 source files)
  $ mypy mypy
  Success: no issues found in 154 source files

You can use --no-color to disable colored output. You can use --no-error-summary to hide the summary line with the number of errors.

Windows support was contributed by Ethan Smith (requires Windows 10).

Pretty Output Mode

You can use --pretty to display each line which had errors and a caret that points to the location of the error on each line. Example of pretty output:

  $ mypy --pretty mypy
  mypy/ error: Argument 1 to "checkers_for_type" 
  has incompatible type "str"; expected "bytes"
                  c = self.checkers_for_type(specifier.type, ...
  Found 1 error in 1 file (checked 154 source files)

This mode also wraps long error messages at word boundaries to improve readability.

Old Semantic Analyzer Removed

This release no longer includes the old semantic analyzer. See mypy 0.720 release notes for information on the new semantic analyzer.

Reachability and Context Managers

The --warn-unreachable option now behaves more correctly with “exception-swallowing” context managers. If a context manager is currently declared to return bool but it never swallows exceptions, you should annotate the return of __exit__ as Literal[False] instead of bool, or otherwise mypy may complain about missing return statements. Example:

    from typing import Optional, Type
    from types import TracebackType
    from typing_extensions import Literal

    class Resource:
        def __exit__(self,
                     exc_type: Optional[Type[BaseException]],
                     exc_val: Optional[BaseException],
                     exc_tb: Optional[TracebackType]) -> Literal[False]:
            return False

This was contributed by Michael Lee (PR 7317).

Miscellaneous New Features

  • Support PEP 572: Assignment expressions (a.k.a. the “walrus operator”) (Jelle Zijlstra, PR 6899)
  • Basic support for the PEP 593 draft: typing_extensions.Annotated (Brandt Bucher, PR 7292)
  • Don't re-export star-imported names when implicit re-exports are disabled (Ran Benita, PR 7361)
  • Honor the return type of __new__ (PR 7188)
  • Support recursive traversal into PEP 420 namespace packages (Barry Warsaw, PR 7219)
  • Support expansion of home directories (~) and environment variables in all path configuration options (hetmankp, PR 7022, PR 7273)
  • Cache directory may be set via environment (hetmankp, PR 7016, PR 7443)
  • Make type formatting use fully qualified names whenever there is ambiguity (PR 7226)
  • Infer better types when concatenating fixed-length tuples (Ekin Dursun, PR 7409)

Improvements to Strict Equality Checking

  • Apply --strict-equality special-casing for bytes also to bytearray (PR 7473)
  • Make --strict-equality stricter with literals (PR 7310)
  • Fix special case of --strict-equality and literal types (PR 7358)
  • Whitelist some equality checks under --strict-equality (PR 7302)
  • Disable --strict-equality inside functions that have type variables with value restrictions (PR 7304)
  • Fix overly restrictive --strict-equality with NewType and literals (PR 7351)

Changes Affecting Plugins

  • Add error code arguments to the plugin API (PR 7323)
  • Move some (undocumented) API functions as part of a refactoring, including make_simplified_union (PR 7397)
  • Plugins don’t need to support the old semantic analyzer, which was removed

Other Notable Improvements and Bug Fixes

  • Report undefined module attributes with --ignore-missing-imports (PR 7372)
  • Fix issue with list comprehension shadowing variables (PR 7476)
  • Fix false positive when overriding a class method that uses self type (PR 7474)
  • Fix combination of --follow-imports-for-stubs and --follow-imports=skip (g.denis, PR 7459)
  • Fix crash in multiple assignment when __iter__ returns Any (PR 7462)
  • Be more aggressive about site-packages in MYPYPATH (PR 7458)
  • Recognize a class as protocol if at least one of the bases is Protocol (Tom Lee, PR 7455)
  • Handle class with tuple base as TypeVar bound (Andriy Teraz, PR 7444)
  • Test Python 3.8 beta in Continuous Integration (PR 7421)
  • Fix crash in incremental mode related to redefined classes (PR 7413)
  • Assume list can be empty if passed as a *args argument (Ekin Dursun, PR 7392)
  • Fix crash when a dataclass with a no-init InitVar is inherited (Ekin Dursun, PR 7390)
  • Fix dataclass __init__ signature calculation issue with forward references (PR 7382)
  • Second expression in assert statement should be checked as conditional (g.denis, PR 7318)
  • Fix TypedDict with unicode name (PR 7369)
  • Improve overload target selection when using TypedDict (PR 7365)
  • Treat __init_subclass__ as an implicit class method (Tavian Barnes, PR 7355)
  • Be less aggressive when inferring literal types (PR 7354)
  • Analyze and check default arguments to lambdas (PR 7306)
  • Fix stubgen placement of decorators (g.denis, PR 7269)
  • Fix crash when interpolating bytes with a dictionary (PR 7297)
  • Reduce memory use by freeing state and ASTs when they are no longer needed (PR 7280)
  • Analyze super() expressions more precisely (Tavian Barnes, PR 7232)
  • Make per-line type checking precision statistics more precise (PR 7254, PR 7261)
  • Log whether a compiled mypy is being used (PR 7257)
  • Fix crash related to overriding an attribute of a generic class (PR 7235)
  • Fix spurious error about redefining final variable (PR 7230)
  • Fix crash when a broken overload appears in stub file (PR 7223)
  • Add Any type to imports when encountered by stubgen (Oleg Höfling, PR 7090, PR 7208)
  • Reprocess a module if a previously missing import was added (PR 7199)
  • Fix crash related to TypedDict (PR 7195)

Typeshed Updates

Many small improvements were made to typeshed — too many to list. Browse the typeshed commit log here.

The library stubs for subprocess were improved significantly. This may require fixes to existing annotations that weren’t checked by mypy previously.


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:

  • Adam
  • Adam Smith
  • Amit jha
  • Andriy Teraz
  • Barry Warsaw
  • Brandt Bucher
  • Daniël van Eeden
  • ed1d1a8d
  • Ekin Dursun
  • Ethan Smith
  • g.denis
  • hetmankp
  • hoefling
  • Hugh Han
  • Jakub Molinski
  • Jared Hance
  • Jason Veatch
  • Jelle Zijlstra
  • Juan Luis Cano Rodríguez
  • kxing
  • Matthew Wright
  • Max Murin
  • Michael Lee
  • Michael Sullivan
  • nu_no
  • Paul Sokolovsky
  • Ran Benita
  • Sanjit Kalapatapu
  • Shuyao Bi
  • Sinan Cepel
  • Tapasweni Pathak
  • Tavian Barnes
  • Tom Lee
  • Tomer Chachamu
  • Tushar Sadhwani
  • yuxiang-he

Additional thanks to all contributors to typeshed:

  • Alan Du
  • Allison Kaptur
  • Andriy Teraz
  • Anthony Sottile
  • Benjamin Peterson
  • Bouteillebleu
  • Brad
  • Brandt Bucher
  • Cole Maclean
  • Colin Gilgenbach
  • Connor Brinton
  • Daniel Hahler
  • Daniel Holth
  • Evgeny Dedov
  • g.denis
  • Ilya Konstantinov
  • Jaromir Latal
  • Jelle Zijlstra
  • Jon Dufresne
  • Kamil Bar
  • Maarten ter Huurne
  • Michael Lee
  • Michael Nix
  • Michael R. Shannon
  • Naman
  • Nicklas Lindgren
  • Niels Buwen
  • Nipunn Koorapati
  • Pascal Corpet
  • Patrick Valsecchi
  • Phil Jones
  • plokmijnuhby
  • Ran Benita
  • Rebecca Chen
  • Rune Tynan
  • Samuel Freilich
  • Scott Belden
  • Sebastian Rittau
  • Shannon Zhu
  • Vasily Zakharov
  • Ville Skyttä
  • William Ayd
  • Yannack
  • Zsolt Dollenstein
  • 秋葉