Debian Codegraph @

test-data/unit/check-python312.test

raw
1 [case testPEP695TypeAliasBasic]
2 type MyInt = int
3
4 def f(x: MyInt) -> MyInt:
5 return reveal_type(x) # N: Revealed type is "builtins.int"
6
7 type MyList[T] = list[T]
8
9 def g(x: MyList[int]) -> MyList[int]:
10 return reveal_type(x) # N: Revealed type is "builtins.list[builtins.int]"
11
12 type MyInt2 = int
13
14 def h(x: MyInt2) -> MyInt2:
15 return reveal_type(x) # N: Revealed type is "builtins.int"
16
17 [case testPEP695Class]
18 class MyGen[T]:
19 def __init__(self, x: T) -> None:
20 self.x = x
21
22 def f(x: MyGen[int]):
23 reveal_type(x.x) # N: Revealed type is "builtins.int"
24
25 [case testPEP695Function]
26 def f[T](x: T) -> T:
27 return reveal_type(x) # N: Revealed type is "T`-1"
28
29 reveal_type(f(1)) # N: Revealed type is "builtins.int"
30
31 async def g[T](x: T) -> T:
32 return reveal_type(x) # N: Revealed type is "T`-1"
33
34 reveal_type(g(1)) # E: Value of type "Coroutine[Any, Any, int]" must be used \
35 # N: Are you missing an await? \
36 # N: Revealed type is "typing.Coroutine[Any, Any, builtins.int]"
37
38 [case testPEP695TypeVarBasic]
39 from typing import Callable
40 type Alias1[T: int] = list[T]
41 type Alias2[**P] = Callable[P, int]
42 type Alias3[*Ts] = tuple[*Ts]
43
44 class Cls1[T: int]: ...
45 class Cls2[**P]: ...
46 class Cls3[*Ts]: ...
47
48 def func1[T: int](x: T) -> T: ...
49 def func2[**P](x: Callable[P, int]) -> Callable[P, str]: ...
50 def func3[*Ts](x: tuple[*Ts]) -> tuple[int, *Ts]: ...
51 [builtins fixtures/tuple.pyi]
52
53 [case testPEP695TypeAliasType]
54 from typing import Callable, TypeAliasType, TypeVar, TypeVarTuple
55
56 T = TypeVar("T")
57 Ts = TypeVarTuple("Ts")
58
59 TestType = TypeAliasType("TestType", int | str)
60 x: TestType = 42
61 y: TestType = 'a'
62 z: TestType = object() # E: Incompatible types in assignment (expression has type "object", variable has type "int | str")
63
64 BadAlias1 = TypeAliasType("BadAlias1", tuple[*Ts]) # E: TypeVarTuple "Ts" is not included in type_params
65 ba1: BadAlias1[int] # E: Bad number of arguments for type alias, expected 0, given 1
66 reveal_type(ba1) # N: Revealed type is "builtins.tuple[Any, ...]"
67
68 BadAlias2 = TypeAliasType("BadAlias2", Callable[[*Ts], str]) # E: TypeVarTuple "Ts" is not included in type_params
69 ba2: BadAlias2[int] # E: Bad number of arguments for type alias, expected 0, given 1
70 reveal_type(ba2) # N: Revealed type is "def (*Any) -> builtins.str"
71
72 [builtins fixtures/tuple.pyi]
73 [typing fixtures/typing-full.pyi]
74
75 [case testPEP695IncompleteFeatureIsAcceptedButHasNoEffect]
76 # mypy: enable-incomplete-feature=NewGenericSyntax
77 def f[T](x: T) -> T:
78 return x
79 reveal_type(f(1)) # N: Revealed type is "builtins.int"
80
81 [case testPEP695GenericFunctionSyntax]
82 def ident[TV](x: TV) -> TV:
83 y: TV = x
84 y = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "TV")
85 return x
86
87 reveal_type(ident(1)) # N: Revealed type is "builtins.int"
88 reveal_type(ident('x')) # N: Revealed type is "builtins.str"
89
90 a: TV # E: Name "TV" is not defined
91
92 def tup[T, S](x: T, y: S) -> tuple[T, S]:
93 reveal_type((x, y)) # N: Revealed type is "tuple[T`-1, S`-2]"
94 return (x, y)
95
96 reveal_type(tup(1, 'x')) # N: Revealed type is "tuple[builtins.int, builtins.str]"
97 [builtins fixtures/tuple.pyi]
98
99 [case testPEP695GenericClassSyntax]
100 class C[T]:
101 x: T
102
103 def __init__(self, x: T) -> None:
104 self.x = x
105
106 def ident(self, x: T) -> T:
107 y: T = x
108 if int():
109 return self.x
110 else:
111 return y
112
113 reveal_type(C("x")) # N: Revealed type is "__main__.C[builtins.str]"
114 c: C[int] = C(1)
115 reveal_type(c.x) # N: Revealed type is "builtins.int"
116 reveal_type(c.ident(1)) # N: Revealed type is "builtins.int"
117
118 [case testPEP695GenericMethodInGenericClass]
119 class C[T]:
120 def m[S](self, x: S) -> T | S: ...
121
122 a: C[int] = C[object]() # E: Incompatible types in assignment (expression has type "C[object]", variable has type "C[int]")
123 b: C[object] = C[int]()
124
125 reveal_type(C[str]().m(1)) # N: Revealed type is "builtins.str | builtins.int"
126
127 [case testPEP695InferVarianceSimpleFromMethod]
128 class Invariant[T]:
129 def f(self, x: T) -> None:
130 pass
131
132 def g(self) -> T | None:
133 return None
134
135 a: Invariant[object]
136 b: Invariant[int]
137 if int():
138 a = b # E: Incompatible types in assignment (expression has type "Invariant[int]", variable has type "Invariant[object]")
139 if int():
140 b = a # E: Incompatible types in assignment (expression has type "Invariant[object]", variable has type "Invariant[int]")
141
142 class Covariant[T]:
143 def g(self) -> T | None:
144 return None
145
146 c: Covariant[object]
147 d: Covariant[int]
148 if int():
149 c = d
150 if int():
151 d = c # E: Incompatible types in assignment (expression has type "Covariant[object]", variable has type "Covariant[int]")
152
153 class Contravariant[T]:
154 def f(self, x: T) -> None:
155 pass
156
157 e: Contravariant[object]
158 f: Contravariant[int]
159 if int():
160 e = f # E: Incompatible types in assignment (expression has type "Contravariant[int]", variable has type "Contravariant[object]")
161 if int():
162 f = e
163
164 [case testPEP695InferVarianceSimpleFromAttribute]
165 class Invariant1[T]:
166 def __init__(self, x: T) -> None:
167 self.x = x
168
169 a: Invariant1[object]
170 b: Invariant1[int]
171 if int():
172 a = b # E: Incompatible types in assignment (expression has type "Invariant1[int]", variable has type "Invariant1[object]")
173 if int():
174 b = a # E: Incompatible types in assignment (expression has type "Invariant1[object]", variable has type "Invariant1[int]")
175
176 class Invariant2[T]:
177 def __init__(self) -> None:
178 self.x: list[T] = []
179
180 a2: Invariant2[object]
181 b2: Invariant2[int]
182 if int():
183 a2 = b2 # E: Incompatible types in assignment (expression has type "Invariant2[int]", variable has type "Invariant2[object]")
184 if int():
185 b2 = a2 # E: Incompatible types in assignment (expression has type "Invariant2[object]", variable has type "Invariant2[int]")
186
187 class Invariant3[T]:
188 def __init__(self) -> None:
189 self.x: T | None = None
190
191 a3: Invariant3[object]
192 b3: Invariant3[int]
193 if int():
194 a3 = b3 # E: Incompatible types in assignment (expression has type "Invariant3[int]", variable has type "Invariant3[object]")
195 if int():
196 b3 = a3 # E: Incompatible types in assignment (expression has type "Invariant3[object]", variable has type "Invariant3[int]")
197
198 [case testPEP695InferVarianceRecursive]
199 class Invariant[T]:
200 def f(self, x: Invariant[T]) -> Invariant[T]:
201 return x
202
203 class Covariant[T]:
204 def f(self) -> Covariant[T]:
205 return self
206
207 class Contravariant[T]:
208 def f(self, x: Contravariant[T]) -> None:
209 pass
210
211 a: Invariant[object]
212 b: Invariant[int]
213 if int():
214 a = b # E: Incompatible types in assignment (expression has type "Invariant[int]", variable has type "Invariant[object]")
215 if int():
216 b = a
217
218 c: Covariant[object]
219 d: Covariant[int]
220 if int():
221 c = d
222 if int():
223 d = c # E: Incompatible types in assignment (expression has type "Covariant[object]", variable has type "Covariant[int]")
224
225 e: Contravariant[object]
226 f: Contravariant[int]
227 if int():
228 e = f # E: Incompatible types in assignment (expression has type "Contravariant[int]", variable has type "Contravariant[object]")
229 if int():
230 f = e
231
232 [case testPEP695InferVarianceInFrozenDataclass]
233 from dataclasses import dataclass
234
235 @dataclass(frozen=True)
236 class Covariant[T]:
237 x: T
238
239 cov1: Covariant[float] = Covariant[int](1)
240 cov2: Covariant[int] = Covariant[float](1) # E: Incompatible types in assignment (expression has type "Covariant[float]", variable has type "Covariant[int]")
241
242 @dataclass(frozen=True)
243 class Invariant[T]:
244 x: list[T]
245
246 inv1: Invariant[float] = Invariant[int]([1]) # E: Incompatible types in assignment (expression has type "Invariant[int]", variable has type "Invariant[float]")
247 inv2: Invariant[int] = Invariant[float]([1]) # E: Incompatible types in assignment (expression has type "Invariant[float]", variable has type "Invariant[int]")
248 [builtins fixtures/tuple.pyi]
249 [typing fixtures/typing-full.pyi]
250
251 [case testPEP695InferVarianceCalculateOnDemand]
252 class Covariant[T]:
253 def __init__(self) -> None:
254 self.x = [1]
255
256 def f(self) -> None:
257 c = Covariant[int]()
258 # We need to know that T is covariant here
259 self.g(c)
260 c2 = Covariant[object]()
261 self.h(c2) # E: Argument 1 to "h" of "Covariant" has incompatible type "Covariant[object]"; expected "Covariant[int]"
262
263 def g(self, x: Covariant[object]) -> None: pass
264 def h(self, x: Covariant[int]) -> None: pass
265
266 [case testPEP695InferVarianceNotReadyWhenNeeded]
267 class Covariant[T]:
268 def f(self) -> None:
269 c = Covariant[int]()
270 # We need to know that T is covariant here
271 self.g(c)
272 c2 = Covariant[object]()
273 self.h(c2) # E: Argument 1 to "h" of "Covariant" has incompatible type "Covariant[object]"; expected "Covariant[int]"
274
275 def g(self, x: Covariant[object]) -> None: pass
276 def h(self, x: Covariant[int]) -> None: pass
277
278 def __init__(self) -> None:
279 self.x = [1]
280
281 class Invariant[T]:
282 def f(self) -> None:
283 # We add this to force processing this method as part of the interface in parallel checking,
284 # otherwise, it will be processed at a later stage, not testing the no ready behavior below
285 self.y = int()
286 c = Invariant(1)
287 # We need to know that T is invariant here, and for this we need the type
288 # of self.x, which won't be available on the first type checking pass,
289 # since __init__ is defined later in the file. In this case we fall back
290 # covariance.
291 self.g(c)
292 c2 = Invariant(object())
293 self.h(c2) # E: Argument 1 to "h" of "Invariant" has incompatible type "Invariant[object]"; expected "Invariant[int]"
294
295 def g(self, x: Invariant[object]) -> None: pass
296 def h(self, x: Invariant[int]) -> None: pass
297
298 def __init__(self, x: T) -> None:
299 self.x = x
300
301 # Now we should have the variance correct.
302 a: Invariant[object]
303 b: Invariant[int]
304 if int():
305 a = b # E: Incompatible types in assignment (expression has type "Invariant[int]", variable has type "Invariant[object]")
306 if int():
307 b = a # E: Incompatible types in assignment (expression has type "Invariant[object]", variable has type "Invariant[int]")
308
309 [case testPEP695InferVarianceNotReadyForJoin]
310 class Invariant[T]:
311 def f(self) -> None:
312 self.y = int()
313 # Assume covariance if variance us not ready
314 reveal_type([Invariant(1), Invariant(object())]) \
315 # N: Revealed type is "builtins.list[__main__.Invariant[builtins.object]]"
316
317 def __init__(self, x: T) -> None:
318 self.x = x
319
320 reveal_type([Invariant(1), Invariant(object())]) # N: Revealed type is "builtins.list[builtins.object]"
321
322 [case testPEP695InferVarianceNotReadyForMeet]
323 from typing import TypeVar, Callable
324
325 S = TypeVar("S")
326 def c(a: Callable[[S], None], b: Callable[[S], None]) -> S: ...
327
328 def a1(x: Invariant[int]) -> None: pass
329 def a2(x: Invariant[object]) -> None: pass
330
331 class Invariant[T]:
332 def f(self) -> None:
333 self.y = int()
334 reveal_type(c(a1, a2)) # N: Revealed type is "__main__.Invariant[builtins.int]"
335
336 def __init__(self, x: T) -> None:
337 self.x = x
338
339 reveal_type(c(a1, a2)) # N: Revealed type is "Never"
340
341 [case testPEP695InferVarianceUnderscorePrefix]
342 class Covariant1[T]:
343 def __init__(self, x: T) -> None:
344 self._x = x
345
346 @property
347 def x(self) -> T:
348 return self._x
349
350 co1_1: Covariant1[float] = Covariant1[int](1)
351 co1_2: Covariant1[int] = Covariant1[float](1) # E: Incompatible types in assignment (expression has type "Covariant1[float]", variable has type "Covariant1[int]")
352
353 class Covariant2[T]:
354 def __init__(self, x: T) -> None:
355 self.__foo_bar = x
356
357 @property
358 def x(self) -> T:
359 return self.__foo_bar
360
361 co2_1: Covariant2[float] = Covariant2[int](1)
362 co2_2: Covariant2[int] = Covariant2[float](1) # E: Incompatible types in assignment (expression has type "Covariant2[float]", variable has type "Covariant2[int]")
363
364 class Invariant1[T]:
365 def __init__(self, x: T) -> None:
366 self._x = x
367
368 # Methods behave differently from attributes
369 def _f(self, x: T) -> None: ...
370
371 @property
372 def x(self) -> T:
373 return self._x
374
375 inv1_1: Invariant1[float] = Invariant1[int](1) # E: Incompatible types in assignment (expression has type "Invariant1[int]", variable has type "Invariant1[float]")
376 inv1_2: Invariant1[int] = Invariant1[float](1) # E: Incompatible types in assignment (expression has type "Invariant1[float]", variable has type "Invariant1[int]")
377
378 class Invariant2[T]:
379 def __init__(self, x: T) -> None:
380 # Dunders are special
381 self.__x__ = x
382
383 @property
384 def x(self) -> T:
385 return self.__x__
386
387 inv2_1: Invariant2[float] = Invariant2[int](1) # E: Incompatible types in assignment (expression has type "Invariant2[int]", variable has type "Invariant2[float]")
388 inv2_2: Invariant2[int] = Invariant2[float](1) # E: Incompatible types in assignment (expression has type "Invariant2[float]", variable has type "Invariant2[int]")
389
390 class Invariant3[T]:
391 def __init__(self, x: T) -> None:
392 self._x = Invariant1(x)
393
394 @property
395 def x(self) -> T:
396 return self._x._x
397
398 inv3_1: Invariant3[float] = Invariant3[int](1) # E: Incompatible types in assignment (expression has type "Invariant3[int]", variable has type "Invariant3[float]")
399 inv3_2: Invariant3[int] = Invariant3[float](1) # E: Incompatible types in assignment (expression has type "Invariant3[float]", variable has type "Invariant3[int]")
400 [builtins fixtures/property.pyi]
401
402 [case testPEP695InferVarianceWithInheritedSelf]
403 from typing import overload, Self, TypeVar, Generic
404
405 T = TypeVar("T")
406 S = TypeVar("S")
407
408 class C(Generic[T]):
409 def f(self, x: T) -> Self: ...
410 def g(self) -> T: ...
411
412 class D[T1, T2](C[T1]):
413 def m(self, x: T2) -> None: ...
414
415 a1: D[int, int] = D[int, object]()
416 a2: D[int, object] = D[int, int]() # E: Incompatible types in assignment (expression has type "D[int, int]", variable has type "D[int, object]")
417 a3: D[int, int] = D[object, object]() # E: Incompatible types in assignment (expression has type "D[object, object]", variable has type "D[int, int]")
418 a4: D[object, int] = D[int, object]() # E: Incompatible types in assignment (expression has type "D[int, object]", variable has type "D[object, int]")
419
420 [case testPEP695InferVarianceWithReturnSelf]
421 from typing import Self, overload
422
423 class Cov[T]:
424 def f(self) -> Self: ...
425
426 a1: Cov[int] = Cov[float]() # E: Incompatible types in assignment (expression has type "Cov[float]", variable has type "Cov[int]")
427 a2: Cov[float] = Cov[int]()
428
429 class Contra[T]:
430 def f(self) -> Self: ...
431 def g(self, x: T) -> None: ...
432
433 b1: Contra[int] = Contra[float]()
434 b2: Contra[float] = Contra[int]() # E: Incompatible types in assignment (expression has type "Contra[int]", variable has type "Contra[float]")
435
436 class Cov2[T]:
437 @overload
438 def f(self, x): ...
439 @overload
440 def f(self) -> Self: ...
441 def f(self, x=None): ...
442
443 c1: Cov2[int] = Cov2[float]() # E: Incompatible types in assignment (expression has type "Cov2[float]", variable has type "Cov2[int]")
444 c2: Cov2[float] = Cov2[int]()
445
446 class Contra2[T]:
447 @overload
448 def f(self, x): ...
449 @overload
450 def f(self) -> Self: ...
451 def f(self, x=None): ...
452
453 def g(self, x: T) -> None: ...
454
455 d1: Contra2[int] = Contra2[float]()
456 d2: Contra2[float] = Contra2[int]() # E: Incompatible types in assignment (expression has type "Contra2[int]", variable has type "Contra2[float]")
457
458 [case testPEP695InheritInvariant]
459 class Invariant[T]:
460 x: T
461
462 class Subclass[T](Invariant[T]):
463 pass
464
465 x: Invariant[int]
466 y: Invariant[object]
467 if int():
468 x = y # E: Incompatible types in assignment (expression has type "Invariant[object]", variable has type "Invariant[int]")
469 if int():
470 y = x # E: Incompatible types in assignment (expression has type "Invariant[int]", variable has type "Invariant[object]")
471
472 a: Subclass[int]
473 b: Subclass[object]
474 if int():
475 a = b # E: Incompatible types in assignment (expression has type "Subclass[object]", variable has type "Subclass[int]")
476 if int():
477 b = a # E: Incompatible types in assignment (expression has type "Subclass[int]", variable has type "Subclass[object]")
478
479 [case testPEP695InheritanceMakesInvariant]
480 class Covariant[T]:
481 def f(self) -> T:
482 ...
483
484 class Subclass[T](Covariant[list[T]]):
485 pass
486
487 x: Covariant[int] = Covariant[object]() # E: Incompatible types in assignment (expression has type "Covariant[object]", variable has type "Covariant[int]")
488 y: Covariant[object] = Covariant[int]()
489
490 a: Subclass[int] = Subclass[object]() # E: Incompatible types in assignment (expression has type "Subclass[object]", variable has type "Subclass[int]")
491 b: Subclass[object] = Subclass[int]() # E: Incompatible types in assignment (expression has type "Subclass[int]", variable has type "Subclass[object]")
492
493 [case testPEP695InheritCoOrContravariant]
494 class Contravariant[T]:
495 def f(self, x: T) -> None: pass
496
497 class CovSubclass[T](Contravariant[T]):
498 pass
499
500 a: CovSubclass[int] = CovSubclass[object]()
501 b: CovSubclass[object] = CovSubclass[int]() # E: Incompatible types in assignment (expression has type "CovSubclass[int]", variable has type "CovSubclass[object]")
502
503 class Covariant[T]:
504 def f(self) -> T: ...
505
506 class CoSubclass[T](Covariant[T]):
507 pass
508
509 c: CoSubclass[int] = CoSubclass[object]() # E: Incompatible types in assignment (expression has type "CoSubclass[object]", variable has type "CoSubclass[int]")
510 d: CoSubclass[object] = CoSubclass[int]()
511
512 class InvSubclass[T](Covariant[T]):
513 def g(self, x: T) -> None: pass
514
515 e: InvSubclass[int] = InvSubclass[object]() # E: Incompatible types in assignment (expression has type "InvSubclass[object]", variable has type "InvSubclass[int]")
516 f: InvSubclass[object] = InvSubclass[int]() # E: Incompatible types in assignment (expression has type "InvSubclass[int]", variable has type "InvSubclass[object]")
517
518 [case testPEP695FinalAttribute]
519 from typing import Final
520
521 class C[T]:
522 def __init__(self, x: T) -> None:
523 self.x: Final = x
524
525 a: C[int] = C[object](1) # E: Incompatible types in assignment (expression has type "C[object]", variable has type "C[int]")
526 b: C[object] = C[int](1)
527
528 [case testPEP695TwoTypeVariables]
529 class C[T, S]:
530 def f(self, x: T) -> None: ...
531 def g(self) -> S: ...
532
533 a: C[int, int] = C[object, int]()
534 b: C[object, int] = C[int, int]() # E: Incompatible types in assignment (expression has type "C[int, int]", variable has type "C[object, int]")
535 c: C[int, int] = C[int, object]() # E: Incompatible types in assignment (expression has type "C[int, object]", variable has type "C[int, int]")
536 d: C[int, object] = C[int, int]()
537
538 [case testPEP695Properties]
539 class R[T]:
540 @property
541 def p(self) -> T: ...
542
543 class RW[T]:
544 @property
545 def p(self) -> T: ...
546 @p.setter
547 def p(self, x: T) -> None: ...
548
549 a: R[int] = R[object]() # E: Incompatible types in assignment (expression has type "R[object]", variable has type "R[int]")
550 b: R[object] = R[int]()
551 c: RW[int] = RW[object]() # E: Incompatible types in assignment (expression has type "RW[object]", variable has type "RW[int]")
552 d: RW[object] = RW[int]() # E: Incompatible types in assignment (expression has type "RW[int]", variable has type "RW[object]")
553 [builtins fixtures/property.pyi]
554
555 [case testPEP695Protocol]
556 from typing import Protocol
557
558 class PContra[T](Protocol):
559 def f(self, x: T) -> None: ...
560
561 PContra() # E: Cannot instantiate protocol class "PContra"
562 a: PContra[int]
563 b: PContra[object]
564 if int():
565 a = b
566 if int():
567 b = a # E: Incompatible types in assignment (expression has type "PContra[int]", variable has type "PContra[object]")
568
569 class PCov[T](Protocol):
570 def f(self) -> T: ...
571
572 PCov() # E: Cannot instantiate protocol class "PCov"
573 c: PCov[int]
574 d: PCov[object]
575 if int():
576 c = d # E: Incompatible types in assignment (expression has type "PCov[object]", variable has type "PCov[int]")
577 if int():
578 d = c
579
580 class PInv[T](Protocol):
581 def f(self, x: T) -> T: ...
582
583 PInv() # E: Cannot instantiate protocol class "PInv"
584 e: PInv[int]
585 f: PInv[object]
586 if int():
587 e = f # E: Incompatible types in assignment (expression has type "PInv[object]", variable has type "PInv[int]")
588 if int():
589 f = e # E: Incompatible types in assignment (expression has type "PInv[int]", variable has type "PInv[object]")
590
591 [case testPEP695TypeAlias]
592 class C[T]: pass
593 class D[T, S]: pass
594
595 type A[S] = C[S]
596
597 a: A[int]
598 reveal_type(a) # N: Revealed type is "__main__.C[builtins.int]"
599
600 type A2[T] = C[C[T]]
601 a2: A2[str]
602 reveal_type(a2) # N: Revealed type is "__main__.C[__main__.C[builtins.str]]"
603
604 type A3[T, S] = D[S, C[T]]
605 a3: A3[int, str]
606 reveal_type(a3) # N: Revealed type is "__main__.D[builtins.str, __main__.C[builtins.int]]"
607
608 type A4 = int | str
609 a4: A4
610 reveal_type(a4) # N: Revealed type is "builtins.int | builtins.str"
611 [builtins fixtures/type.pyi]
612
613 [case testPEP695TypeAliasNotValidAsBaseClass]
614 from typing import TypeAlias
615
616 import m
617
618 type A1 = int
619 class Bad1(A1): # E: Type alias defined using "type" statement not valid as base class
620 pass
621
622 type A2[T] = list[T]
623 class Bad2(A2[int]): # E: Type alias defined using "type" statement not valid as base class
624 pass
625
626 class Bad3(m.A1): # E: Type alias defined using "type" statement not valid as base class
627 pass
628
629 class Bad4(m.A2[int]): # E: Type alias defined using "type" statement not valid as base class
630 pass
631
632 B1 = int
633 B2 = list
634 B3: TypeAlias = int
635 class Good1(B1): pass
636 class Good2(B2[int]): pass
637 class Good3(list[A1]): pass
638 class Good4(list[A2[int]]): pass
639 class Good5(B3): pass
640
641 [file m.py]
642 type A1 = str
643 type A2[T] = list[T]
644 [typing fixtures/typing-medium.pyi]
645
646 [case testPEP695TypeAliasWithUnusedTypeParams]
647 type A[T] = int
648 a: A[str]
649 reveal_type(a) # N: Revealed type is "builtins.int"
650
651 [case testPEP695TypeAliasForwardReference1]
652 type A[T] = C[T]
653
654 a: A[int]
655 reveal_type(a) # N: Revealed type is "__main__.C[builtins.int]"
656
657 class C[T]: pass
658
659 [case testPEP695TypeAliasForwardReference2]
660 type X = C
661 type A = X
662
663 a: A
664 reveal_type(a) # N: Revealed type is "__main__.C"
665
666 class C: pass
667 [builtins fixtures/tuple.pyi]
668 [typing fixtures/typing-full.pyi]
669
670 [case testPEP695TypeAliasForwardReference3]
671 type X = D
672 type A = C[X]
673
674 a: A
675 reveal_type(a) # N: Revealed type is "__main__.C[__main__.D]"
676
677 class C[T]: pass
678 class D: pass
679
680 [case testPEP695TypeAliasForwardReference4]
681 type A = C
682
683 class D(A): # E: Type alias defined using "type" statement not valid as base class
684 pass
685
686 class C: pass
687
688 x: C = D()
689 y: D = C() # E: Incompatible types in assignment (expression has type "C", variable has type "D")
690
691 [case testPEP695TypeAliasForwardReference5]
692 type A = str
693 type B[T] = C[T]
694 class C[T]: pass
695 a: A
696 b: B[int]
697 c: C[str]
698 reveal_type(a) # N: Revealed type is "builtins.str"
699 reveal_type(b) # N: Revealed type is "__main__.C[builtins.int]"
700 reveal_type(c) # N: Revealed type is "__main__.C[builtins.str]"
701
702 [case testPEP695TypeAliasWithUndefineName]
703 type A[T] = XXX # E: Name "XXX" is not defined
704 a: A[int]
705 reveal_type(a) # N: Revealed type is "Any"
706
707 [case testPEP695TypeAliasInvalidType]
708 type A = int | 1 # E: Invalid type: try using Literal[1] instead?
709
710 a: A
711 reveal_type(a) # N: Revealed type is "builtins.int | Any"
712 type B = int + str # E: Invalid type alias: expression is not a valid type
713 b: B
714 reveal_type(b) # N: Revealed type is "Any"
715 [builtins fixtures/type.pyi]
716
717 [case testPEP695TypeAliasBoundForwardReference]
718 type B[T: Foo] = list[T]
719 class Foo: pass
720
721 [case testPEP695UpperBound]
722 class D:
723 x: int
724 class E(D): pass
725
726 class C[T: D]: pass
727
728 a: C[D]
729 b: C[E]
730 reveal_type(a) # N: Revealed type is "__main__.C[__main__.D]"
731 reveal_type(b) # N: Revealed type is "__main__.C[__main__.E]"
732
733 c: C[int] # E: Type argument "int" of "C" must be a subtype of "D"
734
735 def f[T: D](a: T) -> T:
736 reveal_type(a.x) # N: Revealed type is "builtins.int"
737 return a
738
739 reveal_type(f(D())) # N: Revealed type is "__main__.D"
740 reveal_type(f(E())) # N: Revealed type is "__main__.E"
741 f(1) # E: Value of type variable "T" of "f" cannot be "int"
742
743 [case testPEP695UpperBoundForwardReference1]
744 class C[T: D]: pass
745
746 a: C[D]
747 b: C[E]
748 reveal_type(a) # N: Revealed type is "__main__.C[__main__.D]"
749 reveal_type(b) # N: Revealed type is "__main__.C[__main__.E]"
750
751 c: C[int] # E: Type argument "int" of "C" must be a subtype of "D"
752
753 class D: pass
754 class E(D): pass
755
756 [case testPEP695UpperBoundForwardReference2]
757 type A = D
758 class C[T: A]: pass
759
760 class D: pass
761 class E(D): pass
762
763 a: C[D]
764 b: C[E]
765 reveal_type(a) # N: Revealed type is "__main__.C[__main__.D]"
766 reveal_type(b) # N: Revealed type is "__main__.C[__main__.E]"
767
768 c: C[int] # E: Type argument "int" of "C" must be a subtype of "D"
769
770 [case testPEP695UpperBoundForwardReference3]
771 class D[T]: pass
772 class E[T](D[T]): pass
773
774 type A = D[X]
775
776 class C[T: A]: pass
777
778 class X: pass
779
780 a: C[D[X]]
781 b: C[E[X]]
782 reveal_type(a) # N: Revealed type is "__main__.C[__main__.D[__main__.X]]"
783 reveal_type(b) # N: Revealed type is "__main__.C[__main__.E[__main__.X]]"
784
785 c: C[D[int]] # E: Type argument "D[int]" of "C" must be a subtype of "D[X]"
786
787 [case testPEP695UpperBoundForwardReference4]
788 def f[T: D](a: T) -> T:
789 reveal_type(a.x) # N: Revealed type is "builtins.int"
790 return a
791
792 class D:
793 x: int
794 class E(D): pass
795
796 reveal_type(f(D())) # N: Revealed type is "__main__.D"
797 reveal_type(f(E())) # N: Revealed type is "__main__.E"
798 f(1) # E: Value of type variable "T" of "f" cannot be "int"
799
800 [case testPEP695UpperBoundUndefinedName]
801 class C[T: XX]: # E: Name "XX" is not defined
802 pass
803
804 a: C[int]
805
806 def f[T: YY](x: T) -> T: # E: Name "YY" is not defined
807 return x
808 reveal_type(f) # N: Revealed type is "def [T <: Any] (x: T`-1) -> T`-1"
809
810 [case testPEP695UpperBoundWithMultipleParams]
811 class C[T, S: int]: pass
812 class D[A: int, B]: pass
813
814 def f[T: int, S: int | str](x: T, y: S) -> T | S:
815 return x
816
817 C[str, int]()
818 C[str, str]() # E: Value of type variable "S" of "C" cannot be "str"
819 D[int, str]()
820 D[str, str]() # E: Value of type variable "A" of "D" cannot be "str"
821 f(1, 1)
822 u: int | str
823 f(1, u)
824 f('x', None) # E: Value of type variable "T" of "f" cannot be "str" \
825 # E: Value of type variable "S" of "f" cannot be "None"
826
827 [case testPEP695UpperBoundTypeTypeConstructorReturnType]
828 class A:
829 def __init__(self, value: str = "") -> None: pass
830 class B:
831 def __init__(self, value: str = "") -> None: pass
832
833 def make[T: A | B](ftype: type[T], value: str | None) -> T:
834 if value is None:
835 return ftype()
836 return ftype(value)
837
838 reveal_type(make(A, "a")) # N: Revealed type is "__main__.A"
839 reveal_type(make(B, None)) # N: Revealed type is "__main__.B"
840
841 [case testPEP695InferVarianceOfTupleType]
842 class Cov[T](tuple[int, str]):
843 def f(self) -> T: pass
844
845 class Cov2[T](tuple[T, T]):
846 pass
847
848 class Contra[T](tuple[int, str]):
849 def f(self, x: T) -> None: pass
850
851 a: Cov[object] = Cov[int]()
852 b: Cov[int] = Cov[object]() # E: Incompatible types in assignment (expression has type "Cov[object]", variable has type "Cov[int]")
853
854 c: Cov2[object] = Cov2[int]()
855 d: Cov2[int] = Cov2[object]() # E: Incompatible types in assignment (expression has type "Cov2[object]", variable has type "Cov2[int]")
856
857 e: Contra[int] = Contra[object]()
858 f: Contra[object] = Contra[int]() # E: Incompatible types in assignment (expression has type "Contra[int]", variable has type "Contra[object]")
859 [builtins fixtures/tuple-simple.pyi]
860
861 [case testPEP695ValueRestriction]
862 def f[T: (int, str)](x: T) -> T:
863 reveal_type(x) # N: Revealed type is "builtins.int" \
864 # N: Revealed type is "builtins.str"
865 return x
866
867 reveal_type(f(1)) # N: Revealed type is "builtins.int"
868 reveal_type(f('x')) # N: Revealed type is "builtins.str"
869 f(None) # E: Value of type variable "T" of "f" cannot be "None"
870
871 class C[T: (object, None)]: pass
872
873 a: C[object]
874 b: C[None]
875 c: C[int] # E: Value of type variable "T" of "C" cannot be "int"
876
877 [case testPEP695ValueRestrictionForwardReference]
878 class C[T: (int, D)]:
879 def __init__(self, x: T) -> None:
880 a = x
881 if int():
882 a = 'x' # E: Incompatible types in assignment (expression has type "str", variable has type "int") \
883 # E: Incompatible types in assignment (expression has type "str", variable has type "D")
884 self.x: T = x
885
886 reveal_type(C(1).x) # N: Revealed type is "builtins.int"
887 C(None) # E: Value of type variable "T" of "C" cannot be "None"
888
889 class D: pass
890
891 C(D())
892
893 [case testPEP695ValueRestrictionUndefinedName]
894 class C[T: (int, XX)]: # E: Name "XX" is not defined
895 pass
896
897 def f[S: (int, YY)](x: S) -> S: # E: Name "YY" is not defined
898 return x
899
900 [case testPEP695ParamSpec]
901 from typing import Callable
902
903 def g[**P](f: Callable[P, None], *args: P.args, **kwargs: P.kwargs) -> None:
904 f(*args, **kwargs)
905 f(1, *args, **kwargs) # E: Argument 1 has incompatible type "int"; expected "P.args"
906
907 def h(x: int, y: str) -> None: pass
908
909 g(h, 1, y='x')
910 g(h, 1, x=1) # E: "g" gets multiple values for keyword argument "x" \
911 # E: Missing positional argument "y" in call to "g"
912
913 class C[**P, T]:
914 def m(self, *args: P.args, **kwargs: P.kwargs) -> T: ...
915
916 a: C[[int, str], None]
917 reveal_type(a) # N: Revealed type is "__main__.C[[builtins.int, builtins.str], None]"
918 reveal_type(a.m) # N: Revealed type is "def (builtins.int, builtins.str)"
919 [builtins fixtures/tuple.pyi]
920
921 [case testPEP695ParamSpecTypeAlias]
922 from typing import Callable
923
924 type C[**P] = Callable[P, int]
925
926 f: C[[str, int | None]]
927 reveal_type(f) # N: Revealed type is "def (builtins.str, builtins.int | None) -> builtins.int"
928 [builtins fixtures/tuple.pyi]
929 [typing fixtures/typing-full.pyi]
930
931 [case testPEP695TypeVarTuple]
932 def f[*Ts](t: tuple[*Ts]) -> tuple[*Ts]:
933 reveal_type(t) # N: Revealed type is "tuple[Unpack[Ts`-1]]"
934 return t
935
936 reveal_type(f((1, 'x'))) # N: Revealed type is "tuple[Literal[1]?, Literal['x']?]"
937 a: tuple[int, ...]
938 reveal_type(f(a)) # N: Revealed type is "builtins.tuple[builtins.int, ...]"
939
940 class C[T, *Ts]:
941 pass
942
943 b: C[int, str, None]
944 reveal_type(b) # N: Revealed type is "__main__.C[builtins.int, builtins.str, None]"
945 c: C[str]
946 reveal_type(c) # N: Revealed type is "__main__.C[builtins.str]"
947 b = c # E: Incompatible types in assignment (expression has type "C[str]", variable has type "C[int, str, None]")
948 [builtins fixtures/tuple.pyi]
949
950 [case testPEP695TypeVarTupleAlias]
951 from typing import Callable
952
953 type C[*Ts] = tuple[*Ts, int]
954
955 a: C[str, None]
956 reveal_type(a) # N: Revealed type is "tuple[builtins.str, None, builtins.int]"
957 [builtins fixtures/tuple.pyi]
958
959 [case testPEP695IncrementalFunction]
960 import a
961
962 [file a.py]
963 import b
964
965 [file a.py.2]
966 import b
967 reveal_type(b.f(1))
968 reveal_type(b.g(1, 'x'))
969 b.g('x', 'x')
970 b.g(1, 2)
971
972 [file b.py]
973 def f[T](x: T) -> T:
974 return x
975
976 def g[T: int, S: (str, None)](x: T, y: S) -> T | S:
977 return x
978
979 [out2]
980 tmp/a.py:2: note: Revealed type is "builtins.int"
981 tmp/a.py:3: note: Revealed type is "builtins.int | builtins.str"
982 tmp/a.py:4: error: Value of type variable "T" of "g" cannot be "str"
983 tmp/a.py:5: error: Value of type variable "S" of "g" cannot be "int"
984
985 [case testPEP695IncrementalClass]
986 import a
987
988 [file a.py]
989 import b
990
991 [file a.py.2]
992 from b import C, D
993 x: C[int]
994 reveal_type(x)
995
996 class N(int): pass
997 class SS(str): pass
998
999 y1: D[int, str]
1000 y2: D[N, str]
1001 y3: D[int, None]
1002 y4: D[int, None]
1003 y5: D[int, SS] # Error
1004 y6: D[object, str] # Error
1005
1006 [file b.py]
1007 class C[T]: pass
1008
1009 class D[T: int, S: (str, None)]:
1010 pass
1011
1012 [out2]
1013 tmp/a.py:3: note: Revealed type is "b.C[builtins.int]"
1014 tmp/a.py:12: error: Value of type variable "S" of "D" cannot be "SS"
1015 tmp/a.py:13: error: Type argument "object" of "D" must be a subtype of "int"
1016
1017 [case testPEP695IncrementalParamSpecAndTypeVarTuple]
1018 import a
1019
1020 [file a.py]
1021 import b
1022
1023 [file a.py.2]
1024 from b import C, D
1025 x1: C[()]
1026 x2: C[int]
1027 x3: C[int, str]
1028 y: D[[int, str]]
1029 reveal_type(y.m)
1030
1031 [file b.py]
1032 class C[*Ts]: pass
1033 class D[**P]:
1034 def m(self, *args: P.args, **kwargs: P.kwargs) -> None: pass
1035
1036 [builtins fixtures/tuple.pyi]
1037 [out2]
1038 tmp/a.py:6: note: Revealed type is "def (builtins.int, builtins.str)"
1039
1040 [case testPEP695IncrementalTypeAlias]
1041 import a
1042
1043 [file a.py]
1044 import b
1045
1046 [file a.py.2]
1047 from b import A, B
1048 a: A
1049 reveal_type(a)
1050 b: B[int]
1051 reveal_type(b)
1052
1053 [file b.py]
1054 type A = str
1055 class Foo[T]: pass
1056 type B[T] = Foo[T]
1057
1058 [builtins fixtures/tuple.pyi]
1059 [out2]
1060 tmp/a.py:3: note: Revealed type is "builtins.str"
1061 tmp/a.py:5: note: Revealed type is "b.Foo[builtins.int]"
1062
1063 [case testPEP695UndefinedNameInGenericFunction]
1064 def f[T](x: T) -> T:
1065 return unknown() # E: Name "unknown" is not defined
1066
1067 class C:
1068 def m[T](self, x: T) -> T:
1069 return unknown() # E: Name "unknown" is not defined
1070
1071 [case testPEP695FunctionTypeVarAccessInFunction]
1072 from typing import cast
1073
1074 class C:
1075 def m[T](self, x: T) -> T:
1076 y: T = x
1077 reveal_type(y) # N: Revealed type is "T`-1"
1078 return cast(T, y)
1079
1080 reveal_type(C().m(1)) # N: Revealed type is "builtins.int"
1081
1082 [case testPEP695ScopingBasics]
1083 T = 1
1084
1085 def f[T](x: T) -> T:
1086 T = 'a'
1087 reveal_type(T) # N: Revealed type is "builtins.str"
1088 return x
1089
1090 reveal_type(T) # N: Revealed type is "builtins.int"
1091
1092 class C[T]:
1093 T = 1.2
1094 reveal_type(T) # N: Revealed type is "builtins.float"
1095
1096 reveal_type(T) # N: Revealed type is "builtins.int"
1097
1098 [case testPEP695ClassScoping]
1099 class C:
1100 class D: pass
1101
1102 def m[T: D](self, x: T, y: D) -> T:
1103 return x
1104
1105 C().m(C.D(), C.D())
1106 C().m(1, C.D()) # E: Value of type variable "T" of "m" of "C" cannot be "int"
1107
1108 [case testPEP695NestedGenericFunction]
1109 def f[T](x: T) -> T:
1110 reveal_type(f(x)) # N: Revealed type is "T`-1"
1111 reveal_type(f(1)) # N: Revealed type is "builtins.int"
1112
1113 def ff(x: T) -> T:
1114 y: T = x
1115 return y
1116 reveal_type(ff(x)) # N: Revealed type is "T`-1"
1117 ff(1) # E: Argument 1 to "ff" has incompatible type "int"; expected "T"
1118
1119 def g[S](a: S) -> S:
1120 ff(a) # E: Argument 1 to "ff" has incompatible type "S"; expected "T"
1121 return a
1122 reveal_type(g(1)) # N: Revealed type is "builtins.int"
1123 reveal_type(g(x)) # N: Revealed type is "T`-1"
1124
1125 def h[S](a: S) -> S:
1126 return a
1127 reveal_type(h(1)) # N: Revealed type is "builtins.int"
1128 reveal_type(h(x)) # N: Revealed type is "T`-1"
1129 return x
1130
1131 [case testPEP695NonLocalAndGlobal]
1132 def f() -> None:
1133 T = 1
1134 def g[T](x: T) -> T:
1135 nonlocal T # E: nonlocal binding not allowed for type parameter "T"
1136 T = 'x' # E: "T" is a type variable and only valid in type context
1137 return x
1138 reveal_type(T) # N: Revealed type is "builtins.int"
1139
1140 def g() -> None:
1141 a = 1
1142 def g[T](x: T) -> T:
1143 nonlocal a
1144 a = 'x' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
1145 return x
1146
1147 x = 1
1148
1149 def h[T](a: T) -> T:
1150 global x
1151 x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
1152 return a
1153
1154 class C[T]:
1155 def m[S](self, a: S) -> S:
1156 global x
1157 x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
1158 return a
1159
1160 [case testPEP695ParameterDefault]
1161 from typing import cast
1162
1163 def f[T](
1164 x: T =
1165 T # E: Name "T" is not defined \
1166 # E: Incompatible default for parameter "x" (default has type "TypeVar", parameter has type "T")
1167 ) -> T:
1168 return x
1169
1170 def g[T](x: T = cast(T, None)) -> T: # E: Name "T" is not defined
1171 return x
1172
1173 class C:
1174 def m[T](self, x: T = cast(T, None)) -> T: # E: Name "T" is not defined
1175 return x
1176 [builtins fixtures/tuple.pyi]
1177 [typing fixtures/typing-full.pyi]
1178
1179 [case testPEP695ListComprehension]
1180 from typing import cast
1181
1182 def f[T](x: T) -> T:
1183 b = [cast(T, a) for a in [1, 2]]
1184 reveal_type(b) # N: Revealed type is "builtins.list[T`-1]"
1185 return x
1186
1187 [case testPEP695ReuseNameInSameScope]
1188 class C[T]:
1189 def m[S](self, x: S, y: T) -> S | T:
1190 return x
1191
1192 def m2[S](self, x: S, y: T) -> S | T:
1193 return x
1194
1195 class D[T]:
1196 pass
1197
1198 def f[T](x: T) -> T:
1199 return x
1200
1201 def g[T](x: T) -> T:
1202 def nested[S](y: S) -> S:
1203 return y
1204 def nested2[S](y: S) -> S:
1205 return y
1206 return x
1207
1208 [case testPEP695NestedScopingSpecialCases]
1209 # This is adapted from PEP 695
1210 S = 0
1211
1212 def outer1[S]() -> None:
1213 S = 1
1214 T = 1
1215
1216 def outer2[T]() -> None:
1217 def inner1() -> None:
1218 nonlocal S
1219 nonlocal T # E: nonlocal binding not allowed for type parameter "T"
1220
1221 def inner2() -> None:
1222 global S
1223
1224 [case testPEP695ScopingWithBaseClasses]
1225 # This is adapted from PEP 695
1226 class Outer:
1227 class Private:
1228 pass
1229
1230 # If the type parameter scope was like a traditional scope,
1231 # the base class 'Private' would not be accessible here.
1232 class Inner[T](Private, list[T]):
1233 pass
1234
1235 # Likewise, 'Inner' would not be available in these type annotations.
1236 def method1[T](self, a: Inner[T]) -> Inner[T]:
1237 return a
1238
1239 [case testPEP695RedefineTypeParameterInScope]
1240 class C[T]:
1241 def m[T](self, x: T) -> T: # E: "T" already defined as a type parameter
1242 return x
1243 def m2(self) -> None:
1244 def nested[T](x: T) -> T: # E: "T" already defined as a type parameter
1245 return x
1246
1247 def f[S, S](x: S) -> S: # E: "S" already defined as a type parameter
1248 return x
1249
1250 [case testPEP695TypeVarNameClashNoCrashForwardReference]
1251 # https://github.com/python/mypy/issues/18507
1252 from typing import TypeVar
1253 T = TypeVar("T", bound=Foo) # E: Name "Foo" is used before definition
1254
1255 class Foo: ...
1256 class Bar[T]: ...
1257
1258 [case testPEP695TypeVarNameClashNoCrashDeferredSymbol]
1259 # https://github.com/python/mypy/issues/19526
1260 T = Unknown # E: Name "Unknown" is not defined
1261
1262 class Foo[T]: ...
1263 class Bar[*T]: ...
1264 class Baz[**T]: ...
1265 [builtins fixtures/tuple.pyi]
1266
1267 [case testPEP695TypeVarNameClashTypeAlias]
1268 type Tb = object
1269 type Ta[Tb] = 'B[Tb]'
1270 class A[Ta]: ...
1271 class B[Tb](A[Ta]): ...
1272
1273 [case testPEP695TypeVarNameClashStarImport]
1274 # Similar to
1275 # https://github.com/python/mypy/issues/19946
1276 import a
1277
1278 [file a.py]
1279 from b import *
1280 class Foo[T]: ...
1281
1282 [file b.py]
1283 from a import *
1284 class Bar[T]: ...
1285 [builtins fixtures/tuple.pyi]
1286
1287 [case testPEP695ClassDecorator]
1288 from typing import Any
1289
1290 T = 0
1291
1292 def decorator(x: str) -> Any: ...
1293
1294 @decorator(T) # E: Argument 1 to "decorator" has incompatible type "int"; expected "str"
1295 class C[T]:
1296 pass
1297
1298 [case testPEP695RecursiceTypeAlias]
1299 type A = str | list[A]
1300 a: A
1301 reveal_type(a) # N: Revealed type is "builtins.str | builtins.list[...]"
1302
1303 class C[T]: pass
1304
1305 type B[T] = C[T] | list[B[T]]
1306 b: B[int]
1307 reveal_type(b) # N: Revealed type is "__main__.C[builtins.int] | builtins.list[...]"
1308 [builtins fixtures/type.pyi]
1309
1310 [case testPEP695BadRecursiveTypeAlias]
1311 type A = A # E: Cannot resolve name "A" (possible cyclic definition)
1312 type B = B | int # E: Invalid recursive alias: a union item of itself
1313 a: A
1314 reveal_type(a) # N: Revealed type is "Any"
1315 b: B
1316 reveal_type(b) # N: Revealed type is "Any"
1317 [builtins fixtures/type.pyi]
1318 [typing fixtures/typing-full.pyi]
1319
1320 [case testPEP695RecursiveTypeAliasForwardReference]
1321 def f(a: A) -> None:
1322 if isinstance(a, str):
1323 reveal_type(a) # N: Revealed type is "builtins.str"
1324 else:
1325 reveal_type(a) # N: Revealed type is "__main__.C[builtins.str | __main__.C[...]]"
1326
1327 type A = str | C[A]
1328
1329 class C[T]: pass
1330
1331 f('x')
1332 f(C[str]())
1333 f(C[C[str]]())
1334 f(1) # E: Argument 1 to "f" has incompatible type "int"; expected "A"
1335 f(C[int]()) # E: Argument 1 to "f" has incompatible type "C[int]"; expected "A"
1336 [builtins fixtures/isinstance.pyi]
1337
1338 [case testPEP695InvalidGenericOrProtocolBaseClass]
1339 from typing import Generic, Protocol, TypeVar
1340
1341 S = TypeVar("S")
1342
1343 class C[T](Generic[T]): # E: Generic[...] base class is redundant
1344 pass
1345 class C2[T](Generic[S]): # E: Generic[...] base class is redundant
1346 pass
1347
1348 a: C[int]
1349 b: C2[int, str]
1350
1351 class P[T](Protocol[T]): # E: No arguments expected for "Protocol" base class
1352 pass
1353 class P2[T](Protocol[S]): # E: No arguments expected for "Protocol" base class
1354 pass
1355
1356 [case testPEP695CannotUseTypeVarFromOuterClass]
1357 class ClassG[V]:
1358 # This used to crash
1359 class ClassD[T: dict[str, V]]: # E: Name "V" is not defined
1360 ...
1361 [builtins fixtures/dict.pyi]
1362
1363 [case testPEP695MixNewAndOldStyleGenerics]
1364 from typing import TypeVar
1365
1366 S = TypeVar("S")
1367 U = TypeVar("U")
1368
1369 def f[T](x: T, y: S) -> T | S: ... # E: All type parameters should be declared ("S" not declared)
1370 def g[T](x: S, y: U) -> T | S | U: ... # E: All type parameters should be declared ("S", "U" not declared)
1371
1372 def h[S: int](x: S) -> S:
1373 a: int = x
1374 return x
1375
1376 class C[T]:
1377 def m[X, S](self, x: S, y: U) -> X | S | U: ... # E: All type parameters should be declared ("U" not declared)
1378 def m2(self, x: T, y: S) -> T | S: ...
1379
1380 class D[T](C[S]): # E: All type parameters should be declared ("S" not declared)
1381 pass
1382
1383 [case testPEP695MixNewAndOldStyleTypeVarTupleAndParamSpec]
1384 from typing import TypeVarTuple, ParamSpec, Callable
1385 Ts = TypeVarTuple("Ts")
1386 P = ParamSpec("P")
1387
1388 def f[T](x: T, f: Callable[P, None] # E: All type parameters should be declared ("P" not declared)
1389 ) -> Callable[P, T]: ...
1390 def g[T](x: T, f: tuple[*Ts] # E: All type parameters should be declared ("Ts" not declared)
1391 ) -> tuple[T, *Ts]: ...
1392 [builtins fixtures/tuple.pyi]
1393
1394 [case testPEP695MixNewAndOldStyleGenericsInTypeAlias]
1395 from typing import TypeVar, ParamSpec, TypeVarTuple, Callable
1396
1397 T = TypeVar("T")
1398 Ts = TypeVarTuple("Ts")
1399 P = ParamSpec("P")
1400
1401 type A = list[T] # E: All type parameters should be declared ("T" not declared)
1402 a: A[int] # E: Bad number of arguments for type alias, expected 0, given 1
1403 reveal_type(a) # N: Revealed type is "builtins.list[Any]"
1404
1405 type B = tuple[*Ts] # E: All type parameters should be declared ("Ts" not declared)
1406 type C = Callable[P, None] # E: All type parameters should be declared ("P" not declared)
1407 [builtins fixtures/tuple.pyi]
1408 [typing fixtures/typing-full.pyi]
1409
1410 [case testPEP695NonGenericAliasToGenericClass]
1411 class C[T]: pass
1412 type A = C
1413 x: C
1414 y: A
1415 reveal_type(x) # N: Revealed type is "__main__.C[Any]"
1416 reveal_type(y) # N: Revealed type is "__main__.C[Any]"
1417 z: A[int] # E: Bad number of arguments for type alias, expected 0, given 1
1418
1419 [case testPEP695SelfType]
1420 from typing import Self
1421
1422 class C:
1423 @classmethod
1424 def m[T](cls, x: T) -> tuple[Self, T]:
1425 return cls(), x
1426
1427 class D(C):
1428 pass
1429
1430 reveal_type(C.m(1)) # N: Revealed type is "tuple[__main__.C, builtins.int]"
1431 reveal_type(D.m(1)) # N: Revealed type is "tuple[__main__.D, builtins.int]"
1432
1433 class E[T]:
1434 def m(self) -> Self:
1435 return self
1436
1437 def mm[S](self, x: S) -> tuple[Self, S]:
1438 return self, x
1439
1440 class F[T](E[T]):
1441 pass
1442
1443 reveal_type(E[int]().m()) # N: Revealed type is "__main__.E[builtins.int]"
1444 reveal_type(E[int]().mm(b'x')) # N: Revealed type is "tuple[__main__.E[builtins.int], builtins.bytes]"
1445 reveal_type(F[str]().m()) # N: Revealed type is "__main__.F[builtins.str]"
1446 reveal_type(F[str]().mm(b'x')) # N: Revealed type is "tuple[__main__.F[builtins.str], builtins.bytes]"
1447 [builtins fixtures/tuple.pyi]
1448
1449 [case testPEP695CallAlias]
1450 class C:
1451 def __init__(self, x: str) -> None: ...
1452 type A = C
1453
1454 class D[T]: pass
1455 type B[T] = D[T]
1456
1457 reveal_type(A) # N: Revealed type is "typing.TypeAliasType"
1458 reveal_type(B) # N: Revealed type is "typing.TypeAliasType"
1459 reveal_type(B[int]) # N: Revealed type is "typing.TypeAliasType"
1460
1461 A(1) # E: "TypeAliasType" not callable
1462 B[int]() # E: "TypeAliasType" not callable
1463
1464 A2 = C
1465 B2 = D
1466 A2(1) # E: Argument 1 to "C" has incompatible type "int"; expected "str"
1467 B2[int]()
1468 [builtins fixtures/tuple.pyi]
1469 [typing fixtures/typing-full.pyi]
1470
1471 [case testPEP695IncrementalTypeAliasKinds]
1472 import a
1473
1474 [file a.py]
1475 from b import A
1476
1477 [file a.py.2]
1478 from b import A, B, C
1479 A()
1480 B()
1481 C()
1482
1483 [file b.py]
1484 from typing_extensions import TypeAlias
1485 type A = int
1486 B = int
1487 C: TypeAlias = int
1488 [builtins fixtures/tuple.pyi]
1489 [typing fixtures/typing-full.pyi]
1490 [out2]
1491 tmp/a.py:2: error: "TypeAliasType" not callable
1492
1493 [case testPEP695TypeAliasBoundAndValueChecking]
1494 from typing import Any, cast
1495
1496 class C: pass
1497 class D(C): pass
1498
1499 type A[T: C] = list[T]
1500 a1: A
1501 reveal_type(a1) # N: Revealed type is "builtins.list[Any]"
1502 a2: A[Any]
1503 a3: A[C]
1504 a4: A[D]
1505 a5: A[object] # E: Type argument "object" of "A" must be a subtype of "C"
1506 a6: A[int] # E: Type argument "int" of "A" must be a subtype of "C"
1507
1508 x1 = cast(A[C], a1)
1509 x2 = cast(A[None], a1) # E: Type argument "None" of "A" must be a subtype of "C"
1510
1511 type A2[T: (int, C)] = list[T]
1512 b1: A2
1513 reveal_type(b1) # N: Revealed type is "builtins.list[Any]"
1514 b2: A2[Any]
1515 b3: A2[int]
1516 b4: A2[C]
1517 b5: A2[D] # E: Value of type variable "T" of "A2" cannot be "D"
1518 b6: A2[object] # E: Value of type variable "T" of "A2" cannot be "object"
1519
1520 list[A2[int]]()
1521 list[A2[None]]() # E: Invalid type argument value for "A2"
1522
1523 class N(int): pass
1524
1525 type A3[T: C, S: (int, str)] = T | S
1526 c1: A3[C, int]
1527 c2: A3[D, str]
1528 c3: A3[C, N] # E: Value of type variable "S" of "A3" cannot be "N"
1529 c4: A3[int, str] # E: Type argument "int" of "A3" must be a subtype of "C"
1530 [builtins fixtures/type.pyi]
1531 [typing fixtures/typing-full.pyi]
1532
1533 [case testPEP695TypeAliasInClassBodyOrFunction]
1534 class C:
1535 type A = int
1536 type B[T] = list[T] | None
1537 a: A
1538 b: B[str]
1539
1540 def method(self) -> None:
1541 v: C.A
1542 reveal_type(v) # N: Revealed type is "builtins.int"
1543
1544 reveal_type(C.a) # N: Revealed type is "builtins.int"
1545 reveal_type(C.b) # N: Revealed type is "builtins.list[builtins.str] | None"
1546
1547 C.A = str # E: Incompatible types in assignment (expression has type "type[str]", variable has type "TypeAliasType")
1548
1549 x: C.A
1550 y: C.B[int]
1551 reveal_type(x) # N: Revealed type is "builtins.int"
1552 reveal_type(y) # N: Revealed type is "builtins.list[builtins.int] | None"
1553
1554 def f() -> None:
1555 type A = int
1556 type B[T] = list[T] | None
1557 a: A
1558 reveal_type(a) # N: Revealed type is "builtins.int"
1559
1560 def g() -> None:
1561 b: B[int]
1562 reveal_type(b) # N: Revealed type is "builtins.list[builtins.int] | None"
1563
1564 class D:
1565 def __init__(self) -> None:
1566 type A = int
1567 self.a: A = 0
1568 type B[T] = list[T]
1569 self.b: B[int] = [1]
1570
1571 reveal_type(D().a) # N: Revealed type is "builtins.int"
1572 reveal_type(D().b) # N: Revealed type is "builtins.list[builtins.int]"
1573
1574 class E[T]:
1575 type X = list[T] # E: All type parameters should be declared ("T" not declared)
1576
1577 def __init__(self) -> None:
1578 type A = list[T] # E: All type parameters should be declared ("T" not declared)
1579 self.a: A
1580
1581 reveal_type(E[str]().a) # N: Revealed type is "builtins.list[Any]"
1582 [builtins fixtures/type.pyi]
1583 [typing fixtures/typing-full.pyi]
1584
1585 [case testPEP695TypeAliasInvalidGenericConstraint]
1586 class A[T]:
1587 class a[S: (int, list[T])]: pass # E: Name "T" is not defined
1588 type b[S: (int, list[T])] = S # E: TypeVar constraint type cannot be parametrized by type variables
1589 def c[S: (int, list[T])](self) -> None: ... # E: TypeVar constraint type cannot be parametrized by type variables
1590 [builtins fixtures/tuple.pyi]
1591 [typing fixtures/typing-full.pyi]
1592
1593 [case testPEP695TypeAliasUnboundTypeVarConstraint]
1594 from typing import TypeVar
1595 T = TypeVar("T")
1596 class a[S: (int, list[T])]: pass # E: Type variable "__main__.T" is unbound \
1597 # N: (Hint: Use "Generic[T]" or "Protocol[T]" base class to bind "T" inside a class) \
1598 # N: (Hint: Use "T" in function signature to bind "T" inside a function)
1599 type b[S: (int, list[T])] = S # E: Type variable "__main__.T" is unbound \
1600 # N: (Hint: Use "Generic[T]" or "Protocol[T]" base class to bind "T" inside a class) \
1601 # N: (Hint: Use "T" in function signature to bind "T" inside a function)
1602 def c[S: (int, list[T])](self) -> None: ... # E: Type variable "__main__.T" is unbound \
1603 # N: (Hint: Use "Generic[T]" or "Protocol[T]" base class to bind "T" inside a class) \
1604 # N: (Hint: Use "T" in function signature to bind "T" inside a function)
1605 [builtins fixtures/tuple.pyi]
1606 [typing fixtures/typing-full.pyi]
1607
1608 [case testPEP695RedefineAsTypeAlias1]
1609 class C: pass
1610 type C = int # E: Name "C" already defined on line 1
1611
1612 A = 0
1613 type A = str # E: Name "A" already defined on line 4
1614 reveal_type(A) # N: Revealed type is "builtins.int"
1615
1616 [case testPEP695RedefineAsTypeAlias2]
1617 from m import D
1618 type D = int # E: Name "D" already defined (possibly by an import)
1619 a: D
1620 reveal_type(a) # N: Revealed type is "m.D"
1621 [file m.py]
1622 class D: pass
1623
1624 [case testPEP695RedefineAsTypeAlias3]
1625 D = list["Forward"]
1626 type D = int # E: Name "D" already defined on line 1
1627 Forward = str
1628 x: D
1629 reveal_type(x) # N: Revealed type is "builtins.list[builtins.str]"
1630
1631 [case testPEP695MultiDefinitionsForTypeAlias]
1632 if int():
1633 type A[T] = list[T]
1634 else:
1635 type A[T] = str # E: Name "A" already defined on line 2
1636 x: T # E: Name "T" is not defined
1637 a: A[int]
1638 reveal_type(a) # N: Revealed type is "builtins.list[builtins.int]"
1639
1640 [case testPEP695UndefinedNameInAnnotation]
1641 def f[T](x: foobar, y: T) -> T: ... # E: Name "foobar" is not defined
1642 reveal_type(f) # N: Revealed type is "def [T] (x: Any, y: T`-1) -> T`-1"
1643
1644 [case testPEP695WrongNumberOfConstrainedTypes_no_native_parse]
1645 type A[T: ()] = list[T] # E: Type variable must have at least two constrained types
1646 a: A[int]
1647 reveal_type(a) # N: Revealed type is "builtins.list[builtins.int]"
1648
1649 type B[T: (int,)] = list[T] # E: Type variable must have at least two constrained types
1650 b: B[str]
1651 reveal_type(b) # N: Revealed type is "builtins.list[builtins.str]"
1652
1653 [case testPEP695UsingTypeVariableInOwnBoundOrConstraint]
1654 type A[T: list[T]] = str # E: Name "T" is not defined
1655 type B[S: (list[S], str)] = str # E: Name "S" is not defined
1656 type C[T, S: list[T]] = str # E: Name "T" is not defined
1657
1658 def f[T: T](x: T) -> T: ... # E: Name "T" is not defined
1659 class D[T: T]: # E: Name "T" is not defined
1660 pass
1661
1662 [case testPEP695InvalidType]
1663 def f[T: 1](x: T) -> T: ... # E: Invalid type: try using Literal[1] instead?
1664 class C[T: (int, (1 + 2))]: pass # E: Invalid type comment or annotation
1665 type A = list[1] # E: Invalid type: try using Literal[1] instead?
1666 type B = (1 + 2) # E: Invalid type alias: expression is not a valid type
1667 a: A
1668 reveal_type(a) # N: Revealed type is "builtins.list[Any]"
1669 b: B
1670 reveal_type(b) # N: Revealed type is "Any"
1671
1672 [case testPEP695GenericNamedTuple]
1673 from typing import NamedTuple
1674
1675 # Invariant because of the signature of the generated _replace method
1676 class N[T](NamedTuple):
1677 x: T
1678 y: int
1679
1680 a: N[object]
1681 reveal_type(a.x) # N: Revealed type is "builtins.object"
1682 b: N[int]
1683 reveal_type(b.x) # N: Revealed type is "builtins.int"
1684 if int():
1685 a = b # E: Incompatible types in assignment (expression has type "N[int]", variable has type "N[object]")
1686 if int():
1687 b = a # E: Incompatible types in assignment (expression has type "N[object]", variable has type "N[int]")
1688
1689 class M[T: (int, str)](NamedTuple):
1690 x: T
1691
1692 c: M[int]
1693 d: M[str]
1694 e: M[bool] # E: Value of type variable "T" of "M" cannot be "bool"
1695 [builtins fixtures/tuple.pyi]
1696 [typing fixtures/typing-full.pyi]
1697
1698 [case testPEP695GenericTypedDict]
1699 from typing import TypedDict
1700
1701 class D[T](TypedDict):
1702 x: T
1703 y: int
1704
1705 class E[T: str](TypedDict):
1706 x: T
1707 y: int
1708
1709 a: D[object]
1710 reveal_type(a["x"]) # N: Revealed type is "builtins.object"
1711 b: D[int]
1712 reveal_type(b["x"]) # N: Revealed type is "builtins.int"
1713 c: E[str]
1714 d: E[int] # E: Type argument "int" of "E" must be a subtype of "str"
1715 [builtins fixtures/tuple.pyi]
1716 [typing fixtures/typing-full.pyi]
1717
1718 [case testCurrentClassWorksAsBound]
1719 from typing import Protocol
1720
1721 class Comparable[T: Comparable](Protocol):
1722 def compare(self, other: T) -> bool: ...
1723
1724 class Good:
1725 def compare(self, other: Good) -> bool: ...
1726
1727 x: Comparable[Good]
1728 y: Comparable[int] # E: Type argument "int" of "Comparable" must be a subtype of "Comparable[Any]"
1729
1730 [case testPEP695TypeAliasWithDifferentTargetTypes]
1731 import types # We need GenericAlias from here, and test stubs don't bring in 'types'
1732 from typing import Any, Callable, List, Literal, TypedDict
1733
1734 # Test that various type expressions don't generate false positives as type alias
1735 # values, as they are type checked as expressions. There is a similar test case in
1736 # pythoneval.test that uses typeshed stubs.
1737
1738 class C[T]: pass
1739
1740 class TD(TypedDict):
1741 x: int
1742
1743 type A1 = type[int]
1744 type A2 = type[int] | None
1745 type A3 = None | type[int]
1746 type A4 = type[Any]
1747
1748 type B1[**P, R] = Callable[P, R] | None
1749 type B2[**P, R] = None | Callable[P, R]
1750 type B3 = Callable[[str], int]
1751 type B4 = Callable[..., int]
1752
1753 type C1 = A1 | None
1754 type C2 = None | A1
1755
1756 type D1 = Any | None
1757 type D2 = None | Any
1758
1759 type E1 = List[int]
1760 type E2 = List[int] | None
1761 type E3 = None | List[int]
1762
1763 type F1 = Literal[1]
1764 type F2 = Literal['x'] | None
1765 type F3 = None | Literal[True]
1766
1767 type G1 = tuple[int, Any]
1768 type G2 = tuple[int, Any] | None
1769 type G3 = None | tuple[int, Any]
1770
1771 type H1 = TD
1772 type H2 = TD | None
1773 type H3 = None | TD
1774
1775 type I1 = C[int]
1776 type I2 = C[Any] | None
1777 type I3 = None | C[TD]
1778 [builtins fixtures/type.pyi]
1779 [typing fixtures/typing-full.pyi]
1780
1781 [case testTypedDictInlineYesNewStyleAlias]
1782 # flags: --enable-incomplete-feature=InlineTypedDict
1783 type X[T] = {"item": T, "other": X[T] | None}
1784 x: X[str]
1785 reveal_type(x) # N: Revealed type is "TypedDict({'item': builtins.str, 'other': ... | None})"
1786 if x["other"] is not None:
1787 reveal_type(x["other"]["item"]) # N: Revealed type is "builtins.str"
1788
1789 type Y[T] = {"item": T, **Y[T]} # E: Overwriting TypedDict field "item" while merging
1790 [builtins fixtures/dict.pyi]
1791 [typing fixtures/typing-full.pyi]
1792
1793 [case testPEP695UsingIncorrectExpressionsInTypeVariableBound_no_native_parse]
1794 type X[T: (yield 1)] = Any # E: Yield expression cannot be used as a type variable bound
1795 type Y[T: (yield from [])] = Any # E: Yield expression cannot be used as a type variable bound
1796 type Z[T: (a := 1)] = Any # E: Named expression cannot be used as a type variable bound
1797 type K[T: (await 1)] = Any # E: Await expression cannot be used as a type variable bound
1798
1799 type XNested[T: (1 + (yield 1))] = Any # E: Yield expression cannot be used as a type variable bound
1800 type YNested[T: (1 + (yield from []))] = Any # E: Yield expression cannot be used as a type variable bound
1801 type ZNested[T: (1 + (a := 1))] = Any # E: Named expression cannot be used as a type variable bound
1802 type KNested[T: (1 + (await 1))] = Any # E: Await expression cannot be used as a type variable bound
1803
1804 class FooX[T: (yield 1)]: pass # E: Yield expression cannot be used as a type variable bound
1805 class FooY[T: (yield from [])]: pass # E: Yield expression cannot be used as a type variable bound
1806 class FooZ[T: (a := 1)]: pass # E: Named expression cannot be used as a type variable bound
1807 class FooK[T: (await 1)]: pass # E: Await expression cannot be used as a type variable bound
1808
1809 class FooXNested[T: (1 + (yield 1))]: pass # E: Yield expression cannot be used as a type variable bound
1810 class FooYNested[T: (1 + (yield from []))]: pass # E: Yield expression cannot be used as a type variable bound
1811 class FooZNested[T: (1 + (a := 1))]: pass # E: Named expression cannot be used as a type variable bound
1812 class FooKNested[T: (1 + (await 1))]: pass # E: Await expression cannot be used as a type variable bound
1813
1814 def foox[T: (yield 1)](): pass # E: Yield expression cannot be used as a type variable bound
1815 def fooy[T: (yield from [])](): pass # E: Yield expression cannot be used as a type variable bound
1816 def fooz[T: (a := 1)](): pass # E: Named expression cannot be used as a type variable bound
1817 def fook[T: (await 1)](): pass # E: Await expression cannot be used as a type variable bound
1818
1819 def foox_nested[T: (1 + (yield 1))](): pass # E: Yield expression cannot be used as a type variable bound
1820 def fooy_nested[T: (1 + (yield from []))](): pass # E: Yield expression cannot be used as a type variable bound
1821 def fooz_nested[T: (1 + (a := 1))](): pass # E: Named expression cannot be used as a type variable bound
1822 def fook_nested[T: (1 +(await 1))](): pass # E: Await expression cannot be used as a type variable bound
1823
1824 [case testPEP695UsingIncorrectExpressionsInTypeAlias_no_native_parse]
1825 type X = (yield 1) # E: Yield expression cannot be used within a type alias
1826 type Y = (yield from []) # E: Yield expression cannot be used within a type alias
1827 type Z = (a := 1) # E: Named expression cannot be used within a type alias
1828 type K = (await 1) # E: Await expression cannot be used within a type alias
1829
1830 type XNested = (1 + (yield 1)) # E: Yield expression cannot be used within a type alias
1831 type YNested = (1 + (yield from [])) # E: Yield expression cannot be used within a type alias
1832 type ZNested = (1 + (a := 1)) # E: Named expression cannot be used within a type alias
1833 type KNested = (1 + (await 1)) # E: Await expression cannot be used within a type alias
1834
1835 [case testWalrusRebindsNamesLoadedInComprehensionTargets]
1836 # Newly legal in https://github.com/python/cpython/pull/100581
1837 from typing import Any
1838
1839 def f(c: Any, d: Any, e: Any, f: Any, g: Any, h: Any, j: Any) -> None:
1840 ((c := 1) for a, (*b, c[d+e::f(g)], h.i) in j)
1841 [(c := 1) for a, (*b, c[d+e::f(g)], h.i) in j]
1842 {(c := 1) for a, (*b, c[d+e::f(g)], h.i) in j}
1843
1844 ((d := 1) for a, (*b, c[d+e::f(g)], h.i) in j)
1845 [(d := 1) for a, (*b, c[d+e::f(g)], h.i) in j]
1846 {(d := 1) for a, (*b, c[d+e::f(g)], h.i) in j}
1847
1848 ((e := 1) for a, (*b, c[d+e::f(g)], h.i) in j)
1849 [(e := 1) for a, (*b, c[d+e::f(g)], h.i) in j]
1850 {(e := 1) for a, (*b, c[d+e::f(g)], h.i) in j}
1851
1852 ((f := 1) for a, (*b, c[d+e::f(g)], h.i) in j)
1853 [(f := 1) for a, (*b, c[d+e::f(g)], h.i) in j]
1854 {(f := 1) for a, (*b, c[d+e::f(g)], h.i) in j}
1855
1856 ((g := 1) for a, (*b, c[d+e::f(g)], h.i) in j)
1857 [(g := 1) for a, (*b, c[d+e::f(g)], h.i) in j]
1858 {(g := 1) for a, (*b, c[d+e::f(g)], h.i) in j}
1859
1860 ((h := 1) for a, (*b, c[d+e::f(g)], h.i) in j)
1861 [(h := 1) for a, (*b, c[d+e::f(g)], h.i) in j]
1862 {(h := 1) for a, (*b, c[d+e::f(g)], h.i) in j}
1863
1864 ((i := 1) for a, (*b, c[d+e::f(g)], h.i) in j)
1865 [(i := 1) for a, (*b, c[d+e::f(g)], h.i) in j]
1866 {(i := 1) for a, (*b, c[d+e::f(g)], h.i) in j}
1867
1868 ((j := 1) for a, (*b, c[d+e::f(g)], h.i) in j)
1869 [(j := 1) for a, (*b, c[d+e::f(g)], h.i) in j]
1870 {(j := 1) for a, (*b, c[d+e::f(g)], h.i) in j}
1871 [builtins fixtures/primitives.pyi]
1872
1873 [case testPEP695TypeAliasAndAnnotated]
1874 from typing_extensions import Annotated, Annotated as _Annotated
1875 import typing_extensions as t
1876
1877 def ann(*args): ...
1878
1879 type A = Annotated[int, ann()]
1880 type B = Annotated[int | str, ann((1, 2))]
1881 type C = _Annotated[int, ann()]
1882 type D = t.Annotated[str, ann()]
1883
1884 x: A
1885 y: B
1886 z: C
1887 zz: D
1888 reveal_type(x) # N: Revealed type is "builtins.int"
1889 reveal_type(y) # N: Revealed type is "builtins.int | builtins.str"
1890 reveal_type(z) # N: Revealed type is "builtins.int"
1891 reveal_type(zz) # N: Revealed type is "builtins.str"
1892 [builtins fixtures/tuple.pyi]
1893
1894 [case testPEP695NestedGenericClass1]
1895 class C[T]:
1896 def f(self) -> T: ...
1897
1898 class A:
1899 class B[Q]:
1900 def __init__(self, a: Q) -> None:
1901 self.a = a
1902
1903 def f(self) -> Q:
1904 return self.a
1905
1906 def g(self, x: Q) -> None: ...
1907
1908 b: B[str]
1909
1910 x: A.B[int]
1911 x.g("x") # E: Argument 1 to "g" of "B" has incompatible type "str"; expected "int"
1912 reveal_type(x.a) # N: Revealed type is "builtins.int"
1913 reveal_type(x) # N: Revealed type is "__main__.A.B[builtins.int]"
1914 reveal_type(A.b) # N: Revealed type is "__main__.A.B[builtins.str]"
1915
1916 [case testPEP695NestedGenericClass2]
1917 class A:
1918 def m(self) -> None:
1919 class B[T]:
1920 def f(self) -> T: ...
1921 x: B[int]
1922 reveal_type(x.f()) # N: Revealed type is "builtins.int"
1923 self.a = B[str]()
1924
1925 reveal_type(A().a) # N: Revealed type is "__main__.B@3[builtins.str]"
1926 reveal_type(A().a.f()) # N: Revealed type is "builtins.str"
1927
1928 [case testPEP695NestedGenericClass3]
1929 class C[T]:
1930 def f(self) -> T: ...
1931 class D[S]:
1932 x: T # E: Name "T" is not defined
1933 def g(self) -> S: ...
1934
1935 a: C[int]
1936 reveal_type(a.f()) # N: Revealed type is "builtins.int"
1937 b: C.D[str]
1938 reveal_type(b.g()) # N: Revealed type is "builtins.str"
1939
1940 class E[T]:
1941 class F[T]: # E: "T" already defined as a type parameter
1942 x: T
1943
1944 c: E.F[int]
1945
1946 [case testPEP695NestedGenericClass4]
1947 class A:
1948 class B[T]:
1949 def __get__(self, instance: A, owner: type[A]) -> T:
1950 return None # E: Incompatible return value type (got "None", expected "T")
1951 f = B[int]()
1952
1953 a = A()
1954 v = a.f
1955
1956 [case testPEP695VarianceInheritedFromBaseWithExplicitVariance]
1957 from typing import TypeVar, Generic
1958
1959 T = TypeVar("T")
1960
1961 class ParentInvariant(Generic[T]):
1962 pass
1963
1964 class Invariant1[T](ParentInvariant[T]):
1965 pass
1966
1967 a1: Invariant1[int] = Invariant1[float]() # E: Incompatible types in assignment (expression has type "Invariant1[float]", variable has type "Invariant1[int]")
1968 a2: Invariant1[float] = Invariant1[int]() # E: Incompatible types in assignment (expression has type "Invariant1[int]", variable has type "Invariant1[float]")
1969
1970 T_contra = TypeVar("T_contra", contravariant=True)
1971
1972 class ParentContravariant(Generic[T_contra]):
1973 pass
1974
1975 class Contravariant[T](ParentContravariant[T]):
1976 pass
1977
1978 b1: Contravariant[int] = Contravariant[float]()
1979 b2: Contravariant[float] = Contravariant[int]() # E: Incompatible types in assignment (expression has type "Contravariant[int]", variable has type "Contravariant[float]")
1980
1981 class Invariant2[T](ParentContravariant[T]):
1982 def f(self) -> T: ...
1983
1984 c1: Invariant2[int] = Invariant2[float]() # E: Incompatible types in assignment (expression has type "Invariant2[float]", variable has type "Invariant2[int]")
1985 c2: Invariant2[float] = Invariant2[int]() # E: Incompatible types in assignment (expression has type "Invariant2[int]", variable has type "Invariant2[float]")
1986
1987 class Multi[T, S](ParentInvariant[T], ParentContravariant[S]):
1988 pass
1989
1990 d1: Multi[int, str] = Multi[float, str]() # E: Incompatible types in assignment (expression has type "Multi[float, str]", variable has type "Multi[int, str]")
1991 d2: Multi[float, str] = Multi[int, str]() # E: Incompatible types in assignment (expression has type "Multi[int, str]", variable has type "Multi[float, str]")
1992 d3: Multi[str, int] = Multi[str, float]()
1993 d4: Multi[str, float] = Multi[str, int]() # E: Incompatible types in assignment (expression has type "Multi[str, int]", variable has type "Multi[str, float]")
1994
1995 [case testPEP695MultipleNestedGenericClass1]
1996 # flags: --enable-incomplete-feature=NewGenericSyntax
1997 class A:
1998 class B:
1999 class C:
2000 class D[Q]:
2001 def g(self, x: Q): ...
2002 d: D[str]
2003
2004 x: A.B.C.D[int]
2005 x.g('a') # E: Argument 1 to "g" of "D" has incompatible type "str"; expected "int"
2006 reveal_type(x) # N: Revealed type is "__main__.A.B.C.D[builtins.int]"
2007 reveal_type(A.B.C.d) # N: Revealed type is "__main__.A.B.C.D[builtins.str]"
2008
2009 [case testPEP695MultipleNestedGenericClass2]
2010 # flags: --enable-incomplete-feature=NewGenericSyntax
2011 class A:
2012 class B:
2013 def m(self) -> None:
2014 class C[T]:
2015 def f(self) -> T: ...
2016 x: C[int]
2017 reveal_type(x.f()) # N: Revealed type is "builtins.int"
2018 self.a = C[str]()
2019
2020 reveal_type(A().B().a) # N: Revealed type is "__main__.C@5[builtins.str]"
2021
2022 [case testPEP695MultipleNestedGenericClass3]
2023 # flags: --enable-incomplete-feature=NewGenericSyntax
2024 class A:
2025 class C[T]:
2026 def f(self) -> T: ...
2027 class D[S]:
2028 x: T # E: Name "T" is not defined
2029 def g(self) -> S: ...
2030
2031 a: A.C[int]
2032 reveal_type(a.f()) # N: Revealed type is "builtins.int"
2033 b: A.C.D[str]
2034 reveal_type(b.g()) # N: Revealed type is "builtins.str"
2035
2036 class B:
2037 class E[T]:
2038 class F[T]: # E: "T" already defined as a type parameter
2039 x: T
2040
2041 c: B.E.F[int]
2042
2043 [case testPEP695MultipleNestedGenericClass4]
2044 # flags: --enable-incomplete-feature=NewGenericSyntax
2045 class Z:
2046 class A:
2047 class B[T]:
2048 def __get__(self, instance: Z.A, owner: type[Z.A]) -> T:
2049 return None # E: Incompatible return value type (got "None", expected "T")
2050 f = B[int]()
2051
2052 a = Z.A()
2053 v = a.f
2054
2055 [case testPEP695MultipleNestedGenericClass5]
2056 # flags: --enable-incomplete-feature=NewGenericSyntax
2057 from a.b.c import d
2058 x: d.D.E.F.G[int]
2059 x.g('a') # E: Argument 1 to "g" of "G" has incompatible type "str"; expected "int"
2060 reveal_type(x) # N: Revealed type is "a.b.c.d.D.E.F.G[builtins.int]"
2061 reveal_type(d.D.E.F.d) # N: Revealed type is "a.b.c.d.D.E.F.G[builtins.str]"
2062
2063 [file a/b/c/d.py]
2064 class D:
2065 class E:
2066 class F:
2067 class G[Q]:
2068 def g(self, x: Q): ...
2069 d: G[str]
2070
2071 [case testTypeAliasNormalization]
2072 from collections.abc import Callable
2073 from typing import Unpack
2074 from typing_extensions import TypeAlias
2075
2076 type RK_function_args = tuple[float, int]
2077 type RK_functionBIS = Callable[[Unpack[RK_function_args], int], int]
2078
2079 def ff(a: float, b: int, c: int) -> int:
2080 return 2
2081
2082 bis: RK_functionBIS = ff
2083 res: int = bis(1.0, 2, 3)
2084 [builtins fixtures/tuple.pyi]
2085 [typing fixtures/typing-full.pyi]
2086
2087 [case testPEP695TypeAliasNotReadyClass]
2088 class CustomizeResponse:
2089 related_resources: "ResourceRule"
2090
2091 class ResourceRule: pass
2092
2093 class DecoratorController:
2094 type CustomizeResponse = CustomizeResponse
2095
2096 x: DecoratorController.CustomizeResponse
2097 reveal_type(x.related_resources) # N: Revealed type is "__main__.ResourceRule"
2098 [builtins fixtures/tuple.pyi]
2099
2100 [case testPEP695TypeAliasRecursiveOuterClass]
2101 class A:
2102 type X = X # E: Cannot resolve name "X" (possible cyclic definition)
2103 class X: ...
2104
2105 class AA:
2106 XX = XX # OK, we allow this as a special case.
2107 class XX: ...
2108
2109 class Y: ...
2110 class B:
2111 type Y = Y
2112
2113 reveal_type(AA.XX) # N: Revealed type is "def () -> __main__.XX"
2114 y: B.Y
2115 reveal_type(y) # N: Revealed type is "__main__.Y"
2116 [builtins fixtures/tuple.pyi]
2117 [typing fixtures/typing-full.pyi]
2118
2119 [case testPEP695TypeAliasRecursiveInvalid]
2120 type X = X # E: Cannot resolve name "X" (possible cyclic definition)
2121 type Z = Z[int] # E: Cannot resolve name "Z" (possible cyclic definition)
2122 def foo() -> None:
2123 type X = X # OK, refers to outer (invalid) X
2124 x: X
2125 reveal_type(x) # N: Revealed type is "Any"
2126 type Y = Y # E: Cannot resolve name "Y" (possible cyclic definition) \
2127 # N: Recursive types are not allowed at function scope
2128 class Z: ... # E: Name "Z" already defined on line 2
2129 [builtins fixtures/tuple.pyi]
2130 [typing fixtures/typing-full.pyi]
2131
2132 [case testPEP695MultipleUnpacksInBareApplicationNoCrash]
2133 # https://github.com/python/mypy/issues/18856
2134 class A[*Ts]: ...
2135
2136 A[*tuple[int, ...], *tuple[int, ...]] # E: More than one variadic Unpack in a type is not allowed
2137 A[*tuple[*tuple[int, ...]], *tuple[*tuple[int, ...]]] # E: More than one variadic Unpack in a type is not allowed
2138 a: A[*tuple[int, ...], *tuple[int, ...]] # E: More than one variadic Unpack in a type is not allowed
2139 def foo(a: A[*tuple[int, ...], *tuple[int, ...]]): ... # E: More than one variadic Unpack in a type is not allowed
2140
2141 tuple[*tuple[int, ...], *tuple[int, ...]] # E: More than one variadic Unpack in a type is not allowed
2142 b: tuple[*tuple[int, ...], *tuple[int, ...]] # E: More than one variadic Unpack in a type is not allowed
2143 [builtins fixtures/tuple.pyi]
2144 [typing fixtures/typing-full.pyi]
2145
2146 [case testForwardNestedPrecedesForwardGlobal]
2147 from typing import NewType
2148
2149 class W[T]: pass
2150
2151 class R:
2152 class M(W[Action.V], type):
2153 FOO = R.Action.V(0)
2154 class Action(metaclass=M):
2155 V = NewType('V', int)
2156
2157 class Action:
2158 pass
2159
2160 [case testPEP695TypeVarConstraintsDefaultAliases]
2161 from typing import Generic
2162 from typing_extensions import TypeVar
2163
2164 type K = int
2165 type V = int
2166 type L = list[int]
2167
2168 T1 = TypeVar("T1", str, K, default=K)
2169 T2 = TypeVar("T2", str, K, default=V)
2170 T3 = TypeVar("T3", str, L, default=L)
2171
2172 class A1(Generic[T1]):
2173 x: T1
2174 class A2(Generic[T2]):
2175 x: T2
2176 class A3(Generic[T3]):
2177 x: T3
2178
2179 reveal_type(A1().x) # N: Revealed type is "builtins.int"
2180 reveal_type(A2().x) # N: Revealed type is "builtins.int"
2181 reveal_type(A3().x) # N: Revealed type is "builtins.list[builtins.int]"
2182 [builtins fixtures/tuple.pyi]
2183
2184 [case testDataclassWithTypeVarTuple]
2185 # flags: --python-version 3.13
2186 # https://github.com/python/mypy/issues/19559
2187 from typing import Callable
2188 from dataclasses import dataclass
2189
2190 @dataclass
2191 class Test[*Ts, R]:
2192 a: Callable[[*Ts], R]
2193 [builtins fixtures/dict.pyi]
2194
2195 [case testPEP695AliasDoesNotReferToFullname]
2196 # https://github.com/python/mypy/issues/19698
2197 from typing import TypeAliasType
2198 type D = dict
2199 type T = type
2200 type TA = TypeAliasType
2201
2202 D() # E: "TypeAliasType" not callable
2203 X = TA("Y") # E: "TypeAliasType" not callable
2204
2205 x: object
2206 if T(x) is str: # E: "TypeAliasType" not callable
2207 reveal_type(x) # N: Revealed type is "builtins.object"
2208 [builtins fixtures/tuple.pyi]
2209 [typing fixtures/typing-full.pyi]
2210
2211 [case testPEP695TypeAliasForwardReferenceInUnusedTypeVar]
2212 # https://discuss.python.org/t/103305
2213 type Alias1[T: "A"] = int
2214 type Alias2[T: ("A", int)] = int
2215 class A: ...
2216
2217 x1: Alias1[A] # ok
2218 x2: Alias2[A] # ok
2219
2220 [case testUndefinedUnpackInPEP696Base]
2221 # Typo below is intentional.
2222 class MyTuple[*Ts](tuple[*TS]): # E: Name "TS" is not defined
2223 ...
2224
2225 x: MyTuple[int, str]
2226 reveal_type(x[0]) # N: Revealed type is "Any"
2227 [builtins fixtures/tuple.pyi]
2228 [typing fixtures/typing-full.pyi]
2229
2230 [case testAnnotatedWithCallableAsParameterTypeKeyword]
2231 from typing_extensions import Annotated
2232
2233 def something() -> None: ...
2234
2235 type A = list[Annotated[str, something()]]
2236 a: A
2237 reveal_type(a) # N: Revealed type is "builtins.list[builtins.str]"
2238 [builtins fixtures/tuple.pyi]
2239
2240 [case testAnnotatedWithCallableAsParameterTypeKeywordDeeper]
2241 from typing_extensions import Annotated
2242
2243 def something() -> None: ...
2244
2245 type A = list[Annotated[Annotated[str, something()], something()]]
2246 a: A
2247 reveal_type(a) # N: Revealed type is "builtins.list[builtins.str]"
2248 [builtins fixtures/tuple.pyi]
2249 [case testPEP695TypeAliasRecursiveInParameterBound]
2250 from typing import Any
2251
2252 type A1[T: B1] = list[int]
2253 type B1 = None | A1[B1]
2254 x1: A1[B1]
2255 y1: A1[int] # E: Type argument "int" of "A1" must be a subtype of "B1"
2256 z1: A1[None]
2257
2258 type A2[T: B2] = list[T]
2259 type B2 = None | A2[Any]
2260 x2: A2[B2]
2261 y2: A2[int] # E: Type argument "int" of "A2" must be a subtype of "B2"
2262 z2: A2[None]
2263
2264 type A3[T: B3] = list[T]
2265 type B3 = None | A3[B3]
2266 x3: A3[B3]
2267 y3: A3[int] # E: Type argument "int" of "A3" must be a subtype of "B3"
2268 z3: A3[None]
2269 [builtins fixtures/tuple.pyi]
2270 [typing fixtures/typing-full.pyi]
2271
2272 [case testPEP695TypeAliasRecursiveTupleUnionNoCrash]
2273 from collections.abc import Hashable
2274
2275 type HashableArg = int | tuple[Hashable | HashableArg]
2276 x: HashableArg
2277 reveal_type(x) # N: Revealed type is "builtins.int | tuple[typing.Hashable | ...]"
2278 if isinstance(x, tuple):
2279 y, = x
2280 reveal_type(y) # N: Revealed type is "typing.Hashable | builtins.int | tuple[typing.Hashable | ...]"
2281 [builtins fixtures/tuple.pyi]
2282 [typing fixtures/typing-full.pyi]
2283
2284 [case testPEP695MultipleTypeVarTuples]
2285 from typing import Generic
2286 from typing_extensions import TypeVarTuple, Unpack
2287
2288 Us = TypeVarTuple("Us")
2289
2290 class C[*Ts, *Us]: # E: Can only use one type var tuple in a class def
2291 pass
2292
2293 class D[*Ts](Generic[Unpack[Us]]): # E: Generic[...] base class is redundant \
2294 # E: Can only use one type var tuple in a class def
2295 pass
2296 [builtins fixtures/tuple.pyi]
2297
2298 [case testPEP695VariadicAliasUnpack]
2299 class C[*Ts]:
2300 pass
2301
2302 type T[T, *Ts] = C[*Ts]
2303
2304 x: T[bool, *tuple[()]]
2305 reveal_type(x) # N: Revealed type is "__main__.C[()]"
2306
2307 y: T[bool]
2308 reveal_type(y) # N: Revealed type is "__main__.C[()]"
2309 [builtins fixtures/tuple.pyi]
2310
2311 [case testTupleBaseTupleTypeUnpack]
2312 class X(tuple[*tuple[int], *tuple[int]]):
2313 pass
2314 x = X((1, 2))
2315 reveal_type(x) # N: Revealed type is "tuple[builtins.int, builtins.int, fallback=__main__.X]"
2316 [builtins fixtures/tuple.pyi]
2317
2318 [case testNewTypeTupleTypeUnpack]
2319 from typing import NewType
2320
2321 T = NewType("T", tuple[*tuple[int], *tuple[int]])
2322 t = T((1, 2))
2323 reveal_type(t) # N: Revealed type is "tuple[builtins.int, builtins.int, fallback=__main__.T]"
2324 [builtins fixtures/tuple.pyi]
2325
2326 [case testNamedTupleTupleTypeUnpack]
2327 from typing import NamedTuple
2328
2329 class N(NamedTuple):
2330 x: tuple[*tuple[int], *tuple[int]]
2331 n = N((1, 2))
2332 reveal_type(n) # N: Revealed type is "tuple[tuple[builtins.int, builtins.int], fallback=__main__.N]"
2333 [builtins fixtures/tuple.pyi]