We’ve just uploaded mypy 1.13 to the Python Package Index (PyPI).
Mypy is a static type checker for Python. 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.
Note that unlike typical releases, Mypy 1.13 does not have any changes to type checking semantics
from 1.12.1.
Improved Performance
Mypy 1.13 contains several performance improvements. Users can expect mypy to be 5-20% faster.
In environments with long search paths (such as environments using many editable installs), mypy
can be significantly faster, e.g. 2.2x faster in the use case targeted by these improvements.
Mypy 1.13 allows use of the orjson library for handling the cache instead of the stdlib json,
for improved performance. You can ensure the presence of orjson using the faster-cache extra:
python3 -m pip install -U mypy[faster-cache]
Mypy may depend on orjson by default in the future.
These improvements were contributed by Shantanu.
List of changes:
Significantly speed up file handling error paths (Shantanu, PR 17920)
Use fast path in modulefinder more often (Shantanu, PR 17950)
Let mypyc optimise os.path.join (Shantanu, PR 17949)
We’ve just uploaded mypy 1.12 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.
Support Python 3.12 Syntax for Generics (PEP 695)
Support for the new type parameter syntax introduced in Python 3.12 is now enabled by default,
documented, and no longer experimental. It was available through a feature flag in
mypy 1.11 as an experimental feature.
This example demonstrates the new syntax:
# Generic function
def f[T](x: T) -> T: ...
reveal_type(f(1)) # Revealed type is 'int'
# Generic class
class C[T]:
def __init__(self, x: T) -> None:
self.x = x
c = C('a')
reveal_type(c.x) # Revealed type is 'str'
# Type alias
type A[T] = C[list[T]]
Add detection and error reporting for the use of incorrect expressions within the scope of a type parameter and a type alias (Kirill Podoprigora, PR 17560)
Basic Support for Python 3.13
This release adds partial support for Python 3.13 features and compiled binaries for
Python 3.13. Mypyc now also supports Python 3.13.
In particular, these features are supported:
Various new stdlib features and changes (through typeshed stub improvements)
Type parameter defaults when using Python 3.12 type parameter syntax
Mypyc Support for Python 3.13
Mypyc now supports Python 3.13. This was contributed by Marc Mueller, with additional
fixes by Jukka Lehtosalo. Free threaded Python 3.13 builds are not supported yet.
List of changes:
Add additional includes for Python 3.13 (Marc Mueller, PR 17506)
Add another include for Python 3.13 (Marc Mueller, PR 17509)
Fix ManagedDict functions for Python 3.13 (Marc Mueller, PR 17507)
Update mypyc test output for Python 3.13 (Marc Mueller, PR 17508)
Fix PyUnicode functions for Python 3.13 (Marc Mueller, PR 17504)
Fix _PyObject_LookupAttrId for Python 3.13 (Marc Mueller, PR 17505)
Fix _PyList_Extend for Python 3.13 (Marc Mueller, PR 17503)
Fix gen_is_coroutine for Python 3.13 (Marc Mueller, PR 17501)
Fix _PyObject_FastCall for Python 3.13 (Marc Mueller, PR 17502)
Avoid uses of _PyObject_CallMethodOneArg on 3.13 (Jukka Lehtosalo, PR 17526)
Don't rely on _PyType_CalculateMetaclass on 3.13 (Jukka Lehtosalo, PR 17525)
Don't use _PyUnicode_FastCopyCharacters on 3.13 (Jukka Lehtosalo, PR 17524)
Don't use _PyUnicode_EQ on 3.13, as it's no longer exported (Jukka Lehtosalo, PR 17523)
Inferring Unions for Conditional Expressions
Mypy now always tries to infer a union type for a conditional expression if left and right
operand types are different. This results in more precise inferred types and lets mypy detect
more issues. Example:
s = "foo" if cond() else 1
# Type of "s" is now "str | int" (it used to be "object")
Notably, if one of the operands has type Any, the type of a conditional expression is
now <type> | Any. Previously the inferred type was just Any. The new type essentially
indicates that the value can be of type <type>, and potentially of some (unknown) type.
Most operations performed on the result must also be valid for <type>.
Example where this is relevant:
from typing import Any
def func(a: Any, b: bool) -> None:
x = a if b else None
# Type of x is "Any | None"
print(x.y) # Error: None has no attribute "y"
This feature was contributed by Ivan Levkivskyi (PR 17427).
ReadOnly Support for TypedDict (PEP 705)
You can now use typing.ReadOnly to specity TypedDict items as
read-only (PEP 705):
from typing import TypedDict
# Or "from typing ..." on Python 3.13
from typing_extensions import ReadOnly
class TD(TypedDict):
a: int
b: ReadOnly[int]
d: TD = {"a": 1, "b": 2}
d["a"] = 3 # OK
d["b"] = 5 # Error: "b" is ReadOnly
This feature was contributed by Nikita Sobolev (PR 17644).
Python 3.8 End of Life Approaching
We are planning to drop support for Python 3.8 in the next mypy feature release or the
one after that. Python 3.8 reaches end of life in October 2024.
Planned Changes to Defaults
We are planning to enable --local-partial-types by default in mypy 2.0. This will
often require at least minor code changes. This option is implicitly enabled by mypy
daemon, so this makes the behavior of daemon and non-daemon modes consistent.
We recommend that mypy users start using local partial types soon (or to explicitly disable
them) to prepare for the change.
This can also be configured in a mypy configuration file:
Mypy documentation now uses modern syntax variants and imports in many examples. Some
examples no longer work on Python 3.8, which is the earliest Python version that mypy supports.
Notably, Iterable and other protocols/ABCs are imported from collections.abc instead of
typing:
from collections.abc import Iterable, Callable
Examples also avoid the upper-case aliases to built-in types: list[str] is used instead
of List[str]. The X | Y union type syntax introduced in Python 3.10 is also now prevalent.