Wednesday, 19 June 2019

Mypy 0.710 Released: New Semantic Analyzer

We’ve just uploaded mypy 0.710 to the Python Package Index (PyPI). Mypy is a static type checker for Python. This release features a new, experimental semantic analyzer and inline configuration options. It also includes many other 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 full documentation for this release on Read the Docs.

New Semantic Analyzer (Experimental)

We invite everyone with more than a passing interest in mypy to try out the new semantic analyzer. Please report any issues you might find, since we will likely make it the default in the next mypy release, and we will drop the old semantic analyzer soon after.
The semantic analyzer is a component of mypy that binds names to definitions. It also keeps track of what sort of thing each name defines, such as a function or a type alias. This release officially supports a revamped implementation of the semantic analyzer, dubbed the new semantic analyzer. In particular, it deals better with import cycles and forward references.
You can enable the new semantic analyzer by passing --new-semantic-analyzer on the command line or by setting new_semantic_analyzer = True in your mypy config file (typically mypy.ini). (docs)

Inline Configuration Options

You can now specify per-file configuration flags in # mypy: comments. For example, you can now put
    # mypy: no-strict-optional
at the top of a file to disable strict optional checking in that specific file. Previously you would have to specify this on the command line (e.g., mypy --strict-optional) or in the mypy config file (e.g., strict_optional = True in the [mypy] section or in a file-specific section). Multiple flags can be separated by commas or placed on separate lines.
    # mypy: no-warn-no-return
    # mypy: disallow-any-generics, always-false="FOO,BAR"
For more info, see the docs.

Type inference improvements and bug fixes

  • Improvements to the handling of Literal types. (Michael Lee; docs)
  • You can now ignore a file with a single # type: ignore comment at the top of the file. (Brandt Bucher, PR 6830)
  • Fix crashes with callable() and issubclass(). (Jelle Zijlstra, PR 6981)
  • Allow class-based TypedDicts in all Python versions. (PR 6971)
  • Fix yield from <tuple>. (Brandt Bucher, PR 6902)
  • Add more precise inference for enum attributes. (Michael Lee, PR 6867)
  • When strict optional is disabled, anything can be falsey; previously this caused some incorrect assumptions about code being unreachable. (PR 6876)
  • Document extended callable types as deprecated. (PR 6890)
  • Allow redefinition within with statements. This is strictly speaking unsafe, but allowing this is convenient in the vast majority of cases. (PR 6871)
  • Allow del with a list, e.g. del [x, y]. The semantics are the same as del x, y. (Brandt Bucher, PR 6858)
  • Don't crash when partial types are used in inherited attribute. (Ekin Dursun, PR 6766)
  • Allow ellipsis default arguments in "trivial" non-stub functions. (Michael Lee, PR 6665)
  • Don't treat the first argument of a function nested inside a method as self. (Ekin Dursun, PR 6769)
  • Emit an error message when a class member is redefined. (Ekin Dursun, PR 6686)
  • Don’t complain about overriding a private method name in a subclass. (Rafael Carício, PR 6790)
  • Refine the interaction between Literal and Final. (Michael Lee, PR 6763)
  • Fix the dataclass plugin’s interaction with member expressions. (PR 6739)
  • Support decorated constructors. (Previously, decorating __init__ would cause the class’s constructor to be unchecked!, PR 6645)
  • Fix multiple inheritance check for generic classes. (PR 6720)
  • Add basic support for enum literals. (Michael Lee, PR 6668)
  • Loosen base class attribute compatibility checks when attribute is redefined (Seth Yastrov, PR 6585)
  • Fix crash when a star expression is used in isinstance. (Ekin Dursun, PR 6659)
  • Fix crash when namedtuple, classmethod and generic types used. (Ekin Dursun, PR 6669)
  • Fix __getattr__ and operators related infinite recursion. (Ekin Dursun, PR 6655)
  • Don't type check a return statement containing a lambda . (This would treat everything after the lambda as unreachable., PR 6633)

Error message refinements

  • Messages produced by reveal_type() and reveal_locals() now use the note level rather than error. (Daniel Hahler, PR 6919)
  • Make the error for too few or too many arguments in type comments non-fatal. (PR 6813)
  • Improve the error message when a function is missing a return type. (Allison King, PR 6773)
  • Better error message for assignments and returns incompatible due to invariance. (Felipe de Morais, PR 6803)
  • Update the error message for incompatible arguments. (Matas Kairaitis, PR 6796)
  • Show the specific argument names that are missing in the error message for missing arguments. (Rafael Carício, PR 6795)
  • When a duplicate module is detected, suggest that a missing __init__.py file could be the cause. (Alexander Bliskovsky, PR 6785)
  • Extend the list of third-party packages with the top 200 packages from PyPI. (Matas Kairaitis, PR 6781)
  • Show closest candidates for misspellings in the case of from … import … . (marco-dd, PR 6778)
  • Improve the error message when an annotation is expected. (Rafael Carício, PR 6782)
  • When a type comment contains a syntax error, show the text of the bad comment in the error message. (kcaebe, PR 6779)
  • Include a note when user forgets to import something from the typing module. (Christopher Sabater Cordero, PR 6770)
  • Display both instances of duplicate modules in the error message. (Alexander Bliskovsky, PR 6772)
  • Turn all warnings and bare notes into errors. (PR 6650)

