@@ -14,10 +14,32 @@ You can read the full documentation for this release on [Read the Docs](http://m
14
14
15
15
### Support Python 3.12 Syntax for Generics (PEP 695)
16
16
17
- Support for the new type parameter syntax introduced in 3.12 is now enabled by default,
17
+ Support for the new type parameter syntax introduced in Python 3.12 is now enabled by default,
18
18
documented, and no longer experimental. It was available through a feature flag in
19
19
mypy 1.11 as an experimental feature.
20
20
21
+ This example demonstrates the new syntax:
22
+
23
+ ``` python
24
+ # Generic function
25
+ def f[T](x: T) -> T: ...
26
+
27
+ reveal_type(f(1 )) # Revealed type is 'int'
28
+
29
+ # Generic class
30
+ class C[T]:
31
+ def __init__ (self , x : T) -> None :
32
+ self .x = x
33
+
34
+ c = C(' a' )
35
+ reveal_type(c.x) # Revealed type is 'str'
36
+
37
+ # Type alias
38
+ type A[T] = C[list[T]]
39
+ ```
40
+
41
+ For more information, refer to the [ documentation] ( https://mypy.readthedocs.io/en/latest/generics.html ) .
42
+
21
43
These improvements are included:
22
44
23
45
* Document Python 3.12 type parameter syntax (Jukka Lehtosalo, PR [ 17816] ( https://github.com/python/mypy/pull/17816 ) )
@@ -35,14 +57,14 @@ These improvements are included:
35
57
36
58
### Basic Support for Python 3.13
37
59
38
- This release adds compiled binaries for Python 3.13 and partial support for Python 3.13
39
- features. Mypyc also now supports Python 3.13.
60
+ This release adds partial support for Python 3.13 features and compiled binaries for
61
+ Python 3.13. Mypyc now also supports Python 3.13.
40
62
41
- These features are supported:
63
+ In particular, these features are supported:
42
64
* Various new stdlib features and changes (through typeshed stub improvements)
43
65
* ` typing.ReadOnly ` (see below for more)
44
- * ` typing.TypeIs ` (support was added in mypy 1.10)
45
- * Type parameter defaults when using the legacy syntax ([ PEP 695 ] ( https://peps.python.org/pep-0696/ ) )
66
+ * ` typing.TypeIs ` (added in mypy 1.10, [ PEP 742 ] ( https://peps.python.org/pep-0742/ ) )
67
+ * Type parameter defaults when using the legacy syntax ([ PEP 696 ] ( https://peps.python.org/pep-0696/ ) )
46
68
47
69
These features are not supported yet:
48
70
* ` warnings.deprecated ` ([ PEP 702] ( https://peps.python.org/pep-0702/ ) )
@@ -71,12 +93,20 @@ List of changes:
71
93
72
94
### Inferring Unions for Conditional Expressions
73
95
74
- Mypy now always tries to infer a union type for a conditional expression. This results in
75
- more precise inferred types and lets mypy detect more issues.
96
+ Mypy now always tries to infer a union type for a conditional expression if left and right
97
+ operand types are different. This results in more precise inferred types and lets mypy detect
98
+ more issues. Example:
99
+
100
+ ``` python
101
+ s = " foo" if cond() else 1
102
+ # Type of "s" is now "str | int" (it used to be "object")
103
+ ```
76
104
77
105
Notably, if one of the operands has type ` Any ` , the type of a conditional expression is
78
- now ` <type> | Any ` . Previously the inferred type was just ` Any ` . Example where this is
79
- relevant:
106
+ now ` <type> | Any ` . Previously the inferred type was just ` Any ` . The new type essentially
107
+ indicates that the value can be of type ` <type> ` , and potentially of some (unknown) type.
108
+ Most operations performed on the result must also be valid for ` <type> ` .
109
+ Example where this is relevant:
80
110
81
111
``` python
82
112
from typing import Any
@@ -89,14 +119,32 @@ def func(a: Any, b: bool) -> None:
89
119
90
120
This feature was contributed by Ivan Levkivskyi (PR [ 17427] ( https://github.com/python/mypy/pull/17427 ) ).
91
121
92
- ### ReadOnly Support for TypedDict
122
+ ### ReadOnly Support for TypedDict (PEP 705)
123
+
124
+ You can now use ` typing.ReadOnly ` to specity TypedDict items as
125
+ read-only ([ PEP 705] ( https://peps.python.org/pep-0705/ ) ):
126
+
127
+ ``` python
128
+ from typing import TypedDict
129
+
130
+ # Or "from typing ..." on Python 3.13
131
+ from typing_extensions import ReadOnly
93
132
94
- * Add ` ReadOnly ` support for TypedDicts (sobolevn, PR [ 17644] ( https://github.com/python/mypy/pull/17644 ) )
133
+ class TD (TypedDict ):
134
+ a: int
135
+ b: ReadOnly[int ]
136
+
137
+ d: TD = {" a" : 1 , " b" : 2 }
138
+ d[" a" ] = 3 # OK
139
+ d[" b" ] = 5 # Error: "b" is ReadOnly
140
+ ```
141
+
142
+ This feature was contributed by Nikita Sobolev (PR [ 17644] ( https://github.com/python/mypy/pull/17644 ) ).
95
143
96
144
### Python 3.8 End of Life Approaching
97
145
98
146
We are planning to drop support for Python 3.8 in the next mypy feature release or the
99
- one after that. Python 3.8 reaches end of life in Octoboer 2024.
147
+ one after that. Python 3.8 reaches end of life in October 2024.
100
148
101
149
### Planned Changes to Defaults
102
150
@@ -121,6 +169,17 @@ For more information, refer to the
121
169
Mypy documentation now uses modern syntax variants and imports in many examples. Some
122
170
examples no longer work on Python 3.8, which is the earliest Python version that mypy supports.
123
171
172
+ Notably, ` Iterable ` and other protocols/ABCs are imported from ` collections.abc ` instead of
173
+ ` typing ` :
174
+ ``` python
175
+ from collections.abc import Iterable, Callable
176
+ ```
177
+
178
+ Examples also avoid the upper-case aliases to built-in types: ` list[str] ` is used instead
179
+ of ` List[str] ` . The ` X | Y ` union type syntax introduced in Python 3.10 is also now prevalent.
180
+
181
+ List of documentation updates:
182
+
124
183
* Document ` --output=json ` CLI option (Edgar Ramírez Mondragón, PR [ 17611] ( https://github.com/python/mypy/pull/17611 ) )
125
184
* Update various references to deprecated type aliases in docs (Jukka Lehtosalo, PR [ 17829] ( https://github.com/python/mypy/pull/17829 ) )
126
185
* Make "X | Y" union syntax more prominent in documentation (Jukka Lehtosalo, PR [ 17835] ( https://github.com/python/mypy/pull/17835 ) )
@@ -129,13 +188,28 @@ examples no longer work on Python 3.8, which is the earliest Python version that
129
188
* List all incomplete features in ` --enable-incomplete-feature ` docs (sobolevn, PR [ 17633] ( https://github.com/python/mypy/pull/17633 ) )
130
189
* Remove the explicit setting of a pygments theme (Pradyun Gedam, PR [ 17571] ( https://github.com/python/mypy/pull/17571 ) )
131
190
191
+ ### Experimental Inline TypedDict Syntax
192
+
193
+ Mypy now supports a non-standard, experimental syntax for defining anonymous TypedDicts.
194
+ Example:
195
+
196
+ ``` python
197
+ def func (n : str , y : int ) -> {" name" : str , " year" : int }:
198
+ return {" name" : n, " year" : y}
199
+ ```
200
+
201
+ The feature is disabled by default. Use ` --enable-incomplete-feature=InlineTypedDict ` to
202
+ enable it. * We might remove this feature in a future release.*
203
+
204
+ This feature was contributed by Ivan Levkivskyi (PR [ 17457] ( https://github.com/python/mypy/pull/17457 ) ).
205
+
132
206
### Stubgen Improvements
133
207
134
208
* Fix crash on literal class-level keywords (sobolevn, PR [ 17663] ( https://github.com/python/mypy/pull/17663 ) )
135
209
* Stubgen add ` --version ` (sobolevn, PR [ 17662] ( https://github.com/python/mypy/pull/17662 ) )
136
210
* Fix ` stubgen --no-analysis/--parse-only ` docs (sobolevn, PR [ 17632] ( https://github.com/python/mypy/pull/17632 ) )
137
211
* Include keyword only args when generating signatures in stubgenc (Eric Mark Martin, PR [ 17448] ( https://github.com/python/mypy/pull/17448 ) )
138
- * Add Literal support for docstrings (Michael Carlstrom, PR [ 17441] ( https://github.com/python/mypy/pull/17441 ) )
212
+ * Add support for detecting ` Literal ` types when extracting types from docstrings (Michael Carlstrom, PR [ 17441] ( https://github.com/python/mypy/pull/17441 ) )
139
213
* Use ` Generator ` type var defaults (Sebastian Rittau, PR [ 17670] ( https://github.com/python/mypy/pull/17670 ) )
140
214
141
215
### Stubtest Improvements
@@ -152,7 +226,7 @@ examples no longer work on Python 3.8, which is the earliest Python version that
152
226
* Fix narrowing of IntEnum and StrEnum types (Jukka Lehtosalo, PR [ 17874] ( https://github.com/python/mypy/pull/17874 ) )
153
227
* Filter overload items based on self type during type inference (Jukka Lehtosalo, PR [ 17873] ( https://github.com/python/mypy/pull/17873 ) )
154
228
* Enable negative narrowing of union TypeVar upper bounds (Brian Schubert, PR [ 17850] ( https://github.com/python/mypy/pull/17850 ) )
155
- * Fix get_member_expr_fullname returning strings with embedded "None" (Brian Schubert, PR [ 17848] ( https://github.com/python/mypy/pull/17848 ) )
229
+ * Fix issue with member expression formatting (Brian Schubert, PR [ 17848] ( https://github.com/python/mypy/pull/17848 ) )
156
230
* Avoid type size explosion when expanding types (Jukka Lehtosalo, PR [ 17842] ( https://github.com/python/mypy/pull/17842 ) )
157
231
* Fix negative narrowing of tuples in match statement (Brian Schubert, PR [ 17817] ( https://github.com/python/mypy/pull/17817 ) )
158
232
* Narrow falsey str/bytes/int to literal type (Brian Schubert, PR [ 17818] ( https://github.com/python/mypy/pull/17818 ) )
@@ -163,13 +237,12 @@ examples no longer work on Python 3.8, which is the earliest Python version that
163
237
* Added error code for overlapping function signatures (Katrina Connors, PR [ 17597] ( https://github.com/python/mypy/pull/17597 ) )
164
238
* Check for ` truthy-bool ` in ` not ... ` unary expressions (sobolevn, PR [ 17773] ( https://github.com/python/mypy/pull/17773 ) )
165
239
* Add missing lines-covered and lines-valid attributes (Soubhik Kumar Mitra, PR [ 17738] ( https://github.com/python/mypy/pull/17738 ) )
166
- * Fix another crash scenario on recursive tuple types (Ivan Levkivskyi, PR [ 17708] ( https://github.com/python/mypy/pull/17708 ) )
240
+ * Fix another crash scenario with recursive tuple types (Ivan Levkivskyi, PR [ 17708] ( https://github.com/python/mypy/pull/17708 ) )
167
241
* Resolve TypeVar upper bounds in ` functools.partial ` (Shantanu, PR [ 17660] ( https://github.com/python/mypy/pull/17660 ) )
168
242
* Always reset binder when checking deferred nodes (Ivan Levkivskyi, PR [ 17643] ( https://github.com/python/mypy/pull/17643 ) )
169
243
* Fix crash on a callable attribute with single unpack (Ivan Levkivskyi, PR [ 17641] ( https://github.com/python/mypy/pull/17641 ) )
170
244
* Fix mismatched signature between checker plugin API and implementation (bzoracler, PR [ 17343] ( https://github.com/python/mypy/pull/17343 ) )
171
245
* Indexing a type also produces a GenericAlias (Shantanu, PR [ 17546] ( https://github.com/python/mypy/pull/17546 ) )
172
- * Experimental: allow inline/anonymous TypedDicts (Ivan Levkivskyi, PR [ 17457] ( https://github.com/python/mypy/pull/17457 ) )
173
246
* Fix crash on self-type in callable protocol (Ivan Levkivskyi, PR [ 17499] ( https://github.com/python/mypy/pull/17499 ) )
174
247
* Fix crash on NamedTuple with method and error in function (Ivan Levkivskyi, PR [ 17498] ( https://github.com/python/mypy/pull/17498 ) )
175
248
* Add ` __replace__ ` for dataclasses in 3.13 (Max Muoto, PR [ 17469] ( https://github.com/python/mypy/pull/17469 ) )
@@ -185,7 +258,33 @@ Please see [git log](https://github.com/python/typeshed/commits/main?after=91a58
185
258
### Acknowledgements
186
259
Thanks to all mypy contributors who contributed to this release:
187
260
188
- - TODO
261
+ - Ali Hamdan
262
+ - Anders Kaseorg
263
+ - Bénédikt Tran
264
+ - Brian Schubert
265
+ - bzoracler
266
+ - Danny Yang
267
+ - Edgar Ramírez Mondragón
268
+ - Eric Mark Martin
269
+ - InSync
270
+ - Ivan Levkivskyi
271
+ - Jordandev678
272
+ - Katrina Connors
273
+ - Kirill Podoprigora
274
+ - Marc Mueller
275
+ - Max Muoto
276
+ - Max Murin
277
+ - Michael Carlstrom
278
+ - Michael I Chen
279
+ - Pradyun Gedam
280
+ - quinn-sasha
281
+ - Raphael Krupinski
282
+ - Sebastian Rittau
283
+ - Shantanu
284
+ - sobolevn
285
+ - Soubhik Kumar Mitra
286
+ - Stanislav Terliakov
287
+ - wyattscarpenter
189
288
190
289
I’d also like to thank my employer, Dropbox, for supporting mypy development.
191
290
0 commit comments