Wednesday 16 October 2019

Mypy 0.740 Released

Mypy 0.740 Released

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

Type Checking for str.format Calls

Calls to the format method on string literals are now type-checked based on the rules of the format string syntax and the format specification mini-language (PR 7418)

This also fixes some corner cases in % formatting and allows flagging bad interactions between bytes and strings.

Here are some errors mypy can now catch:

  • Likely accidental formatting of bytes ("{}".format(b'123') will produce "b'123'" on Python 3 which is likely unintended — the !r specifier should be used when it is intended)
  • Invalid format string syntax
  • Invalid conversion type or specifier
  • Incorrect number of arguments or field names (One thing this will complain about is too many arguments for a format string—this does not cause an exception at runtime but is still usually a bug.)
  • Type mismatches for numeric formatting
  • Type errors in index or attribute expressions

Improved check_untyped_defs

The self argument to methods without annotations is now correctly given the object’s type (instead of Any) when using check_untyped_defs (PR 7530).

This allows substantially better checking of untyped methods as well as enabling inference of attribute types in some situations where they would not have been determined in the past.

Additionally, we now suppress most “needs type annotation” errors caused by assigning empty collections (i.e, data = []), in untyped functions.

More Precise Error Locations (Breaking Change)

Reported line and column numbers are now more precise; in particular, incompatible assignment errors are now reported as being an error with the right hand side rather than the left hand side (PR 7578, PR 7569).

In some cases these changes can break # type: ignores, but from our experience they don’t do it too often.

Stricter Treatment of Context Manager Objects

Mypy 0.730 added support for using Literal types in the return type from __exit__ to determine whether it is possible for a with statement to swallow exceptions, which can affect whether mypy thinks code after a with statement is reachable.

To better pinpoint the source of issues caused by this, mypy now gives an error when __exit__ methods are declared to return bool but always return False. See the documentation for more information.

Documentation and Other Error Message Improvements

  • Add many cross-references to standard library to mypy docs (Oleg Höfling)
  • Update examples of mutable and non-mutable mapping in cheat sheet (Marco, Gorelli, PR 7620)
  • Fix syntax errors in code examples (Oleg Höfling, PR 7651)
  • Always report dmypy crashes to the client (ideally this fix will affect nobody, but…. 😉 , PR 7593)
  • Fix some notes not being ignored when using # type: ignore[code] (PR 7573)
  • Check syntax errors in # type: ignore[code] comments (PR 7460)
  • Fix incorrect error message when a positional argument is missing in a function with multiple default arguments (PattenR, PR 7547)
  • Fix message when second argument to isinstance is an alias to Any (Cohen Karnell, PR 7504)

Other Notable Improvements and Bug Fixes

  • Only return a declared type from __new__ if it is a subtype (PR 7372)
  • Give better types for (Michael Lee, PR 7643)
  • Fix a crash on missing self with --check-untyped-defs (PR 7669)
  • Treat __class_getitem__ as an implicit class method, similar to __init_subclass__ (ahmadF, PR 7647)
  • Allow TypedDict key with literal type during construction (PR 7645)
  • Support NamedTuple class syntax in all stubs (Sebastian Rittau, PR 7646)
  • Support eq and order arguments to attr.s (David Euresti, PR 7619)
  • Fix some false positives when checking raise statements (Patrick Williams, PR 7515)
  • Fix spurious errors with inherited dataclasses in incremental mode (PR 7596)
  • Fix a crash in mypy daemon (PR 7543)
  • Type-check class keyword in __init_subclass__ calls (g.denis, PR 7452)
  • Fix crash related to __getattr__ and __call__ (Ekin Dursun, PR 7446)
  • Fix incremental mode crashes (PR 7499)

Typeshed Updates

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


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:

  • ahmadF
  • Andrzej Górski
  • Brad
  • Cohen Karnell
  • David Euresti
  • Ekin Dursun
  • g.denis
  • hoefling
  • Jelle Zijlstra
  • John Reese
  • Marco Gorelli
  • Michael Lee
  • Michael R. Crusoe
  • Patrick Williams
  • PattenR
  • Sebastian Rittau

Additional thanks to all contributors to typeshed:

  • Alan Du
  • Alex Garbutt
  • Alex Jurkiewicz
  • Andrew Svetlov
  • Anthony Sottile
  • Árni Már Jónsson
  • Augusto Hack
  • Benjamin Peterson
  • Brad
  • brianjyoung
  • Carl Meyer
  • Colin Gilgenbach
  • Eric Traut
  • Glyph
  • herr kaste
  • hh-h
  • Jelle Zijlstra
  • Jeremy Lainé
  • jjmortensen
  • Lukáš Lalinský
  • Parth Sharma
  • Philippe F
  • plokmijnuhby
  • Rafi Blecher
  • Rebecca Chen
  • robertschweizer
  • Rune Tynan
  • Russ Allbery
  • Ryan Morshead
  • Sebastian Rittau
  • sinancepel
  • Stefan T
  • Utkarsh Gupta
  • Vasily Zakharov
  • wouter bolsterlee
  • 秋葉