Command line/configuration related changes

This section also covers documentation, stubgen and plugins.
  • Update the typed_ast dependency to 1.4.0+. This allows installing mypy using Python 3.8. (PR 6937)
  • Add --no-implicit-reexport flag. (Jared Hance; docs)
  • Only honor --warn-unused-configs in non-incremental mode. In incremental mode it doesn't work right, and it isn't worth fixing. (PR 6829)
  • Fix sys.stdout overriding in mypy.api. (Amanda Walker, and El Khadiri Yassine, PR 6750)
  • Miscellaneous small improvements to stubgen.
  • Command-line syntax errors now consistently use exit code 2. (PR 6738)
  • Tweaks to --strict-equality based on user feedback. (PR 6674)
  • Allow specifying the list of files to check in the mypy config file. (Samuel Williams, docs)
  • Make directory checks case sensitive. (PR 6684)
  • Allow disabling the config file using --config-file="". (PR 6664)
  • Add some more plugin docs. (PR 6618)
  • Add a get_additional_deps hook for plugins, to support django-stubs. (PR 6598)

Python 3.8 related changes

  • Support / for positional-only arguments (PEP 570). (Brandt Bucher, PR 6900)
  • Support importing Protocol, TypedDict, Literal, Final and @final from typing (typing_extensions is still supported). Ironically, this is also supported Python 2.7. These were recently standardized by PEP 544 (Protocol and @runtime_checkable), PEP 586 (Literal), PEP 589 (TypedDict) and PEP 591 (Final/@final).
  • Fix line numbers assigned to class and def and ignoring for Python 3.8. (Brandt Bucher, PR 6753)
  • Expression-scoped ignores in Python 3.8. (Brandt Bucher, PR 6648)

Other Updates

You can browse the full commit log here.

Typeshed Updates

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

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:
  • Alexander Bliskovsky
  • Allison King
  • Amanda Walker
  • Anthony Sottile
  • Bernát Gábor
  • Brandt Bucher
  • Brooke
  • cclauss
  • Charles-Axel Dein
  • Christopher Sabater Cordero
  • crusaderky
  • Daniel Hahler
  • duke9509
  • El Khadiri Yassine
  • Ekin Dursun
  • Emil Goldsmith Olesen
  • Ethan Smith
  • Felipe de Morais
  • Ivar
  • Jan Szopinski
  • Jared Hance
  • Jason Michalski
  • Jelle Zijlstra
  • Joe Juzl
  • kcaebe
  • marco-dd
  • Matas Kairaitis
  • Max Mautner
  • Michael Lee
  • Nick Gaya
  • PattenR
  • Rafael Carício
  • Robin Chen
  • Rémi Lapeyre
  • Samuel Williams
  • Sanjit Kalapatapu
  • Seth Yastrov
  • viourr
Additional thanks to all contributors to typeshed:
  • Alex Chamberlain
  • Alexander Fasching
  • Anirudh Padmarao
  • Anthony Sottile
  • Benjamin Peterson
  • berdario
  • Brandt Bucher
  • Brendan Long
  • Brian Brunner
  • Callum Ryan
  • Carl Meyer
  • Chad Birch
  • Chad Dombrova
  • Chandan Singh
  • Connor Skees
  • Dan Crosta
  • Danny Weinberg
  • Dominic
  • Eric Arellano
  • Ethan Madden
  • Evan Moses
  • ijl
  • J Rob Gant
  • Jadiker
  • Jan Szopinski
  • Jean Hominal
  • Jelle Zijlstra
  • Jennifer Taylor
  • Jia Chen
  • Joe Juzl
  • John Freeman
  • Jon Dufresne
  • Jonathan Slenders
  • Josh Morton
  • Mark Mendoza
  • Mark Vismonte
  • markedwards
  • Masashi SHIBATA
  • Mathieu Bridon
  • Max Rydahl Andersen
  • Michael A. Smith
  • Michael Lee
  • nabnut
  • Paul Dagnelie
  • Philipp Hahn
  • Philipp Schrader
  • Radu Matei Lăcraru
  • Ran Benita
  • Rebecca Chen
  • Rune Tynan
  • Saul Shanabrook
  • Scott Belden
  • Sean Vig
  • Sebastian Rittau
  • Sergey Machulskis
  • Shahar Evron
  • Simon Kohlmeyer
  • Stephen Thorne
  • Sushain Cherivirala
  • The Fox in the Shell
  • Utkarsh Gupta
  • Viktor Roytman
  • Walter Scott Johnson
  • Yegor Roganov
  • zadamah
  • 秋葉
— Guido van Rossum, on behalf of the mypy team