Tuesday 10 October 2023

Mypy 1.6 Released

We’ve just uploaded mypy 1.6 to the Python Package Index (PyPI). Mypy is a static type checker for Python. This release includes new features, performance improvements and bug fixes. 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.

Introduce Error Subcodes for Import Errors

Mypy now uses the error code import-untyped if an import targets an installed library that doesn’t support static type checking, and no stub files are available. Other invalid imports produce the import-not-found error code. They both are subcodes of the import error code, which was previously used for both kinds of import-related errors.

Use --disable-error-code=import-untyped to only ignore import errors about installed libraries without stubs. This way mypy will still report errors about typos in import statements, for example.

If you use --warn-unused-ignore or --strict, mypy will complain if you use # type: ignore[import] to ignore an import error. You are expected to use one of the more specific error codes instead. Otherwise, ignoring the import error code continues to silence both errors.

This feature was contributed by Shantanu (PR 15840, PR 14740).

Remove Support for Targeting Python 3.6 and Earlier

Running mypy with --python-version 3.6, for example, is no longer supported. Python 3.6 hasn’t been properly supported by mypy for some time now, and this makes it explicit. This was contributed by Nikita Sobolev (PR 15668).

Selective Filtering of --disallow-untyped-calls Targets

Using --disallow-untyped-calls could be annoying when using libraries with missing type information, as mypy would generate many errors about code that uses the library. Now you can use --untyped-calls-exclude=acme, for example, to disable these errors about calls targeting functions defined in the acme package. Refer to the documentation for more information.

This feature was contributed by Ivan Levkivskyi (PR 15845).

Improved Type Inference between Callable Types

Mypy now does a better job inferring type variables inside arguments of callable types. For example, this code fragment now type checks correctly:

    def f(c: Callable[[T, S], None]) -> Callable[[str, T, S], None]: ...
    def g(*x: int) -> None: ...
    
    reveal_type(f(g))  # Callable[[str, int, int], None]

This was contributed by Ivan Levkivskyi (PR 15910).

Don’t Consider None and TypeVar to Overlap in Overloads

Mypy now doesn’t consider an overload item with an argument type None to overlap with a type variable:

    @overload
    def f(x: None) -> None: ..
    @overload
    def f(x: T) -> Foo[T]: ...
    ...

Previously mypy would generate an error about the definition of f above. This is slightly unsafe if the upper bound of T is object, since the value of the type variable could be None. We relaxed the rules a little, since this solves a common issue.

This feature was contributed by Ivan Levkivskyi (PR 15846).

Improvements to --new-type-inference

The experimental new type inference algorithm (polymorphic inference) introduced as an opt-in feature in mypy 1.5 has several improvements:

  • Improve transitive closure computation during constraint solving (Ivan Levkivskyi, PR 15754)
  • Add support for upper bounds and values with --new-type-inference (Ivan Levkivskyi, PR 15813)
  • Basic support for variadic types with --new-type-inference (Ivan Levkivskyi, PR 15879)
  • Polymorphic inference: support for parameter specifications and lambdas (Ivan Levkivskyi, PR 15837)
  • Invalidate cache when adding --new-type-inference (Marc Mueller, PR 16059)

Note: We are planning to enable --new-type-inference by default in mypy 1.7. Please try this out and let us know if you encounter any issues.

ParamSpec Improvements

  • Support self-types containing ParamSpec (Ivan Levkivskyi, PR 15903)
  • Allow “…” in Concatenate, and clean up ParamSpec literals (Ivan Levkivskyi, PR 15905)
  • Fix ParamSpec inference for callback protocols (Ivan Levkivskyi, PR 15986)
  • Infer ParamSpec constraint from arguments (Ivan Levkivskyi, PR 15896)
  • Fix crash on invalid type variable with ParamSpec (Ivan Levkivskyi, PR 15953)
  • Fix subtyping between ParamSpecs (Ivan Levkivskyi, PR 15892)

Stubgen Improvements

  • Add option to include docstrings with stubgen (chylek, PR 13284)
  • Add required ... initializer to NamedTuple fields with default values (Nikita Sobolev, PR 15680)

Stubtest Improvements

  • Fix __mypy-replace false positives (Alex Waygood, PR 15689)
  • Fix edge case for bytes enum subclasses (Alex Waygood, PR 15943)
  • Generate error if typeshed is missing modules from the stdlib (Alex Waygood, PR 15729)
  • Fixes to new check for missing stdlib modules (Alex Waygood, PR 15960)
  • Fix stubtest enum.Flag edge case (Alex Waygood, PR 15933)

Documentation Improvements

  • Do not advertise to create your own assert_never helper (Nikita Sobolev, PR 15947)
  • Fix all the missing references found within the docs (Albert Tugushev, PR 15875)
  • Document await-not-async error code (Shantanu, PR 15858)
  • Improve documentation of disabling error codes (Shantanu, PR 15841)

Other Notable Changes and Fixes

  • Make unsupported PEP 695 features (introduced in Python 3.12) give a reasonable error message (Shantanu, PR 16013)
  • Remove the --py2 command-line argument (Marc Mueller, PR 15670)
  • Change empty tuple from tuple[] to tuple[()] in error messages (Nikita Sobolev, PR 15783)
  • Fix assert_type failures when some nodes are deferred (Nikita Sobolev, PR 15920)
  • Generate error on unbound TypeVar with values (Nikita Sobolev, PR 15732)
  • Fix over-eager types-google-cloud-ndb suggestion (Shantanu, PR 15347)
  • Fix type narrowing of == None and in (None,) conditions (Marti Raudsepp, PR 15760)
  • Fix inference for attrs.fields (Shantanu, PR 15688)
  • Make “await in non-async function” a non-blocking error and give it an error code (Gregory Santosa, PR 15384)
  • Add basic support for decorated overloads (Ivan Levkivskyi, PR 15898)
  • Fix TypeVar regression with self types (Ivan Levkivskyi, PR 15945)
  • Add __match_args__ to dataclasses with no fields (Ali Hamdan, PR 15749)
  • Include stdout and stderr in dmypy verbose output (Valentin Stanciu, PR 15881)
  • Improve match narrowing and reachability analysis (Shantanu, PR 15882)
  • Support __bool__ with Literal in --warn-unreachable (Jannic Warken, PR 15645)
  • Fix inheriting from generic @frozen attrs class (Ilya Priven, PR 15700)
  • Correctly narrow types for tuple[type[X], ...] (Nikita Sobolev, PR 15691)
  • Don't flag intentionally empty generators unreachable (Ilya Priven, PR 15722)
  • Add tox.ini to mypy sdist (Marcel Telka, PR 15853)
  • Fix mypyc regression with pretty (Shantanu, PR 16124)

Typeshed Updates

Typeshed is now modular and distributed as separate PyPI packages for everything except the standard library stubs. Please see git log for full list of typeshed changes.

Acknowledgements

Thanks to Max Murin, who did most of the release manager work for this release (I just did the final steps).

Thanks to all mypy contributors who contributed to this release:

  • Albert Tugushev
  • Alex Waygood
  • Ali Hamdan
  • chylek
  • EXPLOSION
  • Gregory Santosa
  • Ilya Priven
  • Ivan Levkivskyi
  • Jannic Warken
  • KotlinIsland
  • Marc Mueller
  • Marcel Johannesmann
  • Marcel Telka
  • Mark Byrne
  • Marti Raudsepp
  • Max Murin
  • Nikita Sobolev
  • Shantanu
  • Valentin Stanciu