Friday 12 October 2018

Mypy 0.641 Released

THIS RELEASE REPLACES 0.640. The bug reported in issue 5784 has been fixed. There are no other changes.

We’ve just uploaded mypy 0.641 to the Python Package Index (PyPI). Mypy is an optional 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 documentation for this release on ReadTheDocs.

New Feature: Final Qualifiers

In this release we’re rolling out an exciting new (experimental) feature for the type system: final qualifiers.


Using this feature requires installing the latest typing_extensions package, version 3.6.6:
    python3 -m pip install -U typing_extensions

Final variables

You can declare a variable “final” using the Final qualifier, like so:
    from typing_extensions import Final
    RATE: Final = 3000
    # ...
    RATE = 300  # error: Cannot assign to final name "RATE"
This also works for local variables, class variables and instance variables. For class variables it implies that subclasses cannot override the value either. You can also use a type comment (# type: Final).
Note that “final” variables may have a mutable type, and then they can still be mutated:
    PLATFORMS: Final = ['mac', 'windows']
    PLATFORMS.append('linux')  # OK

Final methods and classes

You can also declare methods “final”, meaning they cannot be overridden by a subclass:
    from typing_extensions import final  # Note lower-case 'f'
    class Base:
       def update(self) -> None:
    class Derived(base):
        def update(self) -> None:  # error: Cannot override final attribute "update"
The @final decorator can also be used for classes, where it means they cannot be subclassed.


Final qualifiers only have an effect during type checking, in type-checked code; they don’t do anything at runtime.


The full documentation has additional details about this feature.

New Feature: Allow Omitting Return Type for __init__

It is now possible to omit the return type of an annotated __init__ method without getting an error message. For example:
    class Circuit:
        def __init__(self, voltage: float):
            self.voltage = voltage
In previous mypy versions this would elicit an error message:
    error: The return type of "__init__" must be None
This error was just annoying as the only legal return declaration for __init__ is -> None, so we’ve removed it. Note that this only works if there’s at least one annotated argument! For __init__ methods without arguments you must still add -> None, otherwise the method will be treated as untyped, and its body will not be type checked at all. Examples:
    class UntypedExample:
        # This method is not type-checked at all!
        def __init__(self):
            self.voltage = 0.0
    class TypedExample:
        # This is how to ensure that a 0-argument __init__ is type-checked:
        def __init__(self) -> None:
            self.voltage = 0.0

New Feature: Namespace Package Support

Python 3 supports packages without files, better known as namespace packages. These were introduced by PEP 420 as a mechanism for splitting a single Python package across multiple directories. There are some subtleties in their semantics (e.g. a package with file are preferred even if they occur later on sys.path), and until this release mypy did not support them. Support is now provided using the command-line flag --namespace-packages (or the correponding config file phrase namespace_packages = True). Note that the default is still off. At some point in the future the default may change to on for Python 3. (Python 2 doesn’t support namespace packages, though mypy will let you combine this flag with --py2.)

Other Flags and Options Changes

  • New flag: --allow-untyped-globals suppresses errors caused by not being able to fully infer the types of global and class variables (PR 5670)
  • Deprecated flag: --quick-and-dirty ; it will be removed in a future release (PR 5737)
  • Removed flags, all of which have been deprecated for a long time: --disallow-any, -f/--dirty-stubs, --use-python-path, -s/--silent-imports, --almost-silent, --[no-]fast-parser, --strict-boolean (for the latter, see discussion in issue 3195) (PR 5740)
  • The -2/--py2 flag’s behavior now matches --python-version (sqwishy, PR 5619)
  • New experimental flag: --fast-exit makes mypy exit faster (by skipping final GC pass) (PR 5569)

Other Improvements and Notable Bugs Fixed

  • Get rid of Optional when assigning Any in an is None branch (PR 5629)
  • Don't crash on aliases of the form C = C (PR 5632)
  • Fix meet for tuple with instance (PR 5641)
  • Add missing fine-grained dependencies when using Type[X] (PR 5607)
  • Make NamedTuple provide __new__ instead of __init__ (PR 5643)
  • Fully analyze named tuple subclasses in third pass (PR 5644)
  • Defer subclass methods if superclass has not been analyzed to fix issues with checking method overrides in import cycles (PR 5637)
  • Fix type object signature when both __new__ and __init__ present (PR 5642)
  • Fix crash related to decorated functions (PR 5654)
  • Fix “Invalid type” errors in import cycles (PR 5635)
  • Make classmethod's first argument be Type[...] and fix __init__ defined after a class method (PR 5646)
  • Allow comparisons to refine Optional types without strict-optional (PR 4523)
  • Fix untyped decorator check for class instances (Daniel Izquierdo, PR 5509)
  • Fix bug with inferring bad arguments to overloads (Michael Lee, PR 5660)
  • Add the current directory to sys.path when running stubgen (PR 5673)
  • Allow returning inferred None from functions (PR 5663)
  • Support stubs marked with py.typed in PEP-420 nested packages (Chris Philip, PR 5591)
  • Don't report "function does not return a value" in unannotated functions (PR 5683)
  • Make return type implicitly None for type checked __init__ and __init_subclass__ (Ekin Dursun, PR 5677)
  • Improve usage of outer context for inference (PR 5699)
  • Reformat and reorganize the config file docs (Michael Lee, PR 5595)


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:
  • ceh
  • Chris Philip
  • Daniel Izquierdo
  • Ekin Dursun
  • Elazar Gershuni
  • Ethan Smith
  • Jan Teske
  • Joel Croteau
  • Michael Lee
  • sqwishy
Additional thanks to all contributors to typeshed:
  • Adam Dangoor
  • Benjamin Peterson
  • Christian Haudum
  • Dmitry Shachnev
  • Emil Hessman
  • Grzegorz Śliwiński
  • Israel Tsadok
  • Jan Teske
  • Jelle Zijlstra
  • Jon Dufresne
  • kitsuyui
  • Manuel Vázquez Acosta
  • Martin DeMello
  • Matt Robinson
  • Matthew Christopher
  • Michael R. Crusoe
  • Nathaniel Manista
  • Ran Benita
  • Rebecca Chen
  • Richard Levasseur
  • Ruben Berenguel
  • Scott Colby
  • Sebastian Kreft
  • Sebastian Rittau
  • Siva Chandra
  • spdkils
  • Teddy Sudol
  • Wim L
  • Zac Hatfield-Dodds
— Guido van Rossum, on behalf of the mypy team