Skip to content

Commit 106e194

Browse files
Merge branch 'master' into sentinels-reloaded
2 parents aa22da4 + 2433566 commit 106e194

500 files changed

Lines changed: 14891 additions & 7079 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CHANGELOG.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,86 @@
22

33
## Next Release
44

5+
## Mypy 2.1
6+
7+
We’ve just uploaded mypy 2.1.0 to the Python Package Index ([PyPI](https://pypi.org/project/mypy/)).
8+
Mypy is a static type checker for Python. This release includes new features, performance
9+
improvements and bug fixes. You can install it as follows:
10+
11+
python3 -m pip install -U mypy
12+
13+
You can read the full documentation for this release on [Read the Docs](http://mypy.readthedocs.io).
14+
15+
### librt.vecs: Fast Growable Array Type for Mypyc
16+
17+
The new `librt.vecs` module provides an efficient growable array type `vec` that is
18+
optimized for mypyc use. It provides fast, packed arrays with integer and floating point
19+
value types, which can be **several times faster** than `list`, and tens of times faster
20+
than `array.array` in code compiled using mypyc. It also supports nested `vec` objects and
21+
non-value-type items, such as ``vec[vec[str]]``.
22+
23+
Refer to the [documentation](https://mypyc.readthedocs.io/en/latest/librt_vecs.html) for
24+
the details.
25+
26+
Contributed by Jukka Lehtosalo.
27+
28+
### librt.random: Fast Pseudo-Random Number Generation
29+
30+
The new `librt.random` module provides fast pseudo-random number generation that is
31+
optimized for code compiled using mypyc. It can be 3x to 10x faster than the stdlib
32+
`random` module in compiled code.
33+
34+
Refer to the [documentation](https://mypyc.readthedocs.io/en/latest/librt_random.html) for
35+
the details.
36+
37+
Contributed by Jukka Lehtosalo (PR [21433](https://github.com/python/mypy/pull/21433)).
38+
39+
### Mypyc Improvements
40+
41+
- Enable incremental self-compilation (Vaggelis Danias, PR [21369](https://github.com/python/mypy/pull/21369))
42+
- Make compilation order with multiple files consistent (Piotr Sawicki, PR [21419](https://github.com/python/mypy/pull/21419))
43+
- Fix crash on accessing `StopAsyncIteration` (Piotr Sawicki, PR [21406](https://github.com/python/mypy/pull/21406))
44+
- Fix incremental compilation with `separate` flag (Vaggelis Danias, PR [21299](https://github.com/python/mypy/pull/21299))
45+
46+
### Fixes to Crashes
47+
48+
- Fix crash on partial type with `--allow-redefinition` and `global` declaration (Jukka Lehtosalo, PR [21428](https://github.com/python/mypy/pull/21428))
49+
- Fix broken awaitable generator patching (Ivan Levkivskyi, PR [21435](https://github.com/python/mypy/pull/21435))
50+
51+
### Changes to Messages
52+
53+
- Fix function call error message for small number of arguments (sobolevn, PR [21432](https://github.com/python/mypy/pull/21432))
54+
55+
### Other Notable Fixes and Improvements
56+
57+
- Rely on typeshed stubs for `slice` typing (Ivan Levkivskyi, PR [21401](https://github.com/python/mypy/pull/21401))
58+
- Narrow match captures based on previous cases (Shantanu, PR [21405](https://github.com/python/mypy/pull/21405))
59+
- Fix nondeterminism in overload resolution (Shantanu, PR [21455](https://github.com/python/mypy/pull/21455))
60+
- Respect file config comments for stale modules (Adam Turner, PR [21444](https://github.com/python/mypy/pull/21444))
61+
- Fix JSON output mode for syntax errors in parallel mode (Adam Turner, PR [21434](https://github.com/python/mypy/pull/21434))
62+
- Fix type variable with values as a supertype (Ivan Levkivskyi, PR [21431](https://github.com/python/mypy/pull/21431))
63+
- Add support for configuring `--num-workers` with an environment variable (Kevin Kannammalil, PR [21407](https://github.com/python/mypy/pull/21407))
64+
- Respect JSON output mode for syntax errors (Adam Turner, PR [21386](https://github.com/python/mypy/pull/21386))
65+
66+
### Typeshed Updates
67+
68+
Please see [git log](https://github.com/python/typeshed/commits/main?after=e4d32e01bee44241a5e7c33298c261175b9f1bdb+0&branch=main&path=stdlib) for full list of standard library typeshed stub changes.
69+
70+
### Acknowledgements
71+
72+
Thanks to all mypy contributors who contributed to this release:
73+
74+
- Adam Turner
75+
- Ivan Levkivskyi
76+
- Jukka Lehtosalo
77+
- Kevin Kannammalil
78+
- Piotr Sawicki
79+
- Shantanu
80+
- sobolevn
81+
- Vaggelis Danias
82+
83+
I’d also like to thank my employer, Dropbox, for supporting mypy development.
84+
585
## Mypy 2.0
686

787
We’ve just uploaded mypy 2.0.0 to the Python Package Index ([PyPI](https://pypi.org/project/mypy/)).

CONTRIBUTING.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,18 @@ tox -e dev --override testenv:dev.allowlist_externals+=env -- env # inspect the
123123
If you don't already have `tox` installed, you can use a virtual environment as
124124
described above to install `tox` via `pip` (e.g., ``python -m pip install tox``).
125125

126+
## LLM-assisted contributions
127+
128+
In general, mypy takes a neutral stance on using various LLM code assistants to
129+
make contributions. LLMs are just another tool, and contributors bear full
130+
responsibility for the code they are submitting. Disclosing the use of LLMs in
131+
pull request description is recommended, but not required.
132+
133+
However, we discourage use of LLMs by *new* contributors. We are interested in
134+
growing long-term contributors who have good understanding of mypy code.
135+
Pull requests from new contributors that are mostly generated by LLMs
136+
with little human input will be closed.
137+
126138
## First time contributors
127139

128140
If you're looking for things to help with, browse our [issue tracker](https://github.com/python/mypy/issues)!

docs/source/error_code_list2.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ Example:
550550
Check that overrides of mutable attributes are safe [mutable-override]
551551
----------------------------------------------------------------------
552552

553-
`mutable-override` will enable the check for unsafe overrides of mutable attributes.
553+
``mutable-override`` will enable the check for unsafe overrides of mutable attributes.
554554
For historical reasons, and because this is a relatively common pattern in Python,
555555
this check is not enabled by default. The example below is unsafe, and will be
556556
flagged when this error code is enabled:

docs/source/kinds_of_types.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ case you should add an explicit ``... | None`` annotation.
443443
The Python interpreter internally uses the name ``NoneType`` for
444444
the type of ``None``, but ``None`` is always used in type
445445
annotations. The latter is shorter and reads better. (``NoneType``
446-
is available as :py:data:`types.NoneType` on Python 3.10+, but is
446+
is available as :py:class:`types.NoneType` on Python 3.10+, but is
447447
not exposed at all on earlier versions of Python.)
448448

449449
.. note::

docs/source/more_types.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ implementation, then the body is not type checked. If you want to
573573
force mypy to check the body anyways, use the :option:`--check-untyped-defs <mypy --check-untyped-defs>`
574574
flag (:ref:`more details here <untyped-definitions-and-calls>`).
575575

576-
The variants must also also be compatible with the implementation
576+
The variants must also be compatible with the implementation
577577
type hints. In the ``MyList`` example, mypy will check that the
578578
parameter type ``int`` and the return type ``T`` are compatible with
579579
``int | slice`` and ``T | Sequence`` for the

docs/source/typed_dict.rst

Lines changed: 114 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ arbitrarily complex types. For example, you can define nested
8181
``TypedDict``\s and containers with ``TypedDict`` items.
8282
Unlike most other types, mypy uses structural compatibility checking
8383
(or structural subtyping) with ``TypedDict``\s. A ``TypedDict`` object with
84-
extra items is compatible with (a subtype of) a narrower
84+
extra items can be compatible with (a subtype of) a narrower
8585
``TypedDict``, assuming item types are compatible (*totality* also affects
86-
subtyping, as discussed below).
86+
subtyping, as does *closing*, as discussed below).
8787

8888
A ``TypedDict`` object is not a subtype of the regular ``dict[...]``
8989
type (and vice versa), since :py:class:`dict` allows arbitrary keys to be
@@ -276,6 +276,84 @@ vary :ref:`covariantly <variance-of-generics>`:
276276
m: Movie = {"name": "Jaws", "year": 1975}
277277
process_entry(m) # OK
278278
279+
You can override a read-only item with a compatible subtype, make a
280+
read-only item mutable, and inherit from multiple parents with compatible
281+
definitions:
282+
283+
.. code-block:: python
284+
285+
from collections.abc import Collection, Sequence
286+
287+
class Competition(TypedDict):
288+
hosts: ReadOnly[Collection[str]]
289+
entries: ReadOnly[Sequence[Entry]]
290+
291+
class MovieShow(TypedDict):
292+
entries: list[Movie]
293+
294+
class Oscars(Competition, MovieShow):
295+
hosts: set[str]
296+
297+
Defining ``hosts`` as a mutable ``set[str]`` item works as this is compatible
298+
with the read-only ``Collection[str]`` definition in ``Competition``.
299+
``entries`` will be of type ``list[Movie]``, taken from the ``MovieShow`` type,
300+
as it is the only non-readonly definition, and is compatible with the definition
301+
in ``Competition``.
302+
303+
If an item is only defined in supertypes, and is always read-only, mypy takes
304+
the definition from the first parent in the inheritance order, and raises an
305+
error if any other parent definition is incompatible:
306+
307+
.. code-block:: python
308+
309+
class NameIds(TypedDict):
310+
ids: ReadOnly[Collection[str]]
311+
312+
class OrderedIds(TypedDict):
313+
ids: ReadOnly[Sequence[int | str]]
314+
315+
class OrderedNameIds(NameIds, OrderedIds):
316+
pass # Error! Parent definitions incompatible
317+
318+
In this example, the definition of ``ids`` will be taken from ``NameIds``,
319+
which would not be compatible with the definition in ``OrderedIds``; reordering
320+
the parents would not solve the problem. Instead, you will need to make a
321+
compatible definition explicitly:
322+
323+
.. code-block:: python
324+
325+
class OrderedNameIds(NameIds, OrderedIds):
326+
ids: ReadOnly[Sequence[str]]
327+
328+
Closing
329+
-------
330+
331+
You can use the ``closed`` keyword, introduced to ``TypedDict`` in Python
332+
3.15 (and available via ``typing_extensions.TypedDict`` in older
333+
versions) to prevent structural subtypes from adding extra keys to a
334+
type (:pep:`728`):
335+
336+
.. code-block:: python
337+
338+
HasName = TypedDict("HasName", {"name": str})
339+
HasOnlyName = TypedDict("HasOnlyName", {"name": str}, closed=True)
340+
Movie = TypedDict("Movie", {"name": str, "year": int})
341+
342+
movie: Movie = {"name": "Nimona", "year": 2023}
343+
has_name: HasName = movie # OK: type is open
344+
has_only_name: HasOnlyName = movie # Error: type is closed
345+
346+
This allows the typechecker to determine that certain operations are safe,
347+
when they otherwise wouldn't be due to the potential presence of unknown
348+
keys.
349+
350+
The ``closed`` keyword can also be used in class-based syntax:
351+
352+
.. code-block:: python
353+
354+
class HasOnlyName(TypedDict, closed=True):
355+
name: str
356+
279357
Unions of TypedDicts
280358
--------------------
281359

@@ -289,6 +367,40 @@ need to give each TypedDict the same key where each value has a unique
289367
:ref:`Literal type <literal_types>`. Then, check that key to distinguish
290368
between your TypedDicts.
291369

370+
Alternatively, you can implement tagged unions with single-key wrapper dictionaries:
371+
372+
.. code-block:: python
373+
374+
class Book(TypedDict):
375+
name: str
376+
length: int
377+
...
378+
379+
class DVD(TypedDict):
380+
name: str
381+
length: int
382+
...
383+
384+
TaggedBook = TypedDict('TaggedBook', {'book': Book}, closed=True)
385+
TaggedDVD = TypedDict('TaggedDVD', {'dvd': DVD}, closed=True)
386+
type Inventory = TaggedBook | TaggedDVD
387+
388+
def print_length(inventory: Inventory) -> None:
389+
if "book" in inventory:
390+
print(inventory["book"]["length"], 'pages')
391+
else:
392+
print(inventory["dvd"]["length"], 'minutes')
393+
394+
Here, the ``closed`` keyword is necessary to allow the ``if`` guard to safely
395+
narrow the types; without it, there could be a structural subtype of ``TaggedDVD``
396+
that contains a ``book`` field of arbitrary type.
397+
398+
.. note::
399+
400+
Applying ``@final`` to a TypedDict is a legacy way of marking it as closed
401+
for the purposes of type narrowing. It was never fully implemented and is
402+
now superseded; it may be removed in future.
403+
292404
Inline TypedDict types
293405
----------------------
294406

misc/dump-ast.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def dump(fname: str, python_version: tuple[int, int], quiet: bool = False) -> No
1919
options.python_version = python_version
2020
with open(fname, "rb") as f:
2121
s = f.read()
22-
tree = parse(s, fname, None, errors=Errors(options), options=options, file_exists=True)
22+
tree = parse(s, fname, None, errors=Errors(options), options=options)
2323
if not quiet:
2424
print(tree)
2525

Lines changed: 73 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,86 @@
1-
From 5c922a2484f2e18c7f901e62bb499b6414cf1090 Mon Sep 17 00:00:00 2001
1+
From 8985351bf6f917db1a7b15ffed95ec2357d9819d Mon Sep 17 00:00:00 2001
22
From: Marc Mueller <30130371+cdce8p@users.noreply.github.com>
3-
Date: Wed, 18 Feb 2026 13:11:02 +0100
3+
Date: Mon, 18 May 2026 17:33:09 +0200
44
Subject: [PATCH] Adjust stubs to fix mypy lookup error due to circular
55
dependencies in stubs
66

77
---
8-
mypy/typeshed/stdlib/time.pyi | 6 +++---
9-
1 file changed, 3 insertions(+), 3 deletions(-)
8+
mypy/typeshed/stdlib/__future__.pyi | 2 +-
9+
mypy/typeshed/stdlib/dis.pyi | 4 ++--
10+
mypy/typeshed/stdlib/inspect.pyi | 4 ++--
11+
mypy/typeshed/stdlib/itertools.pyi | 2 +-
12+
mypy/typeshed/stdlib/time.pyi | 6 +++---
13+
5 files changed, 9 insertions(+), 9 deletions(-)
1014

15+
diff --git a/mypy/typeshed/stdlib/__future__.pyi b/mypy/typeshed/stdlib/__future__.pyi
16+
index aa445d22b..a90cf1edd 100644
17+
--- a/mypy/typeshed/stdlib/__future__.pyi
18+
+++ b/mypy/typeshed/stdlib/__future__.pyi
19+
@@ -1,4 +1,4 @@
20+
-from typing import TypeAlias
21+
+from typing_extensions import TypeAlias
22+
23+
_VersionInfo: TypeAlias = tuple[int, int, int, str, int]
24+
25+
diff --git a/mypy/typeshed/stdlib/dis.pyi b/mypy/typeshed/stdlib/dis.pyi
26+
index 984f932e3..0ad928934 100644
27+
--- a/mypy/typeshed/stdlib/dis.pyi
28+
+++ b/mypy/typeshed/stdlib/dis.pyi
29+
@@ -2,7 +2,7 @@ import sys
30+
import types
31+
from collections.abc import Callable, Iterator
32+
from opcode import * # `dis` re-exports it as a part of public API
33+
-from typing import IO, Any, Final, NamedTuple, TypeAlias, overload
34+
+from typing import IO, Any, Final, NamedTuple, overload
35+
from typing_extensions import Self, deprecated, disjoint_base
36+
37+
__all__ = [
38+
@@ -41,7 +41,7 @@ else:
39+
40+
# Strictly this should not have to include Callable, but mypy doesn't use FunctionType
41+
# for functions (python/mypy#3171)
42+
-_HaveCodeType: TypeAlias = types.MethodType | types.FunctionType | types.CodeType | type | Callable[..., Any]
43+
+_HaveCodeType = types.MethodType | types.FunctionType | types.CodeType | type | Callable[..., Any]
44+
45+
if sys.version_info >= (3, 11):
46+
class Positions(NamedTuple):
47+
diff --git a/mypy/typeshed/stdlib/inspect.pyi b/mypy/typeshed/stdlib/inspect.pyi
48+
index cc39a7c9f..0ca4cff75 100644
49+
--- a/mypy/typeshed/stdlib/inspect.pyi
50+
+++ b/mypy/typeshed/stdlib/inspect.pyi
51+
@@ -196,8 +196,8 @@ if sys.version_info >= (3, 14):
52+
53+
modulesbyfile: dict[str, Any]
54+
55+
-_GetMembersPredicateTypeGuard: TypeAlias = Callable[[Any], TypeGuard[_T]]
56+
-_GetMembersPredicateTypeIs: TypeAlias = Callable[[Any], TypeIs[_T]]
57+
+_GetMembersPredicateTypeGuard = Callable[[Any], TypeGuard[_T]]
58+
+_GetMembersPredicateTypeIs = Callable[[Any], TypeIs[_T]]
59+
_GetMembersPredicate: TypeAlias = Callable[[Any], bool]
60+
_GetMembersReturn: TypeAlias = list[tuple[str, _T]]
61+
62+
diff --git a/mypy/typeshed/stdlib/itertools.pyi b/mypy/typeshed/stdlib/itertools.pyi
63+
index d26a4e1da..60e79bc2e 100644
64+
--- a/mypy/typeshed/stdlib/itertools.pyi
65+
+++ b/mypy/typeshed/stdlib/itertools.pyi
66+
@@ -23,7 +23,7 @@ _T10 = TypeVar("_T10")
67+
68+
_Step: TypeAlias = SupportsFloat | SupportsInt | SupportsIndex | SupportsComplex
69+
70+
-_Predicate: TypeAlias = Callable[[_T], object]
71+
+_Predicate = Callable[[_T], object]
72+
73+
# Technically count can take anything that implements a number protocol and has an add method
74+
# but we can't enforce the add method
1175
diff --git a/mypy/typeshed/stdlib/time.pyi b/mypy/typeshed/stdlib/time.pyi
12-
index 64a009318..d0853792b 100644
76+
index ac53089b8..9b5344b32 100644
1377
--- a/mypy/typeshed/stdlib/time.pyi
1478
+++ b/mypy/typeshed/stdlib/time.pyi
15-
@@ -1,16 +1,16 @@
79+
@@ -1,15 +1,15 @@
1680
import sys
1781
from _typeshed import structseq
18-
-from typing import Any, Final, Literal, Protocol, SupportsFloat, SupportsIndex, final, type_check_only
19-
+from typing import Any, Final, Literal, Protocol, SupportsFloat, SupportsIndex, Union, final, type_check_only
20-
from typing_extensions import TypeAlias
82+
-from typing import Any, Final, Literal, Protocol, SupportsFloat, SupportsIndex, TypeAlias, final, type_check_only
83+
+from typing import Any, Final, Literal, Protocol, SupportsFloat, SupportsIndex, TypeAlias, Union, final, type_check_only
2184

2285
_TimeTuple: TypeAlias = tuple[int, int, int, int, int, int, int, int, int]
2386

@@ -33,5 +96,5 @@ index 64a009318..d0853792b 100644
3396
altzone: int
3497
daylight: int
3598
--
36-
2.53.0
99+
2.54.0
37100

0 commit comments

Comments
 (0)