Skip to content

Commit ca58e37

Browse files
authored
Merge branch 'master' into fix-issue-21271
2 parents 124282a + 16823a1 commit ca58e37

37 files changed

Lines changed: 632 additions & 174 deletions

CHANGELOG.md

Lines changed: 217 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,252 @@
22

33
## Next Release
44

5-
### Enabling `--local-partial-types` by default
5+
## Mypy 2.0
6+
7+
We’ve just uploaded mypy 2.0.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. There are also changes to options and defaults.
10+
You can install it as follows:
11+
12+
python3 -m pip install -U mypy
13+
14+
You can read the full documentation for this release on [Read the Docs](http://mypy.readthedocs.io).
15+
16+
### Enable `--local-partial-types` by Default
617

718
This flag affects the inference of types based on assignments in other scopes.
819
For now, explicitly disabling this continues to be supported, but this support will be removed
920
in the future as the legacy behaviour is hard to support with other current and future features
1021
in mypy, like the daemon or the new implementation of flexible redefinitions.
1122

12-
Contributed by Ivan Levkivskyi, Jukka Lehtosalo, Shantanu in [PR 21163](https://github.com/python/mypy/pull/21163)
23+
Contributed by Ivan Levkivskyi, Jukka Lehtosalo, Shantanu in [PR 21163](https://github.com/python/mypy/pull/21163).
1324

14-
### Enabling `--strict-bytes` by default
25+
### Enable `--strict-bytes` by Default
1526

1627
Per [PEP 688](https://peps.python.org/pep-0688), mypy no longer treats `bytearray` and `memoryview`
1728
values as assignable to the `bytes` type.
1829

19-
Contributed by Shantanu in [PR 18371](https://github.com/python/mypy/pull/18371)
30+
Contributed by Shantanu in [PR 18371](https://github.com/python/mypy/pull/18371).
31+
32+
### New Behavior for `--allow-redefinition`
33+
34+
The `--allow-redefinition` flag now behaves like `--allow-redefinition-new` in mypy 1.20
35+
and earlier. The new behavior is generally more flexible. For example, you can have different
36+
types for a variable in different blocks:
37+
38+
```python
39+
# mypy: allow-redefinition
40+
41+
def foo(cond: bool) -> None:
42+
if cond:
43+
for x in ["a", "b"]:
44+
# Type of "x" is "str" here
45+
...
46+
else:
47+
for x in [1, 2]:
48+
# Type of "x" is "int" here
49+
...
50+
```
51+
52+
The new behavior requires `--local-partial-types`, which is now enabled by default.
53+
54+
However, `--allow-redefinition` doesn't allow giving two type annotations for the same
55+
variable. The old behavior (sometimes) allows this. Code like this now generates an error
56+
when using `--allow-redefinition`:
57+
58+
```python
59+
def foo() -> None:
60+
x: list[int] = []
61+
...
62+
x: list[str] = [] # Error: "x" redefined
63+
...
64+
```
65+
66+
You can still use `--allow-redefinition-old` to fall back to the old behavior. We have no
67+
plans to remove the legacy behavior, but the old functionality is maintained on a best effort
68+
basis.
69+
70+
Contributed by Jukka Lehtosalo in [PR 21276](https://github.com/python/mypy/pull/21276).
71+
72+
### Parallel Type Checking
73+
74+
Mypy now supports experimental parallel and incremental type checking. Use `--num-workers N`
75+
or `-nN` to use `N` worker processes to type check in parallel. The speedup depends on the
76+
import structure of your codebase and your environment, but for large projects we've seen
77+
performance gains of **up to 5x** when using 8 worker processes.
78+
79+
Parallel type checking implicitly enables the new native parser. There are still some
80+
minor semantic differences between parallel and non-parallel modes, which we will be fixing
81+
in future mypy releases.
82+
83+
Contributed by Ivan Levkivskyi, with additional contributions from Emma Smith and Jukka
84+
Lehtosalo.
85+
86+
Recent related changes since the last release:
87+
88+
- Freeze garbage collection in parallel workers for 4-5% speedup (Ivan Levkivskyi, PR [21302](https://github.com/python/mypy/pull/21302))
89+
- Expose `--num-workers` and `--native-parser` (Ivan Levkivskyi, PR [21387](https://github.com/python/mypy/pull/21387))
90+
- Split type checking into interface and implementation in parallel workers (Ivan Levkivskyi, PR [21119](https://github.com/python/mypy/pull/21119))
91+
- Batch module groups for parallel processing (Ivan Levkivskyi, PR [21287](https://github.com/python/mypy/pull/21287))
92+
- Optimize parallel worker startup (Ivan Levkivskyi, PR [21203](https://github.com/python/mypy/pull/21203))
93+
- Parse files in parallel when possible (Ivan Levkivskyi, PR [21175](https://github.com/python/mypy/pull/21175))
94+
- Use parallel parsing at all stages (Ivan Levkivskyi, PR [21266](https://github.com/python/mypy/pull/21266))
95+
- Fix sequential bottleneck in parallel parsing (Jukka Lehtosalo, PR [21291](https://github.com/python/mypy/pull/21291))
96+
- Fail fast when a user tries to generate reports with parallel workers (Ivan Levkivskyi, PR [21341](https://github.com/python/mypy/pull/21341))
97+
- Partially support old NumPy plugin in parallel type checking (Ivan Levkivskyi, PR [21324](https://github.com/python/mypy/pull/21324))
98+
- Handle reachability consistently in parallel type checking (Ivan Levkivskyi, PR [21322](https://github.com/python/mypy/pull/21322))
99+
- Always respect `@no_type_check` in parallel type checking (Ivan Levkivskyi, PR [21320](https://github.com/python/mypy/pull/21320))
100+
- Minor fixes in parallel checking (Ivan Levkivskyi, PR [21319](https://github.com/python/mypy/pull/21319))
101+
- Fix plugin logic in parallel type checking (Ivan Levkivskyi, PR [21252](https://github.com/python/mypy/pull/21252))
102+
- Fix Windows IPC race condition when using parallel checking (Jukka Lehtosalo, PR [21228](https://github.com/python/mypy/pull/21228))
103+
- Report parallel worker exit status on receive failure (Jukka Lehtosalo, PR [21224](https://github.com/python/mypy/pull/21224))
20104

21105
### Drop Support for Targeting Python 3.9
22106

23107
Mypy no longer supports type checking code with `--python-version 3.9`.
24108
Use `--python-version 3.10` or newer.
25109

26-
Contributed by Shantanu, Marc Mueller in [PR 21243](https://github.com/python/mypy/pull/21243)
110+
Contributed by Shantanu, Marc Mueller in [PR 21243](https://github.com/python/mypy/pull/21243).
27111

28-
### Remove special casing of legacy bundled stubs
112+
### Remove Special Casing of Legacy Bundled Stubs
29113

30114
Mypy used to bundle stubs for a few packages in versions 0.812 and earlier. To navigate the
31115
transition, mypy used to report missing types for these packages even if `--ignore-missing-imports`
32116
was set. Mypy now consistently respects `--ignore-missing-imports` for all packages.
33117

34-
Contributed by Shantanu in [PR 18372](https://github.com/python/mypy/pull/18372)
118+
Contributed by Shantanu in [PR 18372](https://github.com/python/mypy/pull/18372).
35119

36-
### Prevent assignment to None for non-Optional class variables with type comments
120+
### Prevent Assignment to None for Non-Optional Class Variables with Type Comments
37121

38122
Mypy used to allow assignment to None for class variables when using type comments. This was a
39123
common idiom in Python 3.5 and earlier, prior to the introduction of variable annotations.
40124
However, this was a soundness hole and has now been removed.
41125

42-
Contributed by Shantanu in [PR 20054](https://github.com/python/mypy/pull/20054)
126+
Contributed by Shantanu in [PR 20054](https://github.com/python/mypy/pull/20054).
127+
128+
### librt.strings: String and Bytes Primitives for Mypyc
129+
130+
In mypy 1.20, we introduced [librt](https://pypi.org/project/librt/) as a standard library
131+
for mypyc that fills in some gaps in the Python standard library and the C API.
132+
This release adds the new module `librt.strings`, which contains utilities for building
133+
string and bytes objects, and for accessing and generating binary data:
134+
135+
* `StringWriter` and `BytesWriter` classes allow quickly building `str` and `bytes` objects
136+
from parts.
137+
* `read_*` and `write_*` functions provide fast reading and writing of binary-encoded data.
138+
139+
Refer to the [documentation](https://mypyc.readthedocs.io/en/latest/librt_strings.html) for
140+
the details.
141+
142+
Contributed by Jukka Lehtosalo.
143+
144+
### Mypyc Improvements
145+
146+
- Document `librt.time` (Jukka Lehtosalo, PR [21372](https://github.com/python/mypy/pull/21372))
147+
- Mark `librt.time.time()` non-experimental (Ivan Levkivskyi, PR [21310](https://github.com/python/mypy/pull/21310))
148+
- Fix `librt.time` primitive now that it is no longer experimental (Ivan Levkivskyi, PR [21318](https://github.com/python/mypy/pull/21318))
149+
- Fix `librt` API/ABI version checks (Jukka Lehtosalo, PR [21311](https://github.com/python/mypy/pull/21311))
150+
- Generate more type methods for classes with attribute dictionaries (Piotr Sawicki, PR [21290](https://github.com/python/mypy/pull/21290))
151+
- Fix reference counting for tuple items during deallocation (Shantanu, PR [21245](https://github.com/python/mypy/pull/21245))
152+
- Release new instances when `__init__` raises (Shantanu, PR [21248](https://github.com/python/mypy/pull/21248))
153+
- Fix `@property` getter memory leak (Vaggelis Danias, PR [21230](https://github.com/python/mypy/pull/21230))
154+
- Fix semantics for walrus expression in tuple (Shantanu, PR [21249](https://github.com/python/mypy/pull/21249))
155+
- Fix crash on import errors during cleanup (Shantanu, PR [21247](https://github.com/python/mypy/pull/21247))
156+
- Fix reference leak in str index (Shantanu, PR [21251](https://github.com/python/mypy/pull/21251))
157+
- Fix memory leak in integer true division (Shantanu, PR [21246](https://github.com/python/mypy/pull/21246))
158+
- Fix reference leaks in `list.clear()`/`dict.clear()` (Shantanu, PR [21244](https://github.com/python/mypy/pull/21244))
159+
- Resolve type aliases in function specialization (esarp, PR [21233](https://github.com/python/mypy/pull/21233))
160+
- Report an error if an acyclic class inherits from non-acyclic (Piotr Sawicki, PR [21227](https://github.com/python/mypy/pull/21227))
161+
- Fix `b64decode` to match new CPython behavior (Piotr Sawicki, PR [21200](https://github.com/python/mypy/pull/21200))
162+
163+
### Fixes to Crashes
164+
165+
- Fix crash when a file does not exist during semantic analysis (Ivan Levkivskyi, PR [21379](https://github.com/python/mypy/pull/21379))
166+
- Fix parallel worker crash on syntax error (Ivan Levkivskyi, PR [21202](https://github.com/python/mypy/pull/21202))
167+
168+
### Changes to Messages
169+
170+
- Improve error messages for unexpected keyword arguments in overloaded functions (Kevin Kannammalil, PR [20592](https://github.com/python/mypy/pull/20592))
171+
- Don't suggest `Foo[...]` when `Foo(arg=...)` is used in annotation (Yosof Badr, PR [21238](https://github.com/python/mypy/pull/21238))
172+
- Mention what codes are actually ignored in "not covered by type: ignore comment" note (wyattscarpenter, PR [19904](https://github.com/python/mypy/pull/19904))
173+
- Improve error messages when positional argument is missing (Kevin Kannammalil, PR [20591](https://github.com/python/mypy/pull/20591))
174+
- Improve "name is not defined" errors with fuzzy matching (Kevin Kannammalil, PR [20693](https://github.com/python/mypy/pull/20693))
175+
- Add suggestions for misspelled module imports (Kevin Kannammalil, PR [20695](https://github.com/python/mypy/pull/20695))
176+
177+
### Performance Improvements
178+
179+
- Replace `NamedTuple` with faster regular classes in hot paths (Shantanu, PR [21326](https://github.com/python/mypy/pull/21326))
180+
- Avoid calling best-match suggestions unless the message is shown (Ivan Levkivskyi, PR [21307](https://github.com/python/mypy/pull/21307))
181+
- Order cases in native parser based on AST node frequency (Jukka Lehtosalo, PR [21219](https://github.com/python/mypy/pull/21219))
182+
183+
### Stubtest Improvements
184+
185+
- Basic support for unpack kwargs (Shantanu, PR [21024](https://github.com/python/mypy/pull/21024))
186+
- Fix false positive for properties with a deleter (Pranav Manglik, PR [21259](https://github.com/python/mypy/pull/21259))
187+
188+
### Documentation Updates
189+
190+
- Rename "value restriction" to "value-constrained type variable" (Leo Ji, PR [21112](https://github.com/python/mypy/pull/21112))
191+
- Clarify that invariant-by-default applies to legacy `TypeVar` syntax (Leo Ji, PR [21108](https://github.com/python/mypy/pull/21108))
192+
193+
### Improvements to the Native Parser
194+
195+
The new native parser is still experimental.
196+
197+
- Make new parser consistent with the old one (Ivan Levkivskyi, PR [21377](https://github.com/python/mypy/pull/21377))
198+
- Support `--package-root` with the native parser (Ivan Levkivskyi, PR [21321](https://github.com/python/mypy/pull/21321))
199+
- Improve call expressions in type annotations with the native parser (Jukka Lehtosalo, PR [21300](https://github.com/python/mypy/pull/21300))
200+
- Depend on `ast-serialize` by default (Jukka Lehtosalo, PR [21297](https://github.com/python/mypy/pull/21297))
201+
202+
### Other Notable Fixes and Improvements
203+
204+
- Fix narrowing for `AbstractSet` and `Mapping` (Shantanu, PR [21352](https://github.com/python/mypy/pull/21352))
205+
- Preserve gradual guarantee when narrowing `Any` union via equality (Shantanu, PR [21368](https://github.com/python/mypy/pull/21368))
206+
- Make type variable upper bound narrowing symmetric (Ivan Levkivskyi, PR [21350](https://github.com/python/mypy/pull/21350))
207+
- Behave consistently when type-checking a stub package directly (Ivan Levkivskyi, PR [21330](https://github.com/python/mypy/pull/21330))
208+
- Add support for `Final[...]` in dataclasses (Ivan Levkivskyi, PR [21334](https://github.com/python/mypy/pull/21334))
209+
- Narrow more sequence parents (Shantanu, PR [21327](https://github.com/python/mypy/pull/21327))
210+
- Better narrowing for enums and other types with known equality (Shantanu, PR [21281](https://github.com/python/mypy/pull/21281))
211+
- Fix pathspec error (Ivan Levkivskyi, PR [21296](https://github.com/python/mypy/pull/21296))
212+
- Use sharding for the SQLite cache (Jukka Lehtosalo, PR [21292](https://github.com/python/mypy/pull/21292))
213+
- Limit type inference context fallback to the walrus operator only (Ivan Levkivskyi, PR [21294](https://github.com/python/mypy/pull/21294))
214+
- Support `.git/info/exclude` for `--exclude-gitignore` (RogerJinIS, PR [21286](https://github.com/python/mypy/pull/21286))
215+
- Let `--allow-redefinition` widen a global in a function with `None` initialization (Jukka Lehtosalo, PR [21285](https://github.com/python/mypy/pull/21285))
216+
- Delete Python 2 extra (Shantanu, PR [18374](https://github.com/python/mypy/pull/18374))
217+
- No longer narrow final globals in functions (Ivan Levkivskyi, PR [21241](https://github.com/python/mypy/pull/21241))
218+
- Narrow unions containing `Any` in conditional branches (Shantanu, PR [21231](https://github.com/python/mypy/pull/21231))
219+
- Propagate narrowing within chained comparisons (Shantanu, PR [21160](https://github.com/python/mypy/pull/21160))
220+
- Add proper lazy deserialization (Ivan Levkivskyi, PR [21198](https://github.com/python/mypy/pull/21198))
221+
- Add `install_types` to options affecting cache (Brian Schubert, PR [21070](https://github.com/python/mypy/pull/21070))
222+
- Narrow `Any` in conditional type checks (Shantanu, PR [21167](https://github.com/python/mypy/pull/21167))
223+
- Fix exception handler target location in new parser (Ivan Levkivskyi, PR [21185](https://github.com/python/mypy/pull/21185))
224+
- Improve traceback display (Shantanu, PR [21155](https://github.com/python/mypy/pull/21155))
225+
- Include two more files in the sdist: `CREDITS` and the typeshed `README` (Michael R. Crusoe, PR [21131](https://github.com/python/mypy/pull/21131))
226+
227+
### Typeshed Updates
228+
229+
Please see [git log](https://github.com/python/typeshed/commits/main?after=c5e47faeda2cf9d233f91bc1dc95814b0cc7ccba+0&branch=main&path=stdlib) for full list of standard library typeshed stub changes.
230+
231+
### Acknowledgements
232+
233+
Thanks to all mypy contributors who contributed to this release:
234+
- Brian Schubert
235+
- Ethan Sarp
236+
- Ivan Levkivskyi
237+
- Jukka Lehtosalo
238+
- Kevin Kannammalil
239+
- Leo Ji
240+
- Marc Mueller
241+
- Michael R. Crusoe
242+
- Piotr Sawicki
243+
- Pranav Manglik
244+
- RogerJinIS
245+
- Shantanu
246+
- Vaggelis Danias
247+
- wyattscarpenter
248+
- Yosof Badr
249+
250+
I’d also like to thank my employer, Dropbox, for supporting mypy development.
43251

44252
## Mypy 1.20
45253

docs/source/command_line.rst

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,6 +1032,43 @@ beyond what incremental mode can offer, try running mypy in
10321032
Skip cache internal consistency checks based on mtime.
10331033

10341034

1035+
.. _parallel:
1036+
1037+
Parallel type-checking
1038+
**********************
1039+
1040+
By default, mypy checks all modules in the same Python process. This can be slow
1041+
for large code bases. Mypy offers experimental parallel type-checking mode using
1042+
multiple worker processes. In parallel mode, modules that do not depend om each
1043+
other are type-checked in parallel. :ref:`Incremental cache <incremental>` is
1044+
used to manage most of the shared state. Parallel type-checking also requires
1045+
:option:`--local-partial-types <mypy --no-local-partial-types>`, which is
1046+
enabled by default starting from mypy 2.0.
1047+
1048+
.. option:: -n NUMBER, --num-workers NUMBER
1049+
1050+
Use ``NUMBER`` parallel worker processes (in addition to the coordinator
1051+
process) to perform type-checking. Specifying ``--num-workers 0`` (default)
1052+
disables parallel checking. Automatic detection of the optimal number
1053+
of workers is not supported yet.
1054+
1055+
This setting will override the ``MYPY_NUM_WORKERS`` environment
1056+
variable if it is set.
1057+
1058+
Notes:
1059+
1060+
* An import cycle is always processed as a whole by a worker process. Thus,
1061+
avoiding large import cycles in your code will *significantly* improve
1062+
type-checking speed.
1063+
1064+
* Specifying a number of workers that is larger than the number of *physical*
1065+
CPU cores is not beneficial, since mypy is usually CPU bound. Best way to
1066+
tune the number of workers on a given machine is to start from 3-4 workers
1067+
and increase the number while you see a performance improvement.
1068+
1069+
* Parallel mode requires and automatically enables :option:`--native-parser`.
1070+
1071+
10351072
Advanced options
10361073
****************
10371074

@@ -1105,6 +1142,11 @@ in developing or debugging mypy internals.
11051142
cause mypy to type check the contents of ``temp.py`` instead of ``original.py``,
11061143
but error messages will still reference ``original.py``.
11071144

1145+
.. option:: --native-parser
1146+
1147+
This enables fast Rust-based parser that parses directly to mypy AST.
1148+
It will become the default parser in one of the next mypy releases.
1149+
11081150

11091151
Report generation
11101152
*****************
@@ -1169,7 +1211,7 @@ format into the specified directory.
11691211
Enabling incomplete/experimental features
11701212
*****************************************
11711213

1172-
.. option:: --enable-incomplete-feature {PreciseTupleTypes,InlineTypedDict,TypeForm}
1214+
.. option:: --enable-incomplete-feature {PreciseTupleTypes,InlineTypedDict}
11731215

11741216
Some features may require several mypy releases to implement, for example
11751217
due to their complexity, potential for backwards incompatibility, or
@@ -1224,8 +1266,6 @@ List of currently incomplete/experimental features:
12241266
def test_values() -> {"width": int, "description": str}:
12251267
return {"width": 42, "description": "test"}
12261268
1227-
* ``TypeForm``: this feature enables ``TypeForm``, as described in
1228-
`PEP 747 – Annotating Type Forms <https://peps.python.org/pep-0747/>_`.
12291269
12301270
12311271
Miscellaneous

docs/source/config_file.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,6 +1006,22 @@ These options may only be set in the global section (``[mypy]``).
10061006
Skip cache internal consistency checks based on mtime.
10071007

10081008

1009+
Parallel type-checking configuration
1010+
************************************
1011+
1012+
These options may only be set in the global section (``[mypy]``).
1013+
1014+
.. confval:: num_workers
1015+
1016+
:type: integer
1017+
:default: 0
1018+
1019+
Use specific number of parallel worker processes for type-checking, see
1020+
:ref:`parallel type-checking <parallel>` for more details.
1021+
This setting will be overridden by the ``MYPY_NUM_WORKERS`` environment
1022+
variable.
1023+
1024+
10091025
Advanced options
10101026
****************
10111027

@@ -1067,6 +1083,14 @@ These options may only be set in the global section (``[mypy]``).
10671083
Warns about missing type annotations in typeshed. This is only relevant
10681084
in combination with :confval:`disallow_untyped_defs` or :confval:`disallow_incomplete_defs`.
10691085

1086+
.. confval:: native_parser
1087+
1088+
:type: boolean
1089+
:default: False
1090+
1091+
This enables fast Rust-based parser that parses directly to mypy AST.
1092+
It will become the default parser in one of the next mypy releases.
1093+
10701094

10711095
Report generation
10721096
*****************

mypy-requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ typing_extensions>=4.14.0; python_version>='3.15'
55
mypy_extensions>=1.0.0
66
pathspec>=1.0.0
77
tomli>=1.1.0; python_version<'3.11'
8-
librt>=0.9.0; platform_python_implementation != 'PyPy'
9-
ast-serialize>=0.2.3,<1.0.0
8+
librt>=0.10.0; platform_python_implementation != 'PyPy'
9+
ast-serialize>=0.3.0,<1.0.0

0 commit comments

Comments
 (0)