Skip to content

Commit 5c45130

Browse files
authored
Various mypy 2.4 changelog updates (#22077)
Primarily add overviews to beginnings of various sections.
1 parent 051f2c9 commit 5c45130

1 file changed

Lines changed: 128 additions & 4 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 128 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,69 @@ improvements and bug fixes. You can install it as follows:
1212

1313
You can read the full documentation for this release on [Read the Docs](http://mypy.readthedocs.io).
1414

15-
### Python 3.15 Support (TODO)
15+
### Python 3.15 Support
1616

17-
Mypy 2.4 supports running on Python 3.15 and type checking most Python 3.15 features.
17+
Mypy 2.4 supports running on Python 3.15 and type checking most Python
18+
3.15 features. This includes the new builtin `sentinel` type for
19+
sentinel values (PEP 661), which mypy now supports (see below for
20+
details). Lazy imports (PEP 810) and unpacking in comprehensions
21+
(PEP 798) are also supported. Closed TypedDicts (PEP 728) have been
22+
supported since mypy 2.2, but the `extra_items` TypedDict argument,
23+
also introduced in PEP 728, is still unsupported. Support for
24+
`extra_items` will be added in a future mypy release.
1825

1926
### Native Parser Enabled by Default
2027

28+
Mypy now uses the new native parser by default. It's based on the Ruff
29+
parser, and it's significantly faster than the legacy parser, which
30+
uses the stdlib `ast` module. The native parser also has other
31+
benefits:
32+
33+
- You can target newer Python versions and use recent Python syntax
34+
even when running mypy on an older Python version. For example, you
35+
can use `--python-version 3.15` when running mypy on Python 3.14.
36+
- Stub files can use syntax that is newer than the target Python
37+
version. For example, stubs can use the PEP 695 generic class syntax
38+
(`class Box[T]: ...`) when running on or targeting Python 3.10.
39+
- Parallel type checking requires the native parser.
40+
41+
The legacy parser is still available through `--no-native-parser`, or
42+
`native_parser = False` in the config file (`native_parser = false`
43+
under `[tool.mypy]` in pyproject.toml). We are planning to remove the
44+
legacy parser in early 2027. If you run into a problem with the native
45+
parser, please report it on the
46+
[issue tracker](https://github.com/python/mypy/issues).
47+
48+
Unlike the legacy parser, the native parser doesn't support type
49+
comments for variables defined by `for` and `with` statements. These
50+
type comments are silently ignored, and the types of the variables are
51+
inferred instead. This is a very rarely used feature that predates
52+
variable annotations that were introduced in Python 3.6. Other forms
53+
of type comments are still supported. Examples of type comments that
54+
are now ignored:
55+
56+
```python
57+
for i in x: # type: int
58+
...
59+
60+
with foo() as a: # type: Foo
61+
...
62+
```
63+
64+
You can use variable annotations before the statements instead:
65+
66+
```python
67+
i: int
68+
for i in x:
69+
...
70+
71+
a: Foo
72+
with foo() as a:
73+
...
74+
```
75+
76+
Related changes:
77+
2178
- Make the native parser the default (Ivan Levkivskyi, PR [21823](https://github.com/python/mypy/pull/21823))
2279
- Fix native parser crash caused by invalid assignment expression (Jukka Lehtosalo, PR [21882](https://github.com/python/mypy/pull/21882))
2380
- Match native parser behavior for positional-only arguments (Ivan Levkivskyi, PR [21891](https://github.com/python/mypy/pull/21891))
@@ -26,9 +83,27 @@ Mypy 2.4 supports running on Python 3.15 and type checking most Python 3.15 feat
2683
- Fall back to sequential parsing when threads are unavailable (Jukka Lehtosalo, PR [21993](https://github.com/python/mypy/pull/21993))
2784
- Various improvements to `ast-serialize` (Ivan Levkivskyi)
2885

29-
### Stable Parallel Checking
86+
### Parallel Checking No Longer Experimental
87+
88+
Parallel type checking is no longer experimental, and it will be
89+
enabled by default in a future mypy release. Use `-n8` or
90+
`--num-workers 8` to manually specify the number of workers.
3091

31-
Parallel type checking is no longer experimental.
92+
Mypy now also supports automatic determination of worker count using
93+
`-n auto`, which selects the number of workers based on the CPU cores
94+
available to mypy. The auto mode is capped at 8, since very large
95+
numbers of workers can use a lot of memory if type checking large
96+
projects, but you can manually specify higher numbers.
97+
98+
You can also specify these in mypy.ini (e.g. `num_workers = auto` under
99+
`[mypy]`) or pyproject.toml (e.g. `num_workers = "auto"` under `[tool.mypy]`).
100+
101+
We've seen up to a 5x speedup when using 8 workers.
102+
103+
Refer to the [documentation](https://mypy.readthedocs.io/en/stable/command_line.html#parallel)
104+
for more information.
105+
106+
Related changes:
32107

33108
- Use two-phase type checking in sequential mode (Ivan Levkivskyi, PR [21973](https://github.com/python/mypy/pull/21973))
34109
- Use a more thorough cleanup of parallel workers (Ivan Levkivskyi, PR [21981](https://github.com/python/mypy/pull/21981))
@@ -40,6 +115,26 @@ Parallel type checking is no longer experimental.
40115

41116
### Support for PEP 661 Sentinels
42117

118+
Mypy now supports sentinel values ([PEP 661](https://peps.python.org/pep-0661/)).
119+
Sentinels are useful for distinguishing a missing argument from valid values such
120+
as `None`. You can use a sentinel directly in a type annotation, and mypy narrows
121+
the type when you check its identity with `is` or `is not`.
122+
123+
```python
124+
# On Python 3.15, sentinel is a builtin and the import isn't needed
125+
from typing_extensions import sentinel
126+
127+
MISSING = sentinel("MISSING")
128+
129+
def process(value: int | None | MISSING = MISSING) -> None:
130+
if value is MISSING:
131+
print("No value")
132+
else:
133+
reveal_type(value) # int | None
134+
```
135+
136+
Related changes:
137+
43138
- Add support for PEP 661 sentinels (Edgar Ramírez Mondragón, PR [21647](https://github.com/python/mypy/pull/21647))
44139
- Improve sentinel narrowing (Marc Mueller, PR [21844](https://github.com/python/mypy/pull/21844))
45140
- Fix sentinel identity loss through generic substitution and inference (Edgar Ramírez Mondragón, PR [21888](https://github.com/python/mypy/pull/21888))
@@ -49,12 +144,41 @@ Parallel type checking is no longer experimental.
49144

50145
### More Lenient Handling of `*tuple[Any, ...]`
51146

147+
Mypy now treats `*tuple[Any, ...]` more leniently in tuple types and
148+
variadic generic types. The unpacked portion can match any number of
149+
items when checking compatibility, while explicitly specified items
150+
still need to match. Variadic tuples with a non-`Any` item type, such
151+
as `*tuple[int, ...]`, are still handled strictly. This also improves
152+
type inference and support for indexing, slicing, and unpacking these
153+
tuples.
154+
155+
```python
156+
from typing import Any
157+
158+
def process(values: tuple[int, str]) -> None:
159+
...
160+
161+
def example(values: tuple[int, *tuple[Any, ...]]) -> None:
162+
process(values) # Now accepted
163+
first, second = values # Now accepted
164+
reveal_type(first) # int
165+
reveal_type(second) # Any
166+
```
167+
168+
Related changes:
169+
52170
- Lenient handling of `*tuple[Any, ...]` (part 1) (Ivan Levkivskyi, PR [22001](https://github.com/python/mypy/pull/22001))
53171
- Lenient handling of `*tuple[Any, ...]` (part 2) (Ivan Levkivskyi, PR [22006](https://github.com/python/mypy/pull/22006))
54172
- Lenient handling of `*tuple[Any, ...]` (part 3) (Ivan Levkivskyi, PR [22014](https://github.com/python/mypy/pull/22014))
55173

56174
### Mypyc Generator and Coroutine Improvements
57175

176+
This release includes many improvements to the performance of generators and
177+
async functions. The improvements are particularly significant on free-threaded
178+
Python builds (over 2x faster in microbenchmarks compared to mypy 2.3).
179+
180+
Related changes:
181+
58182
- Keep generator local state in C locals when possible (Jukka Lehtosalo, PR [21982](https://github.com/python/mypy/pull/21982))
59183
- Ensure a generator can't be entered while it's being executed (Jukka Lehtosalo, PR [21939](https://github.com/python/mypy/pull/21939))
60184
- Use the generator helper fast path on resume (Jukka Lehtosalo, PR [21954](https://github.com/python/mypy/pull/21954))

0 commit comments

Comments
 (0)