Skip to content

Commit 3c5f3ff

Browse files
authored
Merge branch 'master' into bugfix/st-property-access-rewired
2 parents b211696 + c53367f commit 3c5f3ff

File tree

119 files changed

+2409
-459
lines changed

Some content is hidden

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

119 files changed

+2409
-459
lines changed

CHANGELOG.md

Lines changed: 177 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,70 @@
22

33
## Next Release
44

5-
### Remove Support for targeting Python 3.8
5+
## Mypy 1.17
66

7-
Mypy now requires `--python-version 3.9` or greater. Support for only Python 3.8 is
8-
fully removed now. Given an unsupported version, mypy will default to the oldest
9-
supported one, currently 3.9.
7+
We’ve just uploaded mypy 1.17 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 and bug fixes.
9+
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+
### Optionally Check That Match Is Exhaustive
16+
17+
Mypy can now optionally generate an error if a match statement does not
18+
match exhaustively, without having to use `assert_never(...)`. Enable
19+
this by using `--enable-error-code exhaustive-match`.
20+
21+
Example:
22+
23+
```python
24+
# mypy: enable-error-code=exhaustive-match
25+
26+
import enum
27+
28+
class Color(enum.Enum):
29+
RED = 1
30+
BLUE = 2
31+
32+
def show_color(val: Color) -> None:
33+
# error: Unhandled case for values of type "Literal[Color.BLUE]"
34+
match val:
35+
case Color.RED:
36+
print("red")
37+
```
38+
39+
This feature was contributed by Donal Burns (PR [19144](https://github.com/python/mypy/pull/19144)).
40+
41+
### Further Improvements to Attribute Resolution
42+
43+
This release includes additional improvements to how attribute types
44+
and kinds are resolved. These fix many bugs and overall improve consistency.
45+
46+
* Handle corner case: protocol/class variable/descriptor (Ivan Levkivskyi, PR [19277](https://github.com/python/mypy/pull/19277))
47+
* Fix a few inconsistencies in protocol/type object interactions (Ivan Levkivskyi, PR [19267](https://github.com/python/mypy/pull/19267))
48+
* Refactor/unify access to static attributes (Ivan Levkivskyi, PR [19254](https://github.com/python/mypy/pull/19254))
49+
* Remove inconsistencies in operator handling (Ivan Levkivskyi, PR [19250](https://github.com/python/mypy/pull/19250))
50+
* Make protocol subtyping more consistent (Ivan Levkivskyi, PR [18943](https://github.com/python/mypy/pull/18943))
51+
52+
### Fixes to Nondeterministic Type Checking
53+
54+
Previous mypy versions could infer different types for certain expressions
55+
across different runs (typically depending on which order certain types
56+
were processed, and this order was nondeterministic). This release includes
57+
fixes to several such issues.
58+
59+
* Fix nondeterministic type checking by making join with explicit Protocol and type promotion commute (Shantanu, PR [18402](https://github.com/python/mypy/pull/18402))
60+
* Fix nondeterministic type checking caused by nonassociative of None joins (Shantanu, PR [19158](https://github.com/python/mypy/pull/19158))
61+
* Fix nondeterministic type checking caused by nonassociativity of joins (Shantanu, PR [19147](https://github.com/python/mypy/pull/19147))
62+
* Fix nondeterministic type checking by making join between `type` and TypeVar commute (Shantanu, PR [19149](https://github.com/python/mypy/pull/19149))
63+
64+
### Remove Support for Targeting Python 3.8
65+
66+
Mypy now requires `--python-version 3.9` or greater. Support for targeting Python 3.8 is
67+
fully removed now. Since 3.8 is an unsupported version, mypy will default to the oldest
68+
supported version (currently 3.9) if you still try to target 3.8.
1069

1170
This change is necessary because typeshed stopped supporting Python 3.8 after it
1271
reached its End of Life in October 2024.
@@ -17,18 +76,128 @@ Contributed by Marc Mueller
1776
### Initial Support for Python 3.14
1877

1978
Mypy is now tested on 3.14 and mypyc works with 3.14.0b3 and later.
20-
Mypyc compiled wheels of mypy itself will be available for new versions after 3.14.0rc1 is released.
79+
Binary wheels compiled with mypyc for mypy itself will be available for 3.14
80+
some time after 3.14.0rc1 has been released.
2181

22-
Note that not all new features might be supported just yet.
82+
Note that not all features are supported just yet.
2383

2484
Contributed by Marc Mueller (PR [19164](https://github.com/python/mypy/pull/19164))
2585

26-
### Deprecated Flag: \--force-uppercase-builtins
86+
### Deprecated Flag: `--force-uppercase-builtins`
2787

28-
Mypy only supports Python 3.9+. The \--force-uppercase-builtins flag is now deprecated and a no-op. It will be removed in a future version.
88+
Mypy only supports Python 3.9+. The `--force-uppercase-builtins` flag is now
89+
deprecated as unnecessary, and a no-op. It will be removed in a future version.
2990

3091
Contributed by Marc Mueller (PR [19176](https://github.com/python/mypy/pull/19176))
3192

93+
### Mypyc: Improvements to Generators and Async Functions
94+
95+
This release includes both performance improvements and bug fixes related
96+
to generators and async functions (these share many implementation details).
97+
98+
* Fix exception swallowing in async try/finally blocks with await (Chainfire, PR [19353](https://github.com/python/mypy/pull/19353))
99+
* Fix AttributeError in async try/finally with mixed return paths (Chainfire, PR [19361](https://github.com/python/mypy/pull/19361))
100+
* Make generated generator helper method internal (Jukka Lehtosalo, PR [19268](https://github.com/python/mypy/pull/19268))
101+
* Free coroutine after await encounters StopIteration (Jukka Lehtosalo, PR [19231](https://github.com/python/mypy/pull/19231))
102+
* Use non-tagged integer for generator label (Jukka Lehtosalo, PR [19218](https://github.com/python/mypy/pull/19218))
103+
* Merge generator and environment classes in simple cases (Jukka Lehtosalo, PR [19207](https://github.com/python/mypy/pull/19207))
104+
105+
### Mypyc: Partial, Unsafe Support for Free Threading
106+
107+
Mypyc has minimal, quite memory-unsafe support for the free threaded
108+
builds of 3.14. It is also only lightly tested. Bug reports and experience
109+
reports are welcome!
110+
111+
Here are some of the major limitations:
112+
* Free threading only works when compiling a single module at a time.
113+
* If there is concurrent access to an object while another thread is mutating the same
114+
object, it's possible to encounter segfaults and memory corruption.
115+
* There are no efficient native primitives for thread synthronization, though the
116+
regular `threading` module can be used.
117+
* Some workloads don't scale well to multiple threads for no clear reason.
118+
119+
Related PRs:
120+
121+
* Enable partial, unsafe support for free-threading (Jukka Lehtosalo, PR [19167](https://github.com/python/mypy/pull/19167))
122+
* Fix incref/decref on free-threaded builds (Jukka Lehtosalo, PR [19127](https://github.com/python/mypy/pull/19127))
123+
124+
### Other Mypyc Fixes and Improvements
125+
126+
* Derive .c file name from full module name if using multi_file (Jukka Lehtosalo, PR [19278](https://github.com/python/mypy/pull/19278))
127+
* Support overriding the group name used in output files (Jukka Lehtosalo, PR [19272](https://github.com/python/mypy/pull/19272))
128+
* Add note about using non-native class to subclass built-in types (Jukka Lehtosalo, PR [19236](https://github.com/python/mypy/pull/19236))
129+
* Make some generated classes implicitly final (Jukka Lehtosalo, PR [19235](https://github.com/python/mypy/pull/19235))
130+
* Don't simplify module prefixes if using separate compilation (Jukka Lehtosalo, PR [19206](https://github.com/python/mypy/pull/19206))
131+
132+
### Stubgen Improvements
133+
134+
* Add import for `types` in `__exit__` method signature (Alexey Makridenko, PR [19120](https://github.com/python/mypy/pull/19120))
135+
* Add support for including class and property docstrings (Chad Dombrova, PR [17964](https://github.com/python/mypy/pull/17964))
136+
* Don't generate `Incomplete | None = None` argument annotation (Sebastian Rittau, PR [19097](https://github.com/python/mypy/pull/19097))
137+
* Support several more constructs in stubgen's alias printer (Stanislav Terliakov, PR [18888](https://github.com/python/mypy/pull/18888))
138+
139+
### Miscellaneous Fixes and Improvements
140+
141+
* Combine the revealed types of multiple iteration steps in a more robust manner (Christoph Tyralla, PR [19324](https://github.com/python/mypy/pull/19324))
142+
* Improve the handling of "iteration dependent" errors and notes in finally clauses (Christoph Tyralla, PR [19270](https://github.com/python/mypy/pull/19270))
143+
* Lessen dmypy suggest path limitations for Windows machines (CoolCat467, PR [19337](https://github.com/python/mypy/pull/19337))
144+
* Fix type ignore comments erroneously marked as unused by dmypy (Charlie Denton, PR [15043](https://github.com/python/mypy/pull/15043))
145+
* Fix misspelled `exhaustive-match` error code (johnthagen, PR [19276](https://github.com/python/mypy/pull/19276))
146+
* Fix missing error context for unpacking assignment involving star expression (Brian Schubert, PR [19258](https://github.com/python/mypy/pull/19258))
147+
* Fix and simplify error de-duplication (Ivan Levkivskyi, PR [19247](https://github.com/python/mypy/pull/19247))
148+
* Disallow `ClassVar` in type aliases (Brian Schubert, PR [19263](https://github.com/python/mypy/pull/19263))
149+
* Add script that prints list of compiled files when compiling mypy (Jukka Lehtosalo, PR [19260](https://github.com/python/mypy/pull/19260))
150+
* Fix help message url for "None and Optional handling" section (Guy Wilson, PR [19252](https://github.com/python/mypy/pull/19252))
151+
* Display fully qualified name of imported base classes in errors about incompatible overrides (Mikhail Golubev, PR [19115](https://github.com/python/mypy/pull/19115))
152+
* Avoid false `unreachable`, `redundant-expr`, and `redundant-casts` warnings in loops more robustly and efficiently, and avoid multiple `revealed type` notes for the same line (Christoph Tyralla, PR [19118](https://github.com/python/mypy/pull/19118))
153+
* Fix type extraction from `isinstance` checks (Stanislav Terliakov, PR [19223](https://github.com/python/mypy/pull/19223))
154+
* Erase stray type variables in `functools.partial` (Stanislav Terliakov, PR [18954](https://github.com/python/mypy/pull/18954))
155+
* Make inferring condition value recognize the whole truth table (Stanislav Terliakov, PR [18944](https://github.com/python/mypy/pull/18944))
156+
* Support type aliases, `NamedTuple` and `TypedDict` in constrained TypeVar defaults (Stanislav Terliakov, PR [18884](https://github.com/python/mypy/pull/18884))
157+
* Move dataclass `kw_only` fields to the end of the signature (Stanislav Terliakov, PR [19018](https://github.com/python/mypy/pull/19018))
158+
* Provide a better fallback value for the `python_version` option (Marc Mueller, PR [19162](https://github.com/python/mypy/pull/19162))
159+
* Avoid spurious non-overlapping equality error with metaclass with `__eq__` (Michael J. Sullivan, PR [19220](https://github.com/python/mypy/pull/19220))
160+
* Narrow type variable bounds (Ivan Levkivskyi, PR [19183](https://github.com/python/mypy/pull/19183))
161+
* Add classifier for Python 3.14 (Marc Mueller, PR [19199](https://github.com/python/mypy/pull/19199))
162+
* Capitalize syntax error messages (Charulata, PR [19114](https://github.com/python/mypy/pull/19114))
163+
* Infer constraints eagerly if actual is Any (Ivan Levkivskyi, PR [19190](https://github.com/python/mypy/pull/19190))
164+
* Include walrus assignments in conditional inference (Stanislav Terliakov, PR [19038](https://github.com/python/mypy/pull/19038))
165+
* Use PEP 604 syntax when converting types to strings (Marc Mueller, PR [19179](https://github.com/python/mypy/pull/19179))
166+
* Use more lower-case builtin types in error messages (Marc Mueller, PR [19177](https://github.com/python/mypy/pull/19177))
167+
* Fix example to use correct method of Stack (Łukasz Kwieciński, PR [19123](https://github.com/python/mypy/pull/19123))
168+
* Forbid `.pop` of `Readonly` `NotRequired` TypedDict items (Stanislav Terliakov, PR [19133](https://github.com/python/mypy/pull/19133))
169+
* Emit a friendlier warning on invalid exclude regex, instead of a stacktrace (wyattscarpenter, PR [19102](https://github.com/python/mypy/pull/19102))
170+
* Enable ANSI color codes for dmypy client in Windows (wyattscarpenter, PR [19088](https://github.com/python/mypy/pull/19088))
171+
* Extend special case for context-based type variable inference to unions in return position (Stanislav Terliakov, PR [18976](https://github.com/python/mypy/pull/18976))
172+
173+
### Acknowledgements
174+
175+
Thanks to all mypy contributors who contributed to this release:
176+
177+
* Alexey Makridenko
178+
* Brian Schubert
179+
* Chad Dombrova
180+
* Chainfire
181+
* Charlie Denton
182+
* Charulata
183+
* Christoph Tyralla
184+
* CoolCat467
185+
* Donal Burns
186+
* Guy Wilson
187+
* Ivan Levkivskyi
188+
* johnthagen
189+
* Jukka Lehtosalo
190+
* Łukasz Kwieciński
191+
* Marc Mueller
192+
* Michael J. Sullivan
193+
* Mikhail Golubev
194+
* Sebastian Rittau
195+
* Shantanu
196+
* Stanislav Terliakov
197+
* wyattscarpenter
198+
199+
I’d also like to thank my employer, Dropbox, for supporting mypy development.
200+
32201
## Mypy 1.16
33202

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

docs/source/conf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,9 @@
278278
intersphinx_mapping = {
279279
"python": ("https://docs.python.org/3", None),
280280
"attrs": ("https://www.attrs.org/en/stable/", None),
281-
"cython": ("https://docs.cython.org/en/latest", None),
281+
"cython": ("https://cython.readthedocs.io/en/stable", None),
282282
"monkeytype": ("https://monkeytype.readthedocs.io/en/latest", None),
283-
"setuptools": ("https://setuptools.readthedocs.io/en/latest", None),
283+
"setuptools": ("https://setuptools.pypa.io/en/latest", None),
284284
}
285285

286286

misc/log_trace_check.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
"""Compile mypy using mypyc with trace logging enabled, and collect a trace.
2+
3+
The trace log can be used to analyze low-level performance bottlenecks.
4+
5+
By default does a self check as the workload.
6+
7+
This works on all supported platforms, unlike some of our other performance tools.
8+
"""
9+
10+
from __future__ import annotations
11+
12+
import argparse
13+
import glob
14+
import os
15+
import shutil
16+
import subprocess
17+
import sys
18+
import time
19+
20+
from perf_compare import build_mypy, clone
21+
22+
# Generated files, including binaries, go under this directory to avoid overwriting user state.
23+
TARGET_DIR = "mypy.log_trace.tmpdir"
24+
25+
26+
def perform_type_check(target_dir: str, code: str | None) -> None:
27+
cache_dir = os.path.join(target_dir, ".mypy_cache")
28+
if os.path.exists(cache_dir):
29+
shutil.rmtree(cache_dir)
30+
args = []
31+
if code is None:
32+
args.extend(["--config-file", "mypy_self_check.ini"])
33+
for pat in "mypy/*.py", "mypy/*/*.py", "mypyc/*.py", "mypyc/test/*.py":
34+
args.extend(glob.glob(pat))
35+
else:
36+
args.extend(["-c", code])
37+
check_cmd = ["python", "-m", "mypy"] + args
38+
t0 = time.time()
39+
subprocess.run(check_cmd, cwd=target_dir, check=True)
40+
elapsed = time.time() - t0
41+
print(f"{elapsed:.2f}s elapsed")
42+
43+
44+
def main() -> None:
45+
parser = argparse.ArgumentParser(
46+
description="Compile mypy and collect a trace log while type checking (by default, self check)."
47+
)
48+
parser.add_argument(
49+
"--multi-file",
50+
action="store_true",
51+
help="compile mypy into one C file per module (to reduce RAM use during compilation)",
52+
)
53+
parser.add_argument(
54+
"--skip-compile", action="store_true", help="use compiled mypy from previous run"
55+
)
56+
parser.add_argument(
57+
"-c",
58+
metavar="CODE",
59+
default=None,
60+
type=str,
61+
help="type check Python code fragment instead of mypy self-check",
62+
)
63+
args = parser.parse_args()
64+
multi_file: bool = args.multi_file
65+
skip_compile: bool = args.skip_compile
66+
code: str | None = args.c
67+
68+
target_dir = TARGET_DIR
69+
70+
if not skip_compile:
71+
clone(target_dir, "HEAD")
72+
73+
print(f"Building mypy in {target_dir} with trace logging enabled...")
74+
build_mypy(target_dir, multi_file, log_trace=True, opt_level="0")
75+
elif not os.path.isdir(target_dir):
76+
sys.exit("error: Can't find compile mypy from previous run -- can't use --skip-compile")
77+
78+
perform_type_check(target_dir, code)
79+
80+
trace_fnam = os.path.join(target_dir, "mypyc_trace.txt")
81+
print(f"Generated event trace log in {trace_fnam}")
82+
83+
84+
if __name__ == "__main__":
85+
main()

misc/perf_compare.py

100644100755
Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#! /usr/bin/env python
2+
13
"""Compare performance of mypyc-compiled mypy between one or more commits/branches.
24
35
Simple usage:
@@ -35,13 +37,22 @@ def heading(s: str) -> None:
3537
print()
3638

3739

38-
def build_mypy(target_dir: str, multi_file: bool, *, cflags: str | None = None) -> None:
40+
def build_mypy(
41+
target_dir: str,
42+
multi_file: bool,
43+
*,
44+
cflags: str | None = None,
45+
log_trace: bool = False,
46+
opt_level: str = "2",
47+
) -> None:
3948
env = os.environ.copy()
4049
env["CC"] = "clang"
41-
env["MYPYC_OPT_LEVEL"] = "2"
50+
env["MYPYC_OPT_LEVEL"] = opt_level
4251
env["PYTHONHASHSEED"] = "1"
4352
if multi_file:
4453
env["MYPYC_MULTI_FILE"] = "1"
54+
if log_trace:
55+
env["MYPYC_LOG_TRACE"] = "1"
4556
if cflags is not None:
4657
env["CFLAGS"] = cflags
4758
cmd = [sys.executable, "setup.py", "--use-mypyc", "build_ext", "--inplace"]

mypy/binder.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ def __init__(self, options: Options) -> None:
138138
# flexible inference of variable types (--allow-redefinition-new).
139139
self.bind_all = options.allow_redefinition_new
140140

141+
# This tracks any externally visible changes in binder to invalidate
142+
# expression caches when needed.
143+
self.version = 0
144+
141145
def _get_id(self) -> int:
142146
self.next_id += 1
143147
return self.next_id
@@ -158,6 +162,7 @@ def push_frame(self, conditional_frame: bool = False) -> Frame:
158162
return f
159163

160164
def _put(self, key: Key, type: Type, from_assignment: bool, index: int = -1) -> None:
165+
self.version += 1
161166
self.frames[index].types[key] = CurrentType(type, from_assignment)
162167

163168
def _get(self, key: Key, index: int = -1) -> CurrentType | None:
@@ -185,6 +190,7 @@ def put(self, expr: Expression, typ: Type, *, from_assignment: bool = True) -> N
185190
self._put(key, typ, from_assignment)
186191

187192
def unreachable(self) -> None:
193+
self.version += 1
188194
self.frames[-1].unreachable = True
189195

190196
def suppress_unreachable_warnings(self) -> None:

0 commit comments

Comments
 (0)