Tuesday 17 December 2019

Mypy 0.760 Released

We’ve just uploaded mypy 0.760 to the Python Package Index (PyPI). Mypy is a static type checker for Python. 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 full documentation for this release on Read the Docs.

Type Signature Suggestions for Tools

Normally mypy only infers the types of variables, and you have to explicitly annotate functions. Mypy daemon can now generate a suggestion for a function or method signature using static analysis through dmypy suggest. This is a low-level feature that can be used by other tools and editor integrations to make it easier to annotate existing code. See the documentation for more information.

Type Inference Improvements

Mypy type inference got a few minor improvements. Mypy can now infer a variable with a collection type from two assignments:

    items = []  # No type annotation needed
    if check():
        items = [2, 3]

Previously mypy required a type annotation for items. Mypy can also more often infer an OrderedDict type:

    from collections import OrderedDict

    data = OrderedDict()  # No type annotation needed
    data['key'] = 4

Mypy can infer the type of an attribute in more cases when it’s first initialized through self.x:

    class Counts:
        def __init__(self, n: int) -> None:
            self.counts = {}  # Type inferred from initialization
            for i in range(n):
                self.counts[i] = 0

Fixes to Regressions

  • Fix regression introduced in 0.750: union (non-)simplification should not affect type inference (PR 8095)

Breaking Changes

  • Make the error code of a binary operation involving overload operator, for consistency (PR 8124)

Other Notable Improvements and Bug Fixes

  • Generate error when assigning an Enum or TypedDict type to an attribute (Xuanda Yang, PR 8107)
  • Fix relative path calculation if path is on a different drive on Windows (Netzeband, PR 8094)
  • Don’t infer an abstract type object type from concrete type object values (Xuanda Yang, PR 8096)
  • Fix an inconsistency with type checking lambda expressions (PR 8080)
  • Fix crash involving generic callable types (PR 8075)
  • Improve error message with long tuple initializer (Xuanda Yang, PR 7995)
  • Improve mypy daemon documentation (PR 8041)
  • Recommend typing_extensions instead of mypy_extensions for TypedDict (PR 8023)
  • Fix an inconsistency with self types (PR 8030, PR 8021)
  • Support type alias within Literal[...] (Xuanda Yang, PR 8014)
  • Fix --warn-return-any false positive when returning Any from a function declared to return object (Xuanda Yang, PR 8011)
  • Stubgen: Split @abstractproperty into @abstractmethod and @property (Xuanda Yang, PR 8066)
  • Stubgen: Give a better error message when using a .pyd file as target (Xuanda Yang, PR 8063)
  • Update stubgen documentation (PR 8031)

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:

  • jag426
  • Netzeband
  • Xuanda Yang

Additional thanks to all contributors to typeshed:

  • Alois Klink
  • Benjamin Peterson
  • cshesse
  • Denis Eliseev
  • Dylan Anthony
  • Eugene Ha
  • hauntsaninja
  • Jacob Ilias Komissar
  • Jason Fried
  • Jelle Zijlstra
  • Katelyn Gigante
  • Maksim Kurnikov
  • Michał Słapek
  • Mohammed El-Afifi
  • Ran Benita
  • Robert Schweizer
  • Rune Tynan
  • Ryan Hileman
  • Sebastian Rittau
  • Steven Kalt
  • toppk
  • Tuomas Suutari