We’ve just uploaded mypy 1.16 to the Python Package Index (PyPI). Mypy is a static type checker for Python. This release includes new features 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.
Different Property Getter and Setter Types
Mypy now supports using different types for a property getter and setter:
class A:
_value: int
@property
def foo(self) -> int:
return self._value
@foo.setter
def foo(self, x: str | int) -> None:
try:
self._value = int(x)
except ValueError:
raise Exception(f"'{x}' not valid value for 'foo'")
This was contributed by Ivan Levkivskyi (PR 18510).
Flexible Variable Redefinitions (Experimental)
Mypy now allows unannotated variables to be freely redefined with different types when using the experimental --allow-redefinition-new flag. You will also need to enable --local-partial-types. Mypy will now infer a union type when different types are assigned to a variable:
# mypy: allow-redefinition-new, local-partial-types
def f(n: int, b: bool) -> int | str:
if b:
x = n
else:
x = str(n)
# Type of 'x' is int | str here.
return x
Without the new flag, mypy only supports inferring optional types (X | None) from multiple assignments, but now mypy can infer arbitrary union types.
An unannotated variable can now also have different types in different code locations:
# mypy: allow-redefinition-new, local-partial-types
...
if cond():
for x in range(n):
# Type of 'x' is 'int' here
...
else:
for x in ['a', 'b']:
# Type of 'x' is 'str' here
...
We are planning to turn this flag on by default in mypy 2.0, along with --local-partial-types. The feature is still experimental and has known issues, and the semantics may still change in the future. You may need to update or add type annotations when switching to the new behavior, but if you encounter anything unexpected, please create a GitHub issue.
This was contributed by Jukka Lehtosalo (PR 18727, PR 19153).
Stricter Type Checking with Imprecise Types
Mypy can now detect additional errors in code that uses Any types or has missing function annotations.
When calling dict.get(x, None) on an object of type dict[str, Any], this now results in an optional type (in the past it was Any):
def f(d: dict[str, Any]) -> int:
# Error: Return value is "Any | None" but expected "int"
return d.get("x", None)
Type narrowing using assignments can result in more precise types in the presence of Any types:
def foo(): ...
def bar(n: int) -> None:
x = foo()
# Type of 'x' is 'Any' here
if n > 5:
x = str(n)
# Type of 'x' is 'str' here
When using --check-untyped-defs, unannotated overrides are now checked more strictly against superclass definitions.
Related PRs:
- Use union types instead of join in binder (Ivan Levkivskyi, PR 18538)
- Check superclass compatibility of untyped methods if --check-untyped-defs is set (Stanislav Terliakov, PR 18970)
Improvements to Attribute Resolution
This release includes several fixes to inconsistent resolution of attribute, method and descriptor types.
- Consolidate descriptor handling (Ivan Levkivskyi, PR 18831)
- Make multiple inheritance checking use common semantics (Ivan Levkivskyi, PR 18876)
- Make method override checking use common semantics (Ivan Levkivskyi, PR 18870)
- Fix descriptor overload selection (Ivan Levkivskyi, PR 18868)
- Handle union types when binding self (Ivan Levkivskyi, PR 18867)
- Make variable override checking use common semantics (Ivan Levkivskyi, PR 18847)
- Make descriptor handling behave consistently (Ivan Levkivskyi, PR 18831)
Make Implementation for Abstract Overloads Optional
The implementation can now be omitted for abstract overloaded methods, even outside stubs:
from abc import abstractmethod
from typing import overload
class C:
@abstractmethod
@overload
def foo(self, x: int) -> int: ...
@abstractmethod
@overload
def foo(self, x: str) -> str: ...
# No implementation required for "foo"
This was contributed by Ivan Levkivskyi (PR 18882).
Option to Exclude Everything in .gitignore
You can now use --exclude-gitignore to exclude everything in a .gitignore file from the mypy build. This behaves similar to excluding the paths using --exclude. We might enable this by default in a future mypy release.
This was contributed by Ivan Levkivskyi (PR 18696).
Selectively Disable Deprecated Warnings
It's now possible to selectively disable warnings generated from warnings.deprecated using the --deprecated-calls-exclude option:
# mypy --enable-error-code deprecated
# --deprecated-calls-exclude=foo.A
import foo
foo.A().func() # OK, the deprecated warning is ignored
# file foo.py
from typing_extensions import deprecated
class A:
@deprecated("Use A.func2 instead")
def func(self): pass
...
Contributed by Marc Mueller (PR 18641)
Annotating Native/Non-Native Classes in Mypyc
You can now declare a class as a non-native class when compiling with mypyc. Unlike native classes, which are extension classes and have an immutable structure, non-native classes are normal Python classes at runtime and are fully dynamic. Example:
from mypy_extensions import mypyc_attr
@mypyc_attr(native_class=False)
class NonNativeClass:
...
o = NonNativeClass()
# Ok, even if attribute "foo" not declared in class body
setattr(o, "foo", 1)
Classes are native by default in compiled modules, but classes that use certain features (such as most metaclasses) are implicitly non-native.
You can also explicitly declare a class as native. In this case mypyc will generate an error if it can't compile the class as a native class, instead of falling back to a non-native class:
from mypy_extensions import mypyc_attr
from foo import MyMeta
# Error: Unsupported metaclass for a native class
@mypyc_attr(native_class=True)
class C(metaclass=MyMeta):
...
Since native classes are significantly more efficient that non-native classes, you may want to ensure that certain classes always compiled as native classes.
This feature was contributed by Valentin Stanciu (PR 18802).
Mypyc Fixes and Improvements
- Improve documentation of native and non-native classes (Jukka Lehtosalo, PR 19154)
- Fix compilation when using Python 3.13 debug build (Valentin Stanciu, PR 19045)
- Show the reason why a class can't be a native class (Valentin Stanciu, PR 19016)
- Support await/yield while temporary values are live (Michael J. Sullivan, PR 16305)
- Fix spilling values with overlapping error values (Jukka Lehtosalo, PR 18961)
- Fix reference count of spilled register in async def (Jukka Lehtosalo, PR 18957)
- Add basic optimization for sorted (Marc Mueller, PR 18902)
- Fix access of class object in a type annotation (Advait Dixit, PR 18874)
- Optimize list.__imul__ and tuple.__mul__ (Marc Mueller, PR 18887)
- Optimize list.__add__, list.__iadd__ and tuple.__add__ (Marc Mueller, PR 18845)
- Add and implement primitive list.copy() (exertustfm, PR 18771)
- Optimize builtins.repr (Marc Mueller, PR 18844)
- Support iterating over keys/values/items of dict-bound TypeVar and ParamSpec.kwargs (Stanislav Terliakov, PR 18789)
- Add efficient primitives for str.strip() etc. (Advait Dixit, PR 18742)
- Document that strip() etc. are optimized (Jukka Lehtosalo, PR 18793)
- Fix mypyc crash with enum type aliases (Valentin Stanciu, PR 18725)
- Optimize str.find and str.rfind (Marc Mueller, PR 18709)
- Optimize str.__contains__ (Marc Mueller, PR 18705)
- Fix order of steal/unborrow in tuple unpacking (Ivan Levkivskyi, PR 18732)
- Optimize str.partition and str.rpartition (Marc Mueller, PR 18702)
- Optimize str.startswith and str.endswith with tuple argument (Marc Mueller, PR 18678)
- Improve str.startswith and str.endswith with tuple argument (Marc Mueller, PR 18703)
- pythoncapi_compat: don't define Py_NULL if it is already defined (Michael R. Crusoe, PR 18699)
- Optimize str.splitlines (Marc Mueller, PR 18677)
- Mark dict.setdefault as optimized (Marc Mueller, PR 18685)
- Support __del__ methods (Advait Dixit, PR 18519)
- Optimize str.rsplit (Marc Mueller, PR 18673)
- Optimize str.removeprefix and str.removesuffix (Marc Mueller, PR 18672)
- Recognize literal types in __match_args__ (Stanislav Terliakov, PR 18636)
- Fix non extension classes with attribute annotations using forward references (Valentin Stanciu, PR 18577)
- Use lower-case generic types such as list[t] in documentation (Jukka Lehtosalo, PR 18576)
- Improve support for frozenset (Marc Mueller, PR 18571)
- Fix wheel build for cp313-win (Marc Mueller, PR 18560)
- Reduce impact of immortality (introduced in Python 3.12) on reference counting performance (Jukka Lehtosalo, PR 18459)
- Update math error messages for 3.14 (Marc Mueller, PR 18534)
- Update math error messages for 3.14 (2) (Marc Mueller, PR 18949)
- Replace deprecated _PyLong_new with PyLongWriter API (Marc Mueller, PR 18532)
Fixes to Crashes
- Traverse module ancestors when traversing reachable graph nodes during dmypy update (Stanislav Terliakov, PR 18906)
- Fix crash on multiple unpacks in a bare type application (Stanislav Terliakov, PR 18857)
- Prevent crash when enum/TypedDict call is stored as a class attribute (Stanislav Terliakov, PR 18861)
- Fix crash on multiple unpacks in a bare type application (Stanislav Terliakov, PR 18857)
- Fix crash on type inference against non-normal callables (Ivan Levkivskyi, PR 18858)
- Fix crash on decorated getter in settable property (Ivan Levkivskyi, PR 18787)
- Fix crash on callable with *args and suffix against Any (Ivan Levkivskyi, PR 18781)
- Fix crash on deferred supertype and setter override (Ivan Levkivskyi, PR 18649)
- Fix crashes on incorrectly detected recursive aliases (Ivan Levkivskyi, PR 18625)
- Report that NamedTuple and dataclass are incompatile instead of crashing (Christoph Tyralla, PR 18633)
- Fix mypy daemon crash (Valentin Stanciu, PR 19087)
Performance Improvements
These are specific to mypy. Mypyc-related performance improvements are discussed elsewhere.
- Speed up binding self in trivial cases (Ivan Levkivskyi, PR 19024)
- Small constraint solver optimization (Aaron Gokaslan, PR 18688)
Documentation Updates
- Improve documentation of --strict (lenayoung8, PR 18903)
- Remove a note about from __future__ import annotations (Ageev Maxim, PR 18915)
- Improve documentation on type narrowing (Tim Hoffmann, PR 18767)
- Fix metaclass usage example (Georg, PR 18686)
- Update documentation on extra_checks flag (Ivan Levkivskyi, PR 18537)
Stubgen Improvements
- Fix TypeAlias handling (Alexey Makridenko, PR 18960)
- Handle arg=None in C extension modules (Anthony Sottile, PR 18768)
- Fix valid type detection to allow pipe unions (Chad Dombrova, PR 18726)
- Include simple decorators in stub files (Marc Mueller, PR 18489)
- Support positional and keyword-only arguments in stubdoc (Paul Ganssle, PR 18762)
- Fall back to Incomplete if we are unable to determine the module name (Stanislav Terliakov, PR 19084)
Stubtest Improvements
- Make stubtest ignore __slotnames__ (Nick Pope, PR 19077)
- Fix stubtest tests on 3.14 (Jelle Zijlstra, PR 19074)
- Support for strict_bytes in stubtest (Joren Hammudoglu, PR 19002)
- Understand override (Shantanu, PR 18815)
- Better checking of runtime arguments with dunder names (Shantanu, PR 18756)
- Ignore setattr and delattr inherited from object (Stephen Morton, PR 18325)
Miscellaneous Fixes and Improvements
- Add --strict-bytes to --strict (wyattscarpenter, PR 19049)
- Admit that Final variables are never redefined (Stanislav Terliakov, PR 19083)
- Add special support for @django.cached_property needed in django-stubs (sobolevn, PR 18959)
- Do not narrow types to Never with binder (Ivan Levkivskyi, PR 18972)
- Local forward references should precede global forward references (Ivan Levkivskyi, PR 19000)
- Do not cache module lookup results in incremental mode that may become invalid (Stanislav Terliakov, PR 19044)
- Only consider meta variables in ambiguous "any of" constraints (Stanislav Terliakov, PR 18986)
- Allow accessing __init__ on final classes and when __init__ is final (Stanislav Terliakov, PR 19035)
- Treat varargs as positional-only (A5rocks, PR 19022)
- Enable colored output for argparse help in Python 3.14 (Marc Mueller, PR 19021)
- Fix argparse for Python 3.14 (Marc Mueller, PR 19020)
- dmypy suggest can now suggest through contextmanager-based decorators (Anthony Sottile, PR 18948)
- Fix __r<magic_methods>__ being used under the same __<magic_method>__ hook (Arnav Jain, PR 18995)
- Prioritize .pyi from -stubs packages over bundled .pyi (Joren Hammudoglu, PR 19001)
- Fix missing subtype check case for type[T] (Stanislav Terliakov, PR 18975)
- Fixes to the detection of redundant casts (Anthony Sottile, PR 18588)
- Make some parse errors non-blocking (Shantanu, PR 18941)
- Fix PEP 695 type alias with a mix of type arguments (PEP 696) (Marc Mueller, PR 18919)
- Allow deeper recursion in mypy daemon, better error reporting (Carter Dodd, PR 17707)
- Fix swapped errors for frozen/non-frozen dataclass inheritance (Nazrawi Demeke, PR 18918)
- Fix incremental issue with namespace packages (Shantanu, PR 18907)
- Exclude irrelevant members when narrowing union overlapping with enum (Stanislav Terliakov, PR 18897)
- Flatten union before contracting literals when checking subtyping (Stanislav Terliakov, PR 18898)
- Do not add kw_only dataclass fields to __match_args__ (sobolevn, PR 18892)
- Fix error message when returning long tuple with type mismatch (Thomas Mattone, PR 18881)
- Treat TypedDict (old-style) aliases as regular TypedDicts (Stanislav Terliakov, PR 18852)
- Warn about unused type: ignore comments when error code is disabled (Brian Schubert, PR 18849)
- Reject duplicate ParamSpec.{args,kwargs} at call site (Stanislav Terliakov, PR 18854)
- Make detection of enum members more consistent (sobolevn, PR 18675)
- Admit that **kwargs mapping subtypes may have no direct type parameters (Stanislav Terliakov, PR 18850)
- Don't suggest types-setuptools for pkg_resources (Shantanu, PR 18840)
- Suggest scipy-stubs for scipy as non-typeshed stub package (Joren Hammudoglu, PR 18832)
- Narrow tagged unions in match statements (Gene Parmesan Thomas, PR 18791)
- Consistently store settable property type (Ivan Levkivskyi, PR 18774)
- Do not blindly undefer on leaving function (Ivan Levkivskyi, PR 18674)
- Process superclass methods before subclass methods in semanal (Ivan Levkivskyi, PR 18723)
- Only defer top-level functions (Ivan Levkivskyi, PR 18718)
- Add one more type-checking pass (Ivan Levkivskyi, PR 18717)
- Properly account for member and nonmember in enums (sobolevn, PR 18559)
- Fix instance vs tuple subtyping edge case (Ivan Levkivskyi, PR 18664)
- Improve handling of Any/object in variadic generics (Ivan Levkivskyi, PR 18643)
- Fix handling of named tuples in class match pattern (Ivan Levkivskyi, PR 18663)
- Fix regression for user config files (Shantanu, PR 18656)
- Fix dmypy socket issue on GNU/Hurd (Mattias Ellert, PR 18630)
- Don't assume that for loop body index variable is always set (Jukka Lehtosalo, PR 18631)
- Fix overlap check for variadic generics (Ivan Levkivskyi, PR 18638)
- Improve support for functools.partial of overloaded callable protocol (Shantanu, PR 18639)
- Allow lambdas in except* clauses (Stanislav Terliakov, PR 18620)
- Fix trailing commas in many multiline string options in pyproject.toml (sobolevn, PR 18624)
- Allow trailing commas for files setting in mypy.ini and setup.ini (sobolevn, PR 18621)
- Fix "not callable" issue for @dataclass(frozen=True) with Final attr (sobolevn, PR 18572)
- Add missing TypedDict special case when checking member access (Stanislav Terliakov, PR 18604)
- Use lower case list and dict in invariance notes (Jukka Lehtosalo, PR 18594)
- Fix inference when class and instance match protocol (Ivan Levkivskyi, PR 18587)
- Remove support for builtins.Any (Marc Mueller, PR 18578)
- Update the overlapping check for tuples to account for NamedTuples (A5rocks, PR 18564)
- Fix @deprecated (PEP 702) with normal overloaded methods (Christoph Tyralla, PR 18477)
- Start propagating end columns/lines for type-arg errors (A5rocks, PR 18533)
- Improve handling of type(x) is Foo checks (Stanislav Terliakov, PR 18486)
- Suggest typing.Literal for exit-return error messages (Marc Mueller, PR 18541)
- Allow redefinitions in except/else/finally (Stanislav Terliakov, PR 18515)
- Disallow setting Python version using inline config (Shantanu, PR 18497)
- Improve type inference in tuple multiplication plugin (Shantanu, PR 18521)
- Add missing line number to yield from with wrong type (Stanislav Terliakov, PR 18518)
- Hint at argument names when formatting callables with compatible return types in error messages (Stanislav Terliakov, PR 18495)
- Add better naming and improve compatibility for ad hoc intersections of instances (Christoph Tyralla, PR 18506)
Acknowledgements
Thanks to all mypy contributors who contributed to this release:
- A5rocks
- Aaron Gokaslan
- Advait Dixit
- Ageev Maxim
- Alexey Makridenko
- Ali Hamdan
- Anthony Sottile
- Arnav Jain
- Brian Schubert
- bzoracler
- Carter Dodd
- Chad Dombrova
- Christoph Tyralla
- Dimitri Papadopoulos Orfanos
- Emma Smith
- exertustfm
- Gene Parmesan Thomas
- Georg
- Ivan Levkivskyi
- Jared Hance
- Jelle Zijlstra
- Joren Hammudoglu
- lenayoung8
- Marc Mueller
- Mattias Ellert
- Michael J. Sullivan
- Michael R. Crusoe
- Nazrawi Demeke
- Nick Pope
- Paul Ganssle
- Shantanu
- sobolevn
- Stanislav Terliakov
- Stephen Morton
- Thomas Mattone
- Tim Hoffmann
- Tim Ruffing
- Valentin Stanciu
- Wesley Collin Wright
- wyattscarpenter
I’d also like to thank my employer, Dropbox, for supporting mypy development.