Wednesday, 3 June 2020

Mypy 0.780 Released

Mypy 0.780 Released

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

Following Imports in Mypy Daemon

You can now leave out the --follow-imports option when running dmypy, and mypy daemon will follow imports, similar to a non-daemon mypy run:

    dmypy run -- pkg/*.py

Previously mypy daemon did not support this, and it was necessary to use --follow-imports and explicitly mention all files to check on the command line. (Following imports in mypy daemon is an experimental feature.)

See the docs about following import in mypy.

Miscellaneous New Features

  • Support environment variable interpolation for junit_xml configuration key (Mattwmaster58, PR 8479)
  • Add no_site_packages config file setting (davidzwa, PR 8524)
  • Allow .mypy.ini (with a dot prefix) as an alternative name to mypy.ini (dosisod, PR 8515)
  • Disallow unpacking of strings in multiple assignment, as this is usually a bug (Abtin, PR 8589)
  • Allow type aliases in a runtime (non-type) context (PR 8779)
  • Add narrowing unions with bool literals via identity check (Steve Dignam, PR 8821)
  • Always allow to cast to Any without warnings (oda, PR 8544)
  • Suggest solutions for a typo in a key of a TypedDict (Felicián Németh, PR 8483)
  • Provide more context about why incompatible with supertype is an error (Chetan Khanna, PR 8866)

Other Notable Improvements and Bug Fixes

  • Fix handling dependencies to __call__ in mypy daemon (PR 8494)
  • Various improvements to stubtest (PR 8502, PR 8886) (Shantanu)
  • Fix invalid type causing named tuple errors reported in wrong files (PR 8549)
  • Clarify documentation of Liskov substitution principle (PR 8563)
  • Fix type inference with lambda that returns None (PR 8562)
  • Fix incremental crash bug caused by NewType in functions (PR 8607)
  • Fix indexed assignment check when TypedDict is used as upper bound (Xuanda Yang, PR 8621)
  • Improve error message for bad indexed assignment (pando, PR 8641)
  • Fix crash when the same file is processed under multiple names (PR 8644)
  • Fix parser when using Python 3.9 (PR 8716)
  • Accept dict() as an empty dictionary in a TypedDict context (PR 8749)
  • Fix incorrect coroutine return type for nested function (Jens Widell, PR 8741)
  • Reject bytes literal as a TypedDict key (PR 8782)
  • Allow attrs keyword-only arguments at any position (Markus Schmaus, PR 8803)
  • Detect duplicate keys in call-based TypedDict definitions (PR 8849)
  • Look at arguments when generating constraints for an overload (PR 8845)
  • Fix potential wrong-file error message for unsupported types (PR 8854)

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:

  • Abtin
  • Brad Solomon
  • Chetan Khanna
  • Claudio Jolowicz
  • davidzwa
  • Diego Elio Pettenò
  • dosisod
  • Ethan Smith
  • Felicián Németh
  • Jelle Zijlstra
  • Jens Widell
  • Joy Bhalla
  • LiuYuhui
  • Markus Schmaus
  • Marten Kenbeek
  • Mattwmaster58
  • Max R
  • oda
  • pando
  • PattenR
  • prostomarkeloff
  • Shantanu
  • Steve Dignam
  • Timofey Kukushkin
  • Ville Skyttä
  • Xuanda Yang

Additional thanks to all contributors to typeshed:

  • Alex McGrath Kraak
  • Alexandre Yang
  • Andrew
  • Andrew Svetlov
  • Anis
  • Anthony Sottile
  • Ben Leslie
  • Ben Motz
  • Benjamin Poirier
  • Brian Turek
  • Bruce Merry
  • Christopher Whelan
  • coiax
  • David Euresti
  • David T.H. Kao
  • Debjyoti Biswas
  • Denis Laxalde
  • Diego Elio Pettenò
  • dosisod
  • Ethan Smith
  • Florian Ludwig
  • Florimond Manca
  • Gal Ben David
  • Graham Bleaney
  • Jakub Stasiak
  • Jan Verbeek
  • Jaromir Latal
  • Javier Honduvilla Coto
  • Jeff Hunter
  • Jelle Zijlstra
  • Jia Chen
  • Jocelyn Boullier
  • Joost Cassee
  • Julian Andres Klode
  • Julin S
  • karl ding
  • Katelyn Gigante
  • Kazushi Kitaya
  • Kjell Braden
  • Lars
  • Lawrence
  • Luciano Ramalho
  • Mark Mendoza
  • Markus Wamser
  • Max R
  • Mickaël Schoentgen
  • Nguyễn Gia Phong
  • Nipunn Koorapati
  • Oleg Höfling
  • Paul Ganssle
  • Peter Pentchev
  • petsuter
  • PGijsbers
  • Philipp Hahn
  • Rahix
  • Ran Benita
  • Razzi Abuissa
  • Rebecca Chen
  • Rodrigo Castro
  • Rune Tynan
  • Sahith Nallapareddy
  • Sam Bull
  • Sebastian Rittau
  • Selim Belhaouane
  • Serhiy Storchaka
  • Shantanu
  • Steve Dignam
  • Tim Hatch
  • Vishal Kuo
  • Zhiming Wang

Tuesday, 10 March 2020

Mypy 0.770 Released

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

Tagged Unions

Previously, the only way to distinguish between two types in a union was to use an isinstance check. Now, mypy supports distinguishing between two or more types based on the value of some common shared "tag" field.

This feature is most useful when you want to narrow down a union of TypedDicts: since TypedDicts are really just regular dicts at runtime, using isinstance checks won't work. Instead, you can now label or tag each TypedDict with a distinct Literal type and discriminate by checking that tag:

    from typing import Literal, TypedDict, Union
    class NewJob(TypedDict):
        event_type: Literal["new-job"]
        job_description: str
    class CancelJob(TypedDict):
        event_type: Literal["cancel-job"]
        job_id: int
    request: Union[NewJob, CancelJob]
    # We know request["event_type"] must be of type
    # Union[Literal["new-job"], Literal["cancel-job"]]
    if request["event_type"] == "new-job":
        # So if we narrow request["event_type"] down to type
        # Literal["new-job"], mypy will conclude 'request' must
        # therefore be of type 'NewJob' in this branch...
        print("Starting new job", request.job_description)
        # ...and inferred to be type 'CancelJob' in this one
        print("Cancelling job with id", request.job_id)

Note that you can import Literal from typing_extensions on Python 3.7 and earlier.

You can also use this technique to discriminate between unions of objects, tuples, or named tuples, so long as: 1. Every item in your union is tagged with a unique value 2. Mypy understands how to narrow the union of the tag values.

For more details and examples, see the documentation on tagged unions.

This feature was contributed by Michael Lee.

Type Inference Improvements

Type inference is now more powerful. Mypy can infer types in some cases where you previously needed an explicit type annotation.

Mypy can infer a built-in collection type from augmented assignment statements:

    x = []
    x += [1, 2]  # Infer type List[int] for x

Some dictionary construction idioms are better supported:

    d = {}  # No type annotation needed
    for k in things():
        if k not in d:
            d[k] = 0
            d[k] += 1

Mypy is better at inferring defaultdict types:

    from collections import defaultdict
    d = defaultdict(list)
    d['foo'].append(1)  # Infer type DefaultDict[str, List[int]] for d

Multiple Inheritance and isinstance() Checks

Previously, mypy assumed a variable cannot have two unrelated types when analyzing isinstance checks. For example, in the following example B is not a subclass of A, so mypy would incorrectly conclude the isinstance check is always false, and that the if-branch is unreachable — even though var could actually be a subtype of both types.

This meant that mypy skipped type checking anything inside the if statement, since it (by design) skips analyzing unreachable branches.

    class A: pass
    class B: pass
    class Ok(A, B): pass
    x: A = Ok()
    if isinstance(x, B):

Now, mypy will instead decide that the narrowed type of x is test.<subclass of "A" and "B">, instead of marking the branch as unreachable.

If it’s impossible for two types to be a subtype of one another, mypy will continue to not attempt to infer this "ad-hoc intersection":

    # Fails with a "TypeError: multiple bases have instance lay-out confict" error at runtime
    class Impossible(int, str): pass
    bad: int = Impossible()
    if instance(bad, str):
        # This branch is unreachable and so is not type checked.
        # If you want to be warned about unreachable branches, use
        # the --warn-unreachable flag.

This feature was contributed by Michael Lee.

Other Notable Improvements and Bug Fixes

  • Fix some crash bugs involving import * and import cycles (PR 8450)
  • Fix interaction of descriptor methods with plugins (Jan Verbeek, PR 8365)
  • Allow strict in config file (Ville Skyttä, PR 8192)
  • Don’t crash when a module shadows builtin libraries (such as typing or types, PR 8405)
  • Fix type join between subclasses of unicode and str (PR 8402)
  • Fix type join of fixed-length tuples with mismatching lengths (Marti Raudsepp, PR 8333)
  • Fix type join of Sequence (e.g. variadic tuple) and fixed-length tuple (Marti Raudsepp, PR 8335)
  • Make mypy.api.run_dmypy actually capture the output (PR 8375)
  • Support determining whether a literal is truthy (Jan Verbeek, PR 8368)
  • Fix covariant overriding of decorated methods (Xuanda Yang, PR 8350)
  • Support typing.Annotated in addition to typing_extensions.Annotated (Jakub Stasiak, PR 8371)
  • Add add_method_to_class function to plugins.common (useful when writing mypy plugins) (Maksim Kurnikov, PR 8245)
  • Fix module alias as instance attribute (Uwe L. Korn, PR 8259)
  • Automatically write a file .gitignore file to cache directory, ignoring everything (Ville Skyttä , PR 8193)
  • Don't make dunder attributes enum members (Xuanda Yang, PR 8302)
  • Allow redefining TypedDict keys (while still generating an error) (Cohen Karnell, PR 8109)
  • Fix some crashes in dataclasses (PR 8271)
  • Use TypedDict union as type context when unambiguous (PattenR, PR 8212)
  • Fix false positive for subclasses of bytes overriding __str__ (Shantanu, PR 8222)
  • Fix deeply nested InitVar definitions in dataclasses with init=False (Jacob Beck, PR 8208)
  • Narrow types for walrus assignment in if statements in some cases (Shantanu, PR 8258)
  • Narrow types for walrus assignment in if statements in most of the rest of cases (PR 8458)
  • Fix incorrect error code indexing (Xuanda Yang, PR 8248)
  • Fix regression in container check logic (Michael Lee, PR 8232)
  • Make reachability code understand chained comparisons (Michael Lee, PR 8148)
  • Fix incorrect name lookup for decorated methods (Xuanda Yang, PR 8175)
  • Fix simplifying unions with type aliases (Xuanda Yang, PR 8146)
  • Fix crash when overriding __init__ in a dataclass subclass (Jacob Beck, PR 8159)
  • Fix some daemon crashes involving classes becoming generic (PR 8157)

Documentation and Error Reporting Improvements

  • Use fully qualified names in error messages for class names resembling builtins(Mukuntha N S, PR 8425)
  • Improve diagnostics involving missing stubs for a library that is installed in site-packages (Michael Lee, PR 8238)
  • Add a section to the documentation about incompatible overrides (RAHUL RAJA, PR 8377)
  • Add variable-sized tuples to the cheat sheet (Marcio Mazza, PR 8364)
  • Improve documentation of decorators (add decorator factories) (Marti Raudsepp, PR 8336)
  • Update documentation of variables and aliases (Xuanda Yang, PR 8200)
  • Report an error if a final class has abstract attributes (Denys Halenok, PR 8332)
  • Update common issues to include __init__ without arguments (Tan Yuanhong, PR 8303)
  • Add a new error code for errors about unreachability (Denys Halenok, PR 8312)
  • Fix error indicator position in code with tabs (dosisod, PR 8307)
  • Document --local-partial-types (Xuanda Yang, PR 8201)
  • Update documentation for Literal types (Michael Lee, PR 8152)

Stubtest rewrite

stubtest is a tool that compares stub definitions to what it finds at runtime with introspection and reports back inconsistencies. It got a complete rewrite. Some features of the new stubtest are:
  • Find missing, extraneous or mistyped classes, methods, functions and attributes in the stubs
  • Check presence, names and kinds of function arguments, accounting for overloads, decorators, *args and kwargs. Checks argument types against their default values, accounting for type vars.
  • Checks @property, @classmethod, @staticmethod declarations
  • Check types of module level and class level attributes and enums

Some results of this:

  • We now run stubtest in typeshed CI (for stdlib)
  • We’ve fixed about 1900 issues in typeshed definitions
  • We’ve greatly improved Python 3.8 support in typeshed and it’ll be easier to make the changes needed for future Python versions
  • We’ve uncovered a handful of issues in Python itself

The stubtest rewrite was contributed by Shanatu.

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:

  • Anthony Sottile
  • Cohen Karnell
  • Denys Halenok
  • dosisod
  • Ethan Smith
  • FooTaGe
  • hauntsaninja
  • HeShan
  • Jacob Beck
  • Jakub Stasiak
  • Jan Verbeek
  • Jérome Perrin
  • lazytype
  • Maksim Kurnikov
  • Marcio Mazza
  • Marti Raudsepp
  • Michael Lee
  • Mukuntha N S
  • Nikita Sobolev
  • PattenR
  • Shantanu
  • Tan Yuanhong
  • Uwe L. Korn
  • Ville Skyttä
  • Xuanda Yang

Additional thanks to all contributors to typeshed:

  • abe
  • Alan Du
  • Alex Grönholm
  • Alex Willmer
  • Andrew Svetlov
  • Anthony Sottile
  • Artjoms Iskovs
  • Batuhan Taşkaya
  • Benjamin Peterson
  • Brian Maissy
  • Cal Paterson
  • Christopher Dignam
  • crusaderky
  • Cyril Roelandt
  • Daniel Däschle
  • Daniel Farley
  • Daniel Hahler
  • Daniël van Eeden
  • Dave Halter
  • dave-shawley
  • Erick
  • Faidon Liambotis
  • Fionn Fitzmaurice
  • François Freitag
  • Gisle Aas
  • hauntsaninja
  • Ian Good
  • ijl
  • Ilaï Deutel
  • Jacob Beck
  • Jacob Ilias Komissar
  • Jakub Stasiak
  • Jan Verbeek
  • Jaromir Latal
  • Jason
  • Jason Gilholme
  • Jelle Zijlstra
  • Jens Hedegaard Nielsen
  • Jeppe Fihl-Pearson
  • layday
  • lazytype
  • Maarten ter Huurne
  • Mark
  • Michael Heyns
  • Mickaël Schoentgen
  • Niklas Fiekas
  • Oleg Höfling
  • Ophir LOJKINE
  • Pavel Savchenko
  • Rebecca Chen
  • Reid Swan
  • rikhilraithatha
  • Romain
  • Rune Tynan
  • Sebastian Rittau
  • Shantanu
  • Squirrel
  • Thomas Schaper
  • tikki
  • Vury Leo
  • Wolf Honore

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.


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

Friday, 29 November 2019

Mypy 0.750 Released

We’ve just uploaded mypy 0.750 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 Powerful Self-types

This version adds support for more patterns involving explicit annotations on self argument in methods. In particular, the mixin pattern got basic support via a protocol as a self annotation that specifies what the mixin expects from any class that subclasses it:

    class Lockable(Protocol):
        def lock(self) -> Lock: ...
    class AtomicCloseMixin:
        def atomic_close(self: Lockable) -> int:
            with self.lock:  # This is OK
                ... # perform some actions
    class File(AtomicCloseMixin):
        def __init__(self) -> None:
            self.lock = Lock()
    class Bad(AtomicCloseMixin):
    f = File()
    b = Bad()
    f.atomic_close()  # OK
    b.atomic_close()  # Error: Invalid self type for "atomic_close"

Another somewhat common pattern that got support via explicit self-types is overloaded generic constructors (in particular in library stubs):

    from typing import Any, AnyStr, Generic
    from typing_extensions import Literal
    class File(Generic[AnyStr]):
        def __init__(self: File[bytes], raw: Literal[True]) -> None: ...
        def __init__(self: File[str], raw: Literal[False]) -> None: ...
        def __init__(self: File[Any], raw: bool = ...) -> None: ...
    reveal_type(File())  # type is File[Any]
    reveal_type(File(raw=True))  # type is File[bytes]

Read the docs for more examples and use cases.

Stubgen Revamped

Stubgen, the automatic stub generator that comes with mypy, has many fixes and improvements. In particular:

  • Recover from various errors that broke stub generation previously, such as inconsistent MROs or modules that crash on import.
  • Export imported names that aren't referenced in the module.
  • Export all names imported from the current package by default. Use --export-less to disable this behavior.
  • Fix various issues that resulted in syntactically invalid stubs being generated.
  • Fix signatures of various C extension methods.
  • Make the docstring parser more robust.
  • Add various heuristics to skip internal modules, such as tests and vendored packages.
  • Allow tweaking the level of logging. Add --verbose and --quiet flags.

Generating stub files has never been so easy and robust! Please read more docs here and try it on the libraries you maintain or use that don’t yet have stubs in Typeshed. Please consider contributing any stubs you create to Typeshed so that the community can share and improve them.

Mypy Daemon is No Longer Experimental

The mypy daemon has been the default way of running mypy locally at Dropbox for well over a year and it's quite stable. It also got several additional commands and flags for better customization (and existing flags got better documentation). In particular, mypy daemon now supports basic integration with external file system event watchers for even faster, sub-second type checking results over tens of thousands of files.

Use dmypy recheck --update FILE and dmypy recheck --remove FILE to directly tell the daemon which files got edited/deleted to avoid waiting for the built-in file system watcher. For more details, see the docs.

Static Inference of Annotations (Experimental)

The mypy daemon now supports (as an experimental feature) statically inferring a draft type annotation for a given function or method. Running dmypy suggest FUNCTION will produce a suggested signature in this format:

    (param_type_1, param_type_2, ...) -> ret_type

This is a low-level command that can be used by editors, IDEs, and other tools, such as the mypy plugin for PyCharm, to propose an annotation to the user, and/or to insert an annotation to a source file.

In this example, the function format_id() has no annotation:

    def format_id(user):
        return "User: {}".format(user)
    root = format_id(0)

Mypy uses call sites and return statements to infer that format_id() accepts an int and returns a str. For more details, see the docs.

Other Notable Improvements and Bug Fixes

  • Show closest candidates for misspelled module attributes (Theodore Liu, PR 7971)
  • Fix C string encoding in the mypyc compiler (Saleem Rashid, PR 7978)
  • Fix type-checking decorated generic methods (beezee, PR 7933)
  • Make revealed type of final variables distinct from non-final ones (Michael Lee, PR 7955)
  • Fix issubclass() to narrow down types of type variables (Xuanda Yang, PR 7930)
  • Refine types of parent expressions when narrowing expression types (Michael Lee, PR 7917)
  • Fix starting compiled mypy daemon on Windows (PR 7929)
  • Show closest candidates for misspelled keyword arguments (Martijn Wuis, PR 7888)
  • Fix type checking of ternary expressions with an empty collection (Xuanda Yang, PR 7892)
  • Support negative integers in literal types (Xuanda Yang, PR 7878)
  • Make sure type inference works for literals against protocols (PR 7882)
  • Don't add coercions when compiling expression statements via mypyc (jag426, PR 7872)
  • Fix crashes in mypy daemon when sorting union items (PR 7842)
  • Enable whole-file # type: ignore comments in Python 2.7 (Brandt Bucher, PR 7789)
  • Record internal Any types as precise in HTML report generator (ahmadF, PR 7708)
  • Add misc/ script to dump parse trees (Michael Wayne Goodman, PR 7820)
  • Use union component as a self-type in class methods (PR 7822)
  • Correctly handle union types in string interpolation checker (Vincent Barbaresi, PR 7809)
  • Fix capturing stderr in (David Tucker, PR 7804)
  • Fix internal error when checking dataclasses with a duplicate field (Vincent Barbaresi, PR 7808)
  • Add cross references to mypy command line options in the documentation (Oleg Höfling, PR 7784, PR 7801, PR 7802)
  • Improve missing import error message (Lewis Cowles, PR 7698)
  • Add option to print absolute file paths (Maxim Koltsov, PR 7754)
  • Recommend using string escaping for classes that are non-generic at runtime (Rubin Raithel, PR 7707)
  • Fix accessing unannotated implicit class methods (PR 7739)
  • Ensure that non-total TypedDicts can be false (henribru, PR 7745)
  • Fix type checking __init_subclass__() (PR 7723)

Plugin API Changes and Improvements

This release includes three major mypy plugin API changes:

  • There is a new plugin hook for specifying per-module configuration data: report_config_data(). It can be used if the plugin has some sort of per-module configuration that can affect type checking.
  • *(breaking change)* New TypeAliasType type has been added and is now used for type aliases instead of eagerly expanding them. To get the expanded type use mypy.types.get_proper_type(). See this comment for more compatibility instructions.
  • *(breaking change)* Symbol node methods .fullname() and .name() were replaced with properties. See this comment for compatibility instructions.

These improvements are related to plugins:

  • Invoke the get_dynamic_class_hook() plugin hook also on method calls (Seth Yastrov, PR 7990)
  • Fix plugin invocation on class method calls through Type[...] (PR 7969)
  • Allow new plugins to be added by programs (like mypyc) that directly invoke build (PR 7875)

For more information on plugin hooks see the docs. Breaking API changes are announced in this GitHub issue. Please subscribe to it if you are an active mypy plugin developer.

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
  • Alexandre Viau
  • beezee
  • Bob Whitelock
  • Brandt Bucher
  • Chad Dombrova
  • Daniel Hahler
  • David Euresti
  • David Tucker
  • Djedouas
  • Ekin Dursun
  • Emmanuel Nosa Evbuomwan
  • Ethan Smith
  • Gábor Lipták
  • henribru
  • hoefling
  • Ilaï Deutel
  • jag426
  • Lewis Cowles
  • Maxim Koltsov
  • Michael Lee
  • Michael R. Crusoe
  • Michael Wayne Goodman
  • Rubin Raithel
  • Saleem Rashid
  • Sebastian Rittau
  • Seth Yastrov
  • TH3CHARLie
  • Theodore Liu
  • Thomas Hisch
  • Tim Gates
  • Tudor Brindus
  • Tuomas Suutari
  • Vasily Zakharov
  • Vincent Barbaresi
  • Wuisch
  • Xuanda Yang
  • Zac Hatfield-Dodds

Additional thanks to all contributors to typeshed:

  • Alexander Schlarb
  • Angela Ambroz
  • Anthony Sottile
  • Árni Már Jónsson
  • Benjamin Peterson
  • bianca rosa
  • Brett Cannon
  • Christopher Hunt
  • cptpcrd
  • Daniel Hahler
  • David Tucker
  • Denis Laxalde
  • Diego Elio Pettenò
  • Dima Boger
  • Eric N. Vander Weele
  • Florian Bruhin
  • Greg Ward
  • Ilaï Deutel
  • Jakub Stasiak
  • Jelle Zijlstra
  • Jeremy Lainé
  • Jon Dufresne
  • JR Heard
  • Lawrence Chan
  • Maarten ter Huurne
  • Markus Bauer
  • Martijn Pieters
  • Martin DeMello
  • Michael Lee
  • Michael Seifert
  • Michał Słapek
  • Nathaniel Brahms
  • Nikita Sobolev
  • Nikola Forró
  • Ran Benita
  • Rebecca Chen
  • Rune Tynan
  • Ryan Morshead
  • Sebastian Rittau
  • Taneli Hukkinen
  • Trim21
  • Utkarsh Gupta
  • Vasily Zakharov
  • 秋葉

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
  • 秋葉

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
  • 秋葉

Friday, 12 July 2019

Mypy 0.720 Released

We’ve just uploaded mypy 0.720 to the Python Package Index (PyPI). Mypy is a static type checker for Python. This release features a new semantic analyzer and optional warnings about unreachable code. 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 Used by Default

The new semantic analyzer was introduced in mypy 0.710 and is now the default. The old analyzer is still available under --no-new-semantic-analyzer, but it will be removed in the next mypy release. This release includes many improvements to the new semantic analyzer.

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. The new semantic analyzer is a major redesign and achieves several things.

The old semantic analyzer ordinarily processes files in a linear order, from the beginning to the end. This is subtly different in the mypy daemon, which sometimes processes an individual function or a module top level (code not nested within a function) as a single unit. The new semantic analyzer unifies mypy behavior by always analyzing module top-levels separately, before analyzing functions. As a bonus, this more closely reflects Python runtime behavior, since modules are usually fully initialized before any functions are called.

The old semantic analyzer processes each module in an import cycle twice, in an order determined by complicated heuristics. This can cause bogus errors when there are references to another module that hasn’t been processed yet in a dependency cycle. The new analyzer processes modules in a flexible order, and references across an import cycle work significantly more reliably.

The old semantic analyzer can’t always deal with forward references to definitions. The new semantic analyzer improves support for forward references. Example where this helps:

    from typing import NamedTuple
    f = lambda: A(1)  # This forward reference now works
    A = NamedTuple('A', [('x', int)])

The new semantic analyzer is stricter than the old semantic analyzer. It catches additional type errors and quality issues. Existing code that passes type checking may require small adjustments.

New Feature: Warn about Unreachable Code

If you run mypy with --warn-unreachable, mypy will warn you about unreachable statements, and expressions that may be never evaluated because of short circuiting. For example:

    from typing import Text
    def emphasize(text: Text) -> Text:
        if isinstance(text, int):
            text += 1  # Error: statement is unreachable
        return text.upper()

See the documentation for more information. This feature was contributed by Michael Lee.

Plugin API Changes

The new semantic analyzer may require changes to mypy plugins. We have a GitHub issue with more information for maintainers of plugins.

Notable Improvements and Bug Fixes

  • Have --package usage respect mypy_path (Aaron Batilo, PR 6926)
  • Support flexible option specification in config files. Now all the flags support invertible forms so that one can specify either strict_optional = False or no_strict_optional = True. This matches how flags work on the command line (PR 7054)
  • Fix reachability inference with isinstance(Any, Any) (Michael Lee, PR 7048)
  • Fix mypyc crash with plugins using get_customize_class_mro_hook() (PR 7075)
  • Fix slicing tuples with non-literal expressions (Ethan Smith, PR 7066)
  • Fixes to column numbers in error messages (PR 7078, PR 7081)
  • Make Need type annotation for variable error more consistent (PR 7079, PR 7113)
  • Fix error reporting context for missing generic type arguments (PR 7100)
  • Use only directories containing no as namespace packages (Arne Welzel, PR 7108)
  • Support method plugin hooks on union types (PR 6560)
  • Fix some corner cases in --strict-equality (PR 7156, PR 7164)
  • Fix binding of self-types on unions (PR 7152)
  • Add more details for Invalid type errors (PR 7166)
  • Fix custom __init__() in dataclasses (Ryan Gonzalez, PR 7168)
  • Better support for indexing with unions of literal types (Michael Lee, PR 6558)

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.


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:

  • Aaron Batilo
  • Arne Welzel
  • Chad Birch
  • Daniel Hahler
  • Deepyaman Datta
  • Ethan Smith
  • Michael Lee
  • Rafi Blecher
  • Ryan Gonzalez

Additional thanks to all contributors to typeshed:

  • Benjamin Woodruff
  • Chad Dombrova
  • Cole Maclean
  • crusaderky
  • Eric Arellano
  • Florian Ludwig
  • Francis Colas
  • Gordon Ball
  • Hynek Schlawack
  • ikelos
  • Jason Gilholme
  • Jelle Zijlstra
  • Maarten ter Huurne
  • Martijn Pieters
  • Mathieu Bridon
  • Matthew Wilkes
  • Motoki Naruse
  • Rafi Blecher
  • Ran Benita
  • Rebecca Chen
  • redshiftzero
  • Rune Tynan
  • Sam Zhou
  • Savo Kovačević
  • Sebastian Rittau
  • 秋葉
— Ivan Levkivskyi, on behalf of the mypy team