Debian Codegraph @

test-data/unit/check-newsemanal.test

raw
1 -- Test cases for the new semantic analyzer
2
3 [case testNewAnalyzerEmpty]
4
5 [case testNewAnalyzerSimpleAssignment]
6 x = 1
7 x.y # E: "int" has no attribute "y"
8 y # E: Name "y" is not defined
9
10 [case testNewAnalyzerSimpleAnnotation]
11 x: int = 0
12 y: str = 0 \
13 # E: Incompatible types in assignment (expression has type "int", variable has type "str")
14
15 [case testNewAnalyzerSimpleClass]
16 class A:
17 x: int
18 a: A
19 a.x
20 a.y # E: "A" has no attribute "y"
21
22 [case testNewAnalyzerErrorInClassBody]
23 class A:
24 x # E: Name "x" is not defined
25
26 [case testNewAnalyzerTypeAnnotationForwardReference]
27 class A:
28 b: B
29 class B:
30 a: A
31 a: A
32 b: B
33 a.b = a # E: Incompatible types in assignment (expression has type "A", variable has type "B")
34 a.b = b
35 b.a = a
36 b.a = b # E: Incompatible types in assignment (expression has type "B", variable has type "A")
37
38 [case testNewAnalyzerTypeAnnotationCycle1]
39 import b
40 [file a.py]
41 import b
42 class A: pass
43 y: b.B
44 y() # E: "B" not callable
45 [file b.py]
46 import a
47 class B: pass
48 x: a.A
49 reveal_type(x) # N: Revealed type is "a.A"
50
51 [case testNewAnalyzerTypeAnnotationCycle2]
52 import a
53 [file a.py]
54 from b import B
55 class A: pass
56 y: B
57 y() # E: "B" not callable
58 [file b.py]
59 from a import A
60 class B: pass
61 x: A
62 x() # E: "A" not callable
63
64 [case testNewAnalyzerTypeAnnotationCycle3]
65 import b
66 [file a.py]
67 from b import bad # E: Module "b" has no attribute "bad"; maybe "bad2"?
68 [file b.py]
69 from a import bad2 # E: Module "a" has no attribute "bad2"; maybe "bad"?
70
71 [case testNewAnalyzerTypeAnnotationCycle4]
72 import b
73 [file a.py]
74 from b import bad # E: Module "b" has no attribute "bad"
75 [file b.py]
76 # TODO: Could we generate an error here as well?
77 from a import bad
78 [targets a, b, a, b, a, b, a, b, __main__]
79
80 [case testNewAnalyzerExportedValuesInImportAll]
81 from m import *
82 _ = a
83 _ = b
84 _ = c
85 _ = d
86 _e = e
87 _f = f # E: Name "f" is not defined
88 _ = _g # E: Name "_g" is not defined
89 reveal_type(_e) # N: Revealed type is "m.A"
90 [file m.py]
91 __all__ = ['a']
92 __all__ += ('b',)
93 __all__.append('c')
94 __all__.extend(('d', 'e'))
95
96 a = b = c = d = _g = 1
97 e: 'A'
98 f: 'A'
99
100 class A: ...
101 [builtins fixtures/module_all.pyi]
102
103 [case testNewAnalyzerSimpleFunction]
104 def f(x: int) -> str:
105 return 'x'
106
107 def g(x: int) -> int:
108 y = f(1)
109 return y # E: Incompatible return value type (got "str", expected "int")
110
111 [case testNewAnalyzerSimpleMethod]
112 class A:
113 def __init__(self, x: int) -> None:
114 self.x = x
115
116 def f(self) -> str:
117 return self.x # E: Incompatible return value type (got "int", expected "str")
118
119 def g(self) -> int:
120 return self.f() # E: Incompatible return value type (got "str", expected "int")
121
122 [case testNewAnalyzerFunctionForwardRef]
123 def f() -> None:
124 x = g(1) # E: Argument 1 to "g" has incompatible type "int"; expected "str"
125 reveal_type(x) # N: Revealed type is "builtins.str"
126
127 def g(x: str) -> str:
128 return x
129
130 [case testNewAnalyzerExportedImportThreePasses]
131 import b
132
133 [file a.py]
134 from b import b1 as a2
135 from b import b2 as a3
136
137 def a1() -> int: pass
138
139 reveal_type(a3()) # N: Revealed type is "builtins.int"
140
141 [file b.py]
142 from a import a1 as b2
143 from a import a2 as b3
144
145 def b1() -> str: pass
146
147 reveal_type(b3()) # N: Revealed type is "builtins.str"
148
149 [case testNewAnalyzerBool]
150 reveal_type(True) # N: Revealed type is "Literal[True]?"
151 reveal_type(False) # N: Revealed type is "Literal[False]?"
152
153 [case testNewAnalyzerNewTypeMultiplePasses]
154 import b
155
156 [file a.py]
157 from typing import NewType
158 import b
159 class A: pass
160 N2 = NewType('N2', b.N1)
161 def f1(x: A) -> None: pass
162 def f2(x: b.N1) -> None: pass
163 def f3(x: N2) -> None: pass
164 a = A()
165 n1 = b.N1(a)
166 n2 = N2(n1)
167 f1(a)
168 f1(n1)
169 f1(n2)
170 f2(a) # E: Argument 1 to "f2" has incompatible type "A"; expected "N1"
171 f2(n1)
172 f2(n2)
173 f3(a) # E: Argument 1 to "f3" has incompatible type "A"; expected "N2"
174 f3(n1) # E: Argument 1 to "f3" has incompatible type "N1"; expected "N2"
175 f3(n2)
176
177 # Test N2 etc.
178
179 [file b.py]
180 from typing import NewType
181 import a
182 N1 = NewType('N1', a.A)
183
184 [case testNewAnalyzerInheritanceForwardRef]
185 class C(B):
186 pass
187 class B(A):
188 pass
189 class A:
190 def __init__(self, x: str) -> None: pass
191 def f(self, x: int) -> None: pass
192
193 C(1) # E: Argument 1 to "C" has incompatible type "int"; expected "str"
194 B(1) # E: Argument 1 to "B" has incompatible type "int"; expected "str"
195 C('').f('') # E: Argument 1 to "f" of "A" has incompatible type "str"; expected "int"
196 B('').f('') # E: Argument 1 to "f" of "A" has incompatible type "str"; expected "int"
197
198 [case testNewAnalyzerInheritanceMROInCycle]
199 import a
200
201 [file a.py]
202 from b import A
203 import b
204
205 class B(A):
206 b: int
207
208 class D(b.C):
209 d: int
210
211 d = D()
212 reveal_type(d.a) # N: Revealed type is "builtins.int"
213 reveal_type(d.b) # N: Revealed type is "builtins.int"
214 reveal_type(d.c) # N: Revealed type is "builtins.int"
215 reveal_type(d.d) # N: Revealed type is "builtins.int"
216
217 [file b.py]
218 from a import B
219
220 class A:
221 a: int
222
223 class C(B):
224 c: int
225
226 [targets b, a, b, a, __main__]
227
228 [case testNewAnalyzerTypedDictClass]
229 from typing import TypedDict
230 import a
231 class T1(TypedDict):
232 x: A
233 class A: pass
234 reveal_type(T1(x=A())) # N: Revealed type is "TypedDict('__main__.T1', {'x': __main__.A})"
235
236 [file a.py]
237 from typing import TypedDict
238 from b import TD1 as TD2, TD3
239 class T2(TD3):
240 x: int
241 reveal_type(T2(x=2)) # N: Revealed type is "TypedDict('a.T2', {'x': builtins.int})"
242
243 [file b.py]
244 from a import TypedDict as TD1
245 from a import TD2 as TD3
246 [builtins fixtures/dict.pyi]
247 [typing fixtures/typing-typeddict.pyi]
248
249 [case testNewAnalyzerTypedDictClassInheritance]
250 from typing import TypedDict
251
252 class T2(T1):
253 y: int
254
255 class T1(TypedDict):
256 x: str
257
258 class T3(TypedDict):
259 x: str
260
261 class T4(T3):
262 y: A
263
264 class A: pass
265
266 T2(x=0, y=0) # E: Incompatible types (expression has type "int", TypedDict item "x" has type "str")
267 x: T2
268 reveal_type(x) # N: Revealed type is "TypedDict('__main__.T2', {'x': builtins.str, 'y': builtins.int})"
269 y: T4
270 reveal_type(y) # N: Revealed type is "TypedDict('__main__.T4', {'x': builtins.str, 'y': __main__.A})"
271 [builtins fixtures/dict.pyi]
272 [typing fixtures/typing-typeddict.pyi]
273
274 [case testNewAnalyzerRedefinitionAndDeferral1a]
275 import a
276
277 [file a.py]
278 MYPY = False
279 if MYPY:
280 from b import x as y
281 x = 0
282
283 def y(): pass # E: Name "y" already defined on line 4
284 reveal_type(y) # N: Revealed type is "builtins.int"
285
286 y2 = y
287 class y2: pass # E: Name "y2" already defined on line 9
288 reveal_type(y2) # N: Revealed type is "builtins.int"
289
290 y3, y4 = y, y
291 if MYPY: # Tweak processing order
292 from b import f as y3 # E: Incompatible import of "y3" (imported name has type "Callable[[], Any]", local name has type "int")
293 reveal_type(y3) # N: Revealed type is "builtins.int"
294
295 [file b.py]
296 from a import x
297
298 def f(): pass
299
300 [targets a, b, a, a.y, b.f, __main__]
301 [builtins fixtures/tuple.pyi]
302
303 [case testNewAnalyzerRedefinitionAndDeferral1b]
304 import a
305
306 [file a.py]
307 from b import x as y
308 x = 0
309
310 def y(): pass # E: Name "y" already defined on line 2
311 reveal_type(y) # N: Revealed type is "builtins.int"
312
313 y2 = y
314 class y2: pass # E: Name "y2" already defined on line 7
315 reveal_type(y2) # N: Revealed type is "builtins.int"
316
317 y3, y4 = y, y
318 from b import f as y3 # E: Incompatible import of "y3" (imported name has type "Callable[[], Any]", local name has type "int")
319 reveal_type(y3) # N: Revealed type is "builtins.int"
320
321 [file b.py]
322 MYPY = False
323 if MYPY: # Tweak processing order
324 from a import x
325
326 def f(): pass
327
328 [targets b, a, b, a, b.f, a.y, __main__]
329
330 [case testNewAnalyzerRedefinitionAndDeferral2a]
331 import a
332
333 [file a.py]
334 MYPY = False
335 if MYPY: # Tweak processing order
336 from b import C as C2
337 class C: pass
338 class C2: pass # E: Name "C2" already defined on line 4
339
340 [file b.py]
341 from a import C
342
343 [case testNewAnalyzerRedefinitionAndDeferral2b]
344 import a
345
346 [file a.py]
347 from b import C as C2
348 class C: pass
349
350 class C2: pass # E: Name "C2" already defined on line 2
351 [file b.py]
352 MYPY = False
353 if MYPY: # Tweak processing order
354 from a import C
355
356 [case testNewAnalyzerRedefinitionAndDeferral3]
357 import a
358
359 [file a.py]
360 from b import f as g
361 def f(): pass
362
363 a, *b = g()
364 class b(): pass # E: Name "b" already defined on line 4
365 reveal_type(b) # N: Revealed type is "Any"
366
367 [file b.py]
368 from a import f
369
370 [case testNewAnalyzerImportStarForwardRef1]
371 import a
372
373 [file a.py]
374 x: A
375 reveal_type(x) # N: Revealed type is "b.A"
376
377 from b import *
378
379 class A: pass # E: Name "A" already defined (possibly by an import)
380
381 [file b.py]
382 class A: pass
383 MYPY = False
384 if MYPY: # Tweak processing order
385 from a import x
386
387 [case testNewAnalyzerImportStarForwardRef2]
388 import a
389
390 [file a.py]
391 x: A
392 reveal_type(x) # N: Revealed type is "b.A"
393
394 MYPY = False
395 if MYPY: # Tweak processing order
396 from b import *
397
398 class A: pass # E: Name "A" already defined (possibly by an import)
399
400 [file b.py]
401 class A: pass
402 from a import x
403
404 [case testNewAnalyzerClassInFunction]
405 def main() -> None:
406 x: C
407 class C:
408 def __init__(self) -> None:
409 self.x: A
410 x() # E: "C" not callable
411 reveal_type(x.x) # N: Revealed type is "__main__.A@8"
412 class A: pass
413
414 [case testNewAnalyzerMutuallyRecursiveFunctions]
415 def main() -> None:
416 def f() -> int:
417 reveal_type(g()) # N: Revealed type is "builtins.str"
418 return int()
419 def g() -> str:
420 reveal_type(f()) # N: Revealed type is "builtins.int"
421 return str()
422
423 [case testNewAnalyzerMissingNamesInFunctions]
424 def main() -> None:
425 def f() -> None:
426 x # E: Name "x" is not defined
427 class C:
428 x # E: Name "x" is not defined
429
430 [case testNewAnalyzerCyclicDefinitions]
431 # flags: --disable-error-code used-before-def
432 gx = gy # E: Cannot resolve name "gy" (possible cyclic definition)
433 gy = gx
434 def main() -> None:
435 class C:
436 def meth(self) -> None:
437 lx = ly # E: Cannot resolve name "ly" (possible cyclic definition) \
438 # N: Recursive types are not allowed at function scope
439 ly = lx
440
441 [case testNewAnalyzerCyclicDefinitionCrossModule]
442 import b
443 [file a.py]
444 import b
445 x = b.x # E: Cannot resolve attribute "x" (possible cyclic definition) \
446 # E: Module has no attribute "x"
447 [file b.py]
448 import a
449 x = a.x
450 [builtins fixtures/module.pyi]
451
452 [case testNewAnalyzerMutuallyRecursiveOverloadedFunctions]
453 from typing import overload, Union
454
455 def main() -> None:
456 @overload
457 def f(x: int) -> int: ...
458 @overload
459 def f(x: str) -> str: ...
460 def f(x: Union[int, str]) -> Union[int, str]:
461 reveal_type(g(str())) # N: Revealed type is "builtins.str"
462 return x
463 @overload
464 def g(x: int) -> int: ...
465 @overload
466 def g(x: str) -> str: ...
467 def g(x: Union[int, str]) -> Union[int, str]:
468 reveal_type(f(int())) # N: Revealed type is "builtins.int"
469 return float() # E: Incompatible return value type (got "float", expected "int | str")
470
471 [case testNewAnalyzerNestedClassInMethod]
472 class C:
473 class D:
474 def meth(self) -> None:
475 x: Out.In
476 reveal_type(x.t) # N: Revealed type is "builtins.int"
477 class Out:
478 class In:
479 def meth(self) -> None:
480 self.t: int
481
482 [case testNewAnalyzerDeeplyNestedFunctions]
483 class Out:
484 class In:
485 def meth(self) -> None:
486 x: C.D
487 reveal_type(x.t) # N: Revealed type is "__main__.Test@10"
488 class C:
489 class D:
490 def meth(self) -> None:
491 self.t: Test
492 class Test:
493 def test(self) -> None:
494 def one() -> int:
495 reveal_type(other()) # N: Revealed type is "builtins.str"
496 return int()
497 def other() -> str:
498 reveal_type(one()) # N: Revealed type is "builtins.int"
499 return str()
500
501 [case testNewAnalyzerNestedClass1]
502 class A:
503 class B:
504 x: int
505
506 def __init__(self, x: int) -> None:
507 self.x = x
508
509 def f(self) -> str:
510 return self.x # E: Incompatible return value type (got "int", expected "str")
511
512 b: A.B
513 b = A.B('') # E: Argument 1 to "B" has incompatible type "str"; expected "int"
514 reveal_type(b) # N: Revealed type is "__main__.A.B"
515 reveal_type(b.x) # N: Revealed type is "builtins.int"
516 reveal_type(b.f()) # N: Revealed type is "builtins.str"
517
518 [case testNewAnalyzerNestedClass2]
519 class A:
520 class B:
521 x: int
522
523 def __init__(self, x: int) -> None:
524 self.x = x
525
526 def f(self) -> str:
527 return self.x # E: Incompatible return value type (got "int", expected "str")
528
529 b: A.B
530 b = A.B('') # E: Argument 1 to "B" has incompatible type "str"; expected "int"
531 reveal_type(b) # N: Revealed type is "__main__.A.B"
532 reveal_type(b.x) # N: Revealed type is "builtins.int"
533 reveal_type(b.f()) # N: Revealed type is "builtins.str"
534 [case testNewAnalyzerGenerics]
535 from typing import TypeVar, Generic
536
537 T = TypeVar('T')
538
539 class C(Generic[T]):
540 def __init__(self, x: T) -> None:
541 self.x = x
542
543 def get(self) -> T:
544 return self.x
545
546 c: C[int]
547 c2: C[int, str] # E: "C" expects 1 type argument, but 2 given
548 c3: C
549 c = C('') # E: Argument 1 to "C" has incompatible type "str"; expected "int"
550 reveal_type(c.get()) # N: Revealed type is "builtins.int"
551 reveal_type(c2) # N: Revealed type is "__main__.C[Any]"
552 reveal_type(c3) # N: Revealed type is "__main__.C[Any]"
553 [case testNewAnalyzerGenericsTypeVarForwardRef]
554 from typing import TypeVar, Generic
555
556 class C(Generic[T]):
557 def __init__(self, x: T) -> None:
558 self.x = x
559
560 def get(self) -> T:
561 return self.x
562
563 T = TypeVar('T')
564
565 c: C[int]
566 reveal_type(c) # N: Revealed type is "__main__.C[builtins.int]"
567 c = C('') # E: Argument 1 to "C" has incompatible type "str"; expected "int"
568 reveal_type(c.get()) # N: Revealed type is "builtins.int"
569
570 [case testNewAnalyzerTypeAlias]
571 from typing import Union, TypeVar, Generic
572
573 T = TypeVar('T')
574 S = TypeVar('S')
575 class D(Generic[T, S]): pass
576
577 class C: pass
578
579 C2 = C
580 U = Union[C, int]
581 G = D[T, C]
582
583 c: C2
584 reveal_type(c) # N: Revealed type is "__main__.C"
585 u: U
586 reveal_type(u) # N: Revealed type is "__main__.C | builtins.int"
587 g: G[int]
588 reveal_type(g) # N: Revealed type is "__main__.D[builtins.int, __main__.C]"
589 [case testNewAnalyzerTypeAlias2]
590 from typing import Union
591
592 class C(D): pass
593
594 A = Union[C, int]
595 x: A
596 reveal_type(x) # N: Revealed type is "__main__.C | builtins.int"
597
598 class D: pass
599
600 [case testNewAnalyzerBuiltinTypeAliases]
601 from typing import List
602
603 x: List[C]
604 reveal_type(x) # N: Revealed type is "builtins.list[__main__.C]"
605
606 class C: pass
607 [builtins fixtures/list.pyi]
608
609 [case testNewAnalyzerVersionCheck]
610 import sys
611
612 if sys.version_info[0] < 2:
613 1()
614 import nonexistent
615 else:
616 def f(x: int) -> None: pass
617
618 f('') # E: Argument 1 to "f" has incompatible type "str"; expected "int"
619
620 def g() -> None:
621 if sys.version_info[0] < 3:
622 import nonexistent2
623 else:
624 1() # E: "int" not callable
625 [builtins fixtures/ops.pyi]
626
627 [case testNewAnalyzerVersionCheck2]
628 import sys
629
630 assert sys.version_info[0] == 3
631 1() # E: "int" not callable
632 assert sys.version_info[0] < 3
633 ''()
634 [builtins fixtures/ops.pyi]
635
636 [case testNewAnalyzerOverload]
637 from typing import overload, Union
638
639 @overload
640 def f(x: int) -> int: ...
641 @overload
642 def f(x: str) -> str: ...
643 def f(x: Union[int, str]) -> Union[int, str]:
644 return 1.0 # E: Incompatible return value type (got "float", expected "int | str")
645
646 f(1)
647 f('')
648 f(1.0) # E: No overload variant of "f" matches argument type "float" \
649 # N: Possible overload variants: \
650 # N: def f(x: int) -> int \
651 # N: def f(x: str) -> str
652
653 [case testNewAnalyzerOverload2]
654 from typing import overload, Union
655
656 class A:
657 @overload
658 def f(self, x: int) -> int: ...
659 @overload
660 def f(self, x: str) -> str: ...
661 def f(self, x: Union[int, str]) -> Union[int, str]:
662 return 1.0 # E: Incompatible return value type (got "float", expected "int | str")
663
664 a = A()
665 a.f(1)
666 a.f('')
667 a.f(1.0) # E: No overload variant of "f" of "A" matches argument type "float" \
668 # N: Possible overload variants: \
669 # N: def f(self, x: int) -> int \
670 # N: def f(self, x: str) -> str
671
672 [case testNewAnalyzerPromotion]
673 def f(x: float) -> None: pass
674 y: int
675 f(y)
676 f(1)
677 [builtins fixtures/primitives.pyi]
678
679 [case testNewAnalyzerFunctionDecorator]
680 # flags: --disable-error-code used-before-def
681 from typing import Callable
682
683 @dec
684 def f1(x: int) -> int:
685 return '' # E: Incompatible return value type (got "str", expected "int")
686
687 def dec(f: Callable[[int], int]) -> Callable[[str], str]: ...
688
689 @dec
690 def f2(x: int) -> int:
691 return '' # E: Incompatible return value type (got "str", expected "int")
692
693 f1(1) # E: Argument 1 to "f1" has incompatible type "int"; expected "str"
694 reveal_type(f1('')) # N: Revealed type is "builtins.str"
695 f2(1) # E: Argument 1 to "f2" has incompatible type "int"; expected "str"
696
697 [case testNewAnalyzerTypeVarForwardReference]
698 # flags: --disable-error-code used-before-def
699 from typing import TypeVar, Generic
700
701 T = TypeVar('T')
702 XY = TypeVar('XY', X, Y)
703
704 class C(Generic[T]): pass
705
706 class D(C[XY], Generic[XY]): pass
707
708 class X: pass
709 class Y: pass
710
711 x: D[int] # E: Value of type variable "XY" of "D" cannot be "int"
712 y: D[Y]
713
714 [case testNewAnalyzerTypeVarForwardReference2]
715 from typing import TypeVar, Generic
716
717 T = TypeVar('T')
718 XY = TypeVar('XY', 'X', 'Y')
719
720 class C(Generic[T]): pass
721
722 class D(C[XY]): pass
723
724 class X: pass
725 class Y: pass
726
727 x: D[int] # E: Value of type variable "XY" of "D" cannot be "int"
728 y: D[Y]
729
730 [case testNewAnalyzerTypeVarForwardReferenceValuesDeferred]
731 from typing import TypeVar, Generic
732
733 T = TypeVar('T')
734 XY = TypeVar('XY', 'X', 'Y')
735
736 class C(Generic[T]): pass
737
738 class D(C[XY], Generic[XY]): pass
739
740 class X(Defer): pass
741 class Y(Defer): pass
742 class Defer: ...
743
744 x: D[int] # E: Value of type variable "XY" of "D" cannot be "int"
745 y: D[Y]
746 [builtins fixtures/list.pyi]
747
748 [case testNewAnalyzerTypeVarForwardReferenceBoundDeferred]
749 from typing import TypeVar, Generic
750
751 T = TypeVar('T')
752 TY = TypeVar('TY', bound='Y')
753
754 class C(Generic[T]): pass
755
756 class D(C[TY], Generic[TY]): pass
757
758 class Y(Defer): pass
759 class Defer: ...
760
761 x: D[int] # E: Type argument "int" of "D" must be a subtype of "Y"
762 y: D[Y]
763
764 [case testNewAnalyzerTypeVarForwardReferenceErrors]
765 from typing import TypeVar, Generic
766
767 class C(Generic[T]):
768 def __init__(self, x: T) -> None: ...
769 def func(x: U) -> U: ...
770
771 U = TypeVar('U', asdf, asdf) # E: Name "asdf" is not defined
772 T = TypeVar('T', bound='asdf') # E: Name "asdf" is not defined
773
774 reveal_type(C) # N: Revealed type is "def [T <: Any] (x: T`1) -> __main__.C[T`1]"
775 reveal_type(func) # N: Revealed type is "def [U in (Any, Any)] (x: U`-1) -> U`-1"
776
777 [case testNewAnalyzerSubModuleInCycle]
778 import a
779 [file a.py]
780 MYPY = False
781 if MYPY:
782 from b.c import x
783 [file b/__init__.pyi]
784 import b.c
785 [file b/c.pyi]
786 x = 0
787 import a
788
789 [case testNewAnalyzerBaseClassSelfReference]
790 from typing import TypeVar, Generic
791
792 T = TypeVar('T')
793
794 class A(Generic[T]): pass
795
796 class C(A[C]):
797 pass
798
799 class D(A['D']):
800 pass
801
802 a1: A[C] = C()
803 a2: A[D] = C() \
804 # E: Incompatible types in assignment (expression has type "C", variable has type "A[D]")
805
806 [case testNewAnalyzerTypeVarBoundForwardRef]
807 from typing import TypeVar
808
809 T = TypeVar('T', bound='C')
810
811 class C: pass
812 class D(C): pass
813 class E: pass
814
815 def f(x: T) -> T:
816 return x
817
818 reveal_type(f(D())) # N: Revealed type is "__main__.D"
819 f(E()) # E: Value of type variable "T" of "f" cannot be "E"
820
821 [case testNewAnalyzerNameExprRefersToIncompleteType]
822 import a
823
824 [file a.py]
825 from b import f
826
827 class C(D): pass
828 class D: pass
829
830 [file b.py]
831 from a import C
832 reveal_type(C()) # N: Revealed type is "a.C"
833 def f(): pass
834
835 [case testNewAnalyzerMemberExprRefersToIncompleteType]
836 import a
837
838 [file a.py]
839 from b import f
840
841 class C(D): pass
842 class D: pass
843
844 [file b.py]
845 import a
846 reveal_type(a.C()) # N: Revealed type is "a.C"
847 def f(): pass
848
849 [case testNewAnalyzerNamedTupleCall]
850 from typing import NamedTuple
851
852
853 class Other: pass
854 In = NamedTuple('In', [('s', str), ('t', Other)])
855 Out = NamedTuple('Out', [('x', In), ('y', Other)])
856 o: Out
857 i: In
858 reveal_type(o) # N: Revealed type is "tuple[tuple[builtins.str, __main__.Other, fallback=__main__.In], __main__.Other, fallback=__main__.Out]"
859 reveal_type(o.x) # N: Revealed type is "tuple[builtins.str, __main__.Other, fallback=__main__.In]"
860 reveal_type(o.y) # N: Revealed type is "__main__.Other"
861 reveal_type(o.x.t) # N: Revealed type is "__main__.Other"
862 reveal_type(i.t) # N: Revealed type is "__main__.Other"
863 [builtins fixtures/tuple.pyi]
864
865 [case testNewAnalyzerNamedTupleClass]
866 from typing import NamedTuple
867
868 o: Out
869 i: In
870
871 class Out(NamedTuple):
872 x: In
873 y: Other
874
875 reveal_type(o) # N: Revealed type is "tuple[tuple[builtins.str, __main__.Other, fallback=__main__.In], __main__.Other, fallback=__main__.Out]"
876 reveal_type(o.x) # N: Revealed type is "tuple[builtins.str, __main__.Other, fallback=__main__.In]"
877 reveal_type(o.y) # N: Revealed type is "__main__.Other"
878 reveal_type(o.x.t) # N: Revealed type is "__main__.Other"
879 reveal_type(i.t) # N: Revealed type is "__main__.Other"
880
881 class In(NamedTuple):
882 s: str
883 t: Other
884 class Other: pass
885 [builtins fixtures/tuple.pyi]
886
887 [case testNewAnalyzerNamedTupleCallNested]
888 from typing import NamedTuple
889
890 o: C.Out
891 i: C.In
892
893 reveal_type(o) # N: Revealed type is "tuple[tuple[builtins.str, __main__.C.Other, fallback=__main__.C.In], __main__.C.Other, fallback=__main__.C.Out]"
894 reveal_type(o.x) # N: Revealed type is "tuple[builtins.str, __main__.C.Other, fallback=__main__.C.In]"
895 reveal_type(o.y) # N: Revealed type is "__main__.C.Other"
896 reveal_type(o.x.t) # N: Revealed type is "__main__.C.Other"
897 reveal_type(i.t) # N: Revealed type is "__main__.C.Other"
898
899 class C:
900 In = NamedTuple('In', [('s', str), ('t', Other)])
901 Out = NamedTuple('Out', [('x', In), ('y', Other)])
902 class Other: pass
903 [builtins fixtures/tuple.pyi]
904
905
906 [case testNewAnalyzerNamedTupleClassNested]
907 from typing import NamedTuple
908
909 o: C.Out
910 i: C.In
911
912 reveal_type(o) # N: Revealed type is "tuple[tuple[builtins.str, __main__.C.Other, fallback=__main__.C.In], __main__.C.Other, fallback=__main__.C.Out]"
913 reveal_type(o.x) # N: Revealed type is "tuple[builtins.str, __main__.C.Other, fallback=__main__.C.In]"
914 reveal_type(o.y) # N: Revealed type is "__main__.C.Other"
915 reveal_type(o.x.t) # N: Revealed type is "__main__.C.Other"
916 reveal_type(i.t) # N: Revealed type is "__main__.C.Other"
917
918 class C:
919 class Out(NamedTuple):
920 x: C.In
921 y: C.Other
922 class In(NamedTuple):
923 s: str
924 t: C.Other
925 class Other: pass
926 [builtins fixtures/tuple.pyi]
927
928 [case testNewAnalyzerNamedTupleCallNestedMethod]
929 from typing import NamedTuple
930
931 class C:
932 def get_tuple(self) -> None:
933 Out = NamedTuple('Out', [('x', 'In'), ('y', 'Other')])
934 In = NamedTuple('In', [('s', str), ('t', 'Other')])
935 class Other: pass
936 self.o: Out
937
938 c = C()
939 reveal_type(c.o) # N: Revealed type is "tuple[tuple[builtins.str, __main__.Other@7, fallback=__main__.In@6], __main__.Other@7, fallback=__main__.Out@5]"
940 reveal_type(c.o.x) # N: Revealed type is "tuple[builtins.str, __main__.Other@7, fallback=__main__.In@6]"
941 [builtins fixtures/tuple.pyi]
942
943 [case testNewAnalyzerNamedTupleClassNestedMethod]
944 from typing import NamedTuple
945
946 class C:
947 def get_tuple(self) -> None:
948 class Out(NamedTuple):
949 x: In
950 y: Other
951 def method(self) -> In: ...
952 class In(NamedTuple):
953 s: str
954 t: Other
955 class Other: pass
956 self.o: Out
957
958 c = C()
959 reveal_type(c.o) # N: Revealed type is "tuple[tuple[builtins.str, __main__.Other@12, fallback=__main__.In@9], __main__.Other@12, fallback=__main__.Out@5]"
960 reveal_type(c.o.x) # N: Revealed type is "tuple[builtins.str, __main__.Other@12, fallback=__main__.In@9]"
961 reveal_type(c.o.method()) # N: Revealed type is "tuple[builtins.str, __main__.Other@12, fallback=__main__.In@9]"
962 [builtins fixtures/tuple.pyi]
963
964 [case testNewAnalyzerNamedTupleClassForwardMethod]
965 from typing import NamedTuple
966
967 n: NT
968 reveal_type(n.get_other()) # N: Revealed type is "tuple[builtins.str, fallback=__main__.Other]"
969 reveal_type(n.get_other().s) # N: Revealed type is "builtins.str"
970
971 class NT(NamedTuple):
972 x: int
973 y: int
974 def get_other(self) -> Other: pass
975
976 class Other(NamedTuple):
977 s: str
978 [builtins fixtures/tuple.pyi]
979
980 [case testNewAnalyzerNamedTupleSpecialMethods]
981 from typing import NamedTuple
982
983 class Other: pass
984 In = NamedTuple('In', [('s', str), ('t', Other)])
985 Out = NamedTuple('Out', [('x', In), ('y', Other)])
986 class SubO(Out): pass
987
988 o: SubO
989
990 reveal_type(SubO._make) # N: Revealed type is "def (iterable: typing.Iterable[Any]) -> tuple[tuple[builtins.str, __main__.Other, fallback=__main__.In], __main__.Other, fallback=__main__.SubO]"
991 reveal_type(o._replace(y=Other())) # N: Revealed type is "tuple[tuple[builtins.str, __main__.Other, fallback=__main__.In], __main__.Other, fallback=__main__.SubO]"
992 [builtins fixtures/tuple.pyi]
993
994 [case testNewAnalyzerNamedTupleBaseClass]
995 from typing import NamedTuple
996 class Other: pass
997 class In(NamedTuple):
998 s: str
999 t: Other
1000 class Out(NamedTuple('Out', [('x', In), ('y', Other)])):
1001 pass
1002
1003 o: Out
1004 reveal_type(o) # N: Revealed type is "tuple[tuple[builtins.str, __main__.Other, fallback=__main__.In], __main__.Other, fallback=__main__.Out]"
1005 reveal_type(o.x) # N: Revealed type is "tuple[builtins.str, __main__.Other, fallback=__main__.In]"
1006 reveal_type(o.x.t) # N: Revealed type is "__main__.Other"
1007 reveal_type(Out._make) # N: Revealed type is "def (iterable: typing.Iterable[Any]) -> tuple[tuple[builtins.str, __main__.Other, fallback=__main__.In], __main__.Other, fallback=__main__.Out]"
1008 [builtins fixtures/tuple.pyi]
1009
1010 [case testNewAnalyzerIncompleteRefShadowsBuiltin1]
1011 import a
1012
1013 [file a.py]
1014 from typing import TypeVar, Generic
1015
1016 from b import C as int
1017
1018 x: int[str]
1019
1020 reveal_type(x) # N: Revealed type is "a.C[builtins.str]"
1021
1022 T = TypeVar('T')
1023 class C(Generic[T]): pass
1024
1025 [file b.py]
1026 from a import C
1027
1028 [case testNewAnalyzerIncompleteRefShadowsBuiltin2]
1029 import b
1030
1031 [file a.py]
1032 import b
1033
1034 int = b.C
1035
1036 class C: pass
1037
1038 x: int
1039 reveal_type(x) # N: Revealed type is "b.C"
1040
1041 [file b.py]
1042 import a
1043
1044 int = a.C
1045
1046 class C: pass
1047
1048 x: int
1049 reveal_type(x) # N: Revealed type is "a.C"
1050
1051 [case testNewAnalyzerNamespaceCompleteness]
1052 import a
1053
1054 [file a.py]
1055 import b
1056
1057 x: b.C
1058
1059 [file b.py]
1060 from c import *
1061 class C: pass
1062
1063 [file c.py]
1064 import a
1065 from b import C
1066
1067 [targets c, b, a, c, b, __main__]
1068
1069 [case testNewAnalyzerImportOverExistingInCycle]
1070 import a
1071 [file a.py]
1072 C = 1
1073 from b import C # E: Incompatible import of "C" (imported name has type "type[C]", local name has type "int")
1074
1075 [file b.py]
1076 import a
1077
1078 class C(B): ...
1079 class B: ...
1080
1081 [case testNewAnalyzerImportOverExistingInCycleStar1]
1082 import a
1083 [file a.py]
1084 C = 1
1085 MYPY = False
1086 if MYPY: # Tweak processing order
1087 from b import * # E: Incompatible import of "C" (imported name has type "type[C]", local name has type "int")
1088
1089 [file b.py]
1090 import a
1091
1092 class C(B): ...
1093 class B: ...
1094
1095 [case testNewAnalyzerImportOverExistingInCycleStar2]
1096 import a
1097 [file a.py]
1098 C = 1
1099 from b import * # E: Incompatible import of "C" (imported name has type "type[C]", local name has type "int")
1100
1101 [file b.py]
1102 MYPY = False
1103 if MYPY: # Tweak processing order
1104 import a
1105
1106 class C(B): ...
1107 class B: ...
1108
1109 [case testNewAnalyzerIncompleteFixture]
1110 from typing import Tuple
1111
1112 x: Tuple[int] # E: Name "tuple" is not defined
1113 [builtins fixtures/complex.pyi]
1114
1115 [case testNewAnalyzerMetaclass1]
1116 class A(metaclass=B):
1117 pass
1118
1119 class B(type):
1120 def f(cls) -> int:
1121 return 0
1122
1123 reveal_type(A.f()) # N: Revealed type is "builtins.int"
1124
1125 [case testNewAnalyzerMetaclass2]
1126 class B(type):
1127 def f(cls) -> int:
1128 return 0
1129
1130 class C: pass
1131
1132 class A(metaclass=B):
1133 pass
1134
1135 class AA(metaclass=C): # E: Metaclasses not inheriting from "type" are not supported
1136 pass
1137
1138 reveal_type(A.f()) # N: Revealed type is "builtins.int"
1139 [case testNewAnalyzerMetaclassPlaceholder]
1140 class B(C): pass
1141
1142 class A(metaclass=B):
1143 pass
1144
1145 class C(type):
1146 def f(cls) -> int:
1147 return 0
1148
1149 reveal_type(A.f()) # N: Revealed type is "builtins.int"
1150
1151 [case testNewAnalyzerMetaclassSix1]
1152 import six
1153
1154 class A(six.with_metaclass(B)):
1155 pass
1156
1157 class B(type):
1158 def f(cls) -> int:
1159 return 0
1160
1161 reveal_type(A.f()) # N: Revealed type is "builtins.int"
1162 [builtins fixtures/tuple.pyi]
1163
1164 [case testNewAnalyzerMetaclassSix2]
1165 import six
1166
1167 @six.add_metaclass(B)
1168 class A:
1169 pass
1170
1171 class B(type):
1172 def f(cls) -> int:
1173 return 0
1174
1175 reveal_type(A.f()) # N: Revealed type is "builtins.int"
1176 [builtins fixtures/tuple.pyi]
1177
1178 [case testNewAnalyzerMetaclassSix3]
1179 import six
1180
1181 class A(six.with_metaclass(B, Defer)):
1182 pass
1183
1184 class B(type):
1185 def f(cls) -> int:
1186 return 0
1187
1188 class Defer:
1189 x: str
1190
1191 reveal_type(A.f()) # N: Revealed type is "builtins.int"
1192 reveal_type(A.x) # N: Revealed type is "builtins.str"
1193 [builtins fixtures/tuple.pyi]
1194
1195 [case testNewAnalyzerMetaclassSix4]
1196 import six
1197
1198 class B(type):
1199 def f(cls) -> int:
1200 return 0
1201
1202 class A(six.with_metaclass(B, Defer)):
1203 pass
1204
1205 class Defer:
1206 x: str
1207
1208 reveal_type(A.f()) # N: Revealed type is "builtins.int"
1209 reveal_type(A.x) # N: Revealed type is "builtins.str"
1210 [builtins fixtures/tuple.pyi]
1211
1212 [case testNewAnalyzerMetaclassFuture1]
1213 import future.utils
1214
1215 class A(future.utils.with_metaclass(B)):
1216 pass
1217
1218 class B(type):
1219 def f(cls) -> int:
1220 return 0
1221
1222 reveal_type(A.f()) # N: Revealed type is "builtins.int"
1223 [builtins fixtures/tuple.pyi]
1224
1225 [case testNewAnalyzerMetaclassFuture3]
1226 import future.utils
1227
1228 class A(future.utils.with_metaclass(B, Defer)):
1229 pass
1230
1231 class B(type):
1232 def f(cls) -> int:
1233 return 0
1234
1235 class Defer:
1236 x: str
1237
1238 reveal_type(A.f()) # N: Revealed type is "builtins.int"
1239 reveal_type(A.x) # N: Revealed type is "builtins.str"
1240 [builtins fixtures/tuple.pyi]
1241
1242 [case testNewAnalyzerMetaclassFuture4]
1243 # flags: --disable-error-code used-before-def
1244 import future.utils
1245
1246 class B(type):
1247 def f(cls) -> int:
1248 return 0
1249
1250 reveal_type(A.f()) # N: Revealed type is "builtins.int"
1251 reveal_type(A.x) # N: Revealed type is "builtins.str"
1252
1253 class A(future.utils.with_metaclass(B, Defer)):
1254 pass
1255
1256 class Defer:
1257 x: str
1258 [builtins fixtures/tuple.pyi]
1259
1260 [case testNewAnalyzerFinalDefiningModuleVar]
1261 from typing import Final
1262
1263 class D(C): ...
1264 class C: ...
1265
1266 x: Final = C()
1267 y: Final[C] = D()
1268 bad: Final[D] = C() # E: Incompatible types in assignment (expression has type "C", variable has type "D")
1269
1270 reveal_type(x) # N: Revealed type is "__main__.C"
1271 reveal_type(y) # N: Revealed type is "__main__.C"
1272 [case testNewAnalyzerFinalDefiningInstanceVar]
1273 from typing import Final
1274
1275 class D: ...
1276 class E(C): ...
1277
1278 class C:
1279 def __init__(self, x: D) -> None:
1280 self.x: Final = x
1281 self.y: Final[C] = E(D())
1282 reveal_type(C(D()).x) # N: Revealed type is "__main__.D"
1283 reveal_type(C(D()).y) # N: Revealed type is "__main__.C"
1284 [case testNewAnalyzerFinalReassignModuleVar]
1285 from typing import Final
1286
1287 class A: ...
1288
1289 x: Final = A()
1290 x = A() # E: Cannot assign to final name "x"
1291
1292 x2: Final = A()
1293 def f2() -> None:
1294 global x2
1295 def f() -> None:
1296 g()
1297 x2 = A() # E: Cannot assign to final name "x2"
1298 def g() -> None:
1299 f()
1300
1301 [case testNewAnalyzerFinalReassignModuleReexport]
1302 import a
1303 [file a.py]
1304 from b import ID, A
1305
1306 class C(A): ...
1307 ID = C() # E: Cannot assign to final name "ID"
1308 [file b.py]
1309 from typing import Final
1310 from a import C
1311
1312 class A:
1313 x: C
1314
1315 ID: Final = A()
1316
1317 [case testNewAnalyzerFinalOverrideInSubclass]
1318 from typing import Final
1319
1320 class B:
1321 def __init__(self, x: int) -> None:
1322 self.x: Final = x
1323
1324 class C(B):
1325 x = 1 # E: Cannot assign to final name "x"
1326
1327 [case testNewAnalyzerAssignmentAfterStarImport]
1328 import a
1329 [file a.py]
1330 from b import *
1331
1332 x = 1
1333 def f(): ...
1334 [file b.py]
1335 from a import f
1336 x: int
1337
1338 [case testNewAnalyzerClassLevelImport]
1339 # flags: --ignore-missing-imports
1340 class Test:
1341 import a
1342 def __init__(self) -> None:
1343 some_module = self.a
1344
1345 [case testNewAnalyzerAliasToNotReadyClass]
1346 import a
1347 [file a.py]
1348 from b import B
1349
1350 x: A
1351 A = B
1352 [file b.py]
1353 from typing import List
1354 from a import x
1355
1356 class B(List[B]): pass
1357
1358 reveal_type(x[0][0]) # N: Revealed type is "b.B"
1359 [builtins fixtures/list.pyi]
1360
1361 [case testNewAnalyzerAliasToNotReadyClass2]
1362 from typing import List
1363
1364 x: A
1365
1366 class A(List[B]): pass
1367 B = A
1368
1369 reveal_type(x[0][0]) # N: Revealed type is "__main__.A"
1370 [builtins fixtures/list.pyi]
1371
1372 [case testNewAnalyzerAliasToNotReadyClass3]
1373 # flags: --disable-error-code used-before-def
1374 from typing import List
1375
1376 x: B
1377 B = A
1378 A = C
1379 class C(List[B]): pass
1380
1381 reveal_type(x[0][0]) # N: Revealed type is "__main__.C"
1382 [builtins fixtures/list.pyi]
1383
1384 [case testNewAnalyzerAliasToNotReadyNestedClass]
1385 import a
1386 [file a.py]
1387 from b import Out
1388
1389 x: A
1390 A = Out.B
1391 [file b.py]
1392 from typing import List
1393 from a import x
1394
1395 class Out:
1396 class B(List[B]): pass
1397
1398 reveal_type(x[0][0]) # N: Revealed type is "b.Out.B"
1399 [builtins fixtures/list.pyi]
1400
1401 [case testNewAnalyzerAliasToNotReadyNestedClass2]
1402 from typing import List
1403
1404 x: Out.A
1405
1406 class Out:
1407 class A(List[B]): pass
1408 B = Out.A
1409
1410 reveal_type(x[0][0]) # N: Revealed type is "__main__.Out.A"
1411 [builtins fixtures/list.pyi]
1412
1413 [case testNewAnalyzerAliasToNotReadyClassGeneric]
1414 import a
1415 [file a.py]
1416 from typing import Tuple
1417 from b import B, T
1418
1419 x: A[int]
1420 A = B[Tuple[T, T]]
1421 [file b.py]
1422 from typing import List, Generic, TypeVar
1423 from a import x
1424
1425 class B(List[B], Generic[T]): pass
1426 T = TypeVar('T')
1427 reveal_type(x) # N: Revealed type is "b.B[tuple[builtins.int, builtins.int]]"
1428 [builtins fixtures/list.pyi]
1429
1430 [case testNewAnalyzerAliasToNotReadyClassInGeneric]
1431 import a
1432 [file a.py]
1433 from typing import Tuple
1434 from b import B
1435
1436 x: A
1437 A = Tuple[B, B]
1438 [file b.py]
1439 from typing import List
1440 from a import x
1441
1442 class B(List[B]): pass
1443
1444 reveal_type(x) # N: Revealed type is "tuple[b.B, b.B]"
1445 [builtins fixtures/list.pyi]
1446
1447 [case testNewAnalyzerAliasToNotReadyClassDoubleGeneric]
1448 from typing import List, TypeVar, Union
1449
1450 T = TypeVar('T')
1451
1452 x: B[int]
1453 A = Union[int, T]
1454 B = A[List[T]]
1455 class C(List[B[int]]): pass
1456
1457 y: C
1458 reveal_type(x) # N: Revealed type is "builtins.int | builtins.list[builtins.int]"
1459 reveal_type(y[0]) # N: Revealed type is "builtins.int | builtins.list[builtins.int]"
1460 [builtins fixtures/list.pyi]
1461
1462 [case testNewAnalyzerForwardAliasFromUnion]
1463 from typing import Union, List
1464
1465 A = Union['B', 'C']
1466
1467 class D:
1468 x: List[A]
1469
1470 def test(self) -> None:
1471 reveal_type(self.x[0].y) # N: Revealed type is "builtins.int"
1472
1473 class B:
1474 y: int
1475 class C:
1476 y: int
1477 [builtins fixtures/list.pyi]
1478
1479 [case testNewAnalyzerAliasToNotReadyTwoDeferrals]
1480 # flags: --disable-error-code used-before-def
1481 from typing import List
1482
1483 x: B
1484 B = List[C]
1485 A = C
1486 class C(List[A]): pass
1487
1488 reveal_type(x) # N: Revealed type is "builtins.list[__main__.C]"
1489 reveal_type(x[0][0]) # N: Revealed type is "__main__.C"
1490 [builtins fixtures/list.pyi]
1491
1492 [case testNewAnalyzerAliasToNotReadyDirectBase]
1493 # flags: --disable-error-code used-before-def
1494 from typing import List
1495
1496 def test() -> None:
1497 x: B
1498 B = List[C]
1499 class C(B): pass
1500
1501 reveal_type(x)
1502 reveal_type(x[0][0])
1503 [builtins fixtures/list.pyi]
1504 [out]
1505 main:5: error: Cannot resolve name "B" (possible cyclic definition)
1506 main:5: note: Recursive types are not allowed at function scope
1507 main:6: error: Cannot resolve name "B" (possible cyclic definition)
1508 main:6: note: Recursive types are not allowed at function scope
1509 main:6: error: Cannot resolve name "C" (possible cyclic definition)
1510 main:9: note: Revealed type is "Any"
1511 main:10: note: Revealed type is "Any"
1512
1513 [case testNewAnalyzerAliasToNotReadyTwoDeferralsFunction]
1514 # flags: --disable-error-code used-before-def
1515 import a
1516 [file a.py]
1517 from typing import List
1518 from b import D
1519
1520 def f(x: B) -> List[B]: ...
1521 B = List[C]
1522 A = C
1523 class C(List[A]): pass
1524 [file b.py]
1525 from a import f
1526 class D: ...
1527 reveal_type(f) # N: Revealed type is "def (x: builtins.list[a.C]) -> builtins.list[builtins.list[a.C]]"
1528 [builtins fixtures/list.pyi]
1529
1530 [case testNewAnalyzerAliasToNotReadyDirectBaseFunction]
1531 # flags: --disable-error-code used-before-def
1532 import a
1533 [file a.py]
1534 from typing import List
1535 from b import D
1536
1537 def f(x: B) -> List[B]: ...
1538 B = List[C]
1539 class C(B): pass
1540
1541 [file b.py]
1542 from a import f
1543 class D: ...
1544 reveal_type(f) # N: Revealed type is "def (x: builtins.list[a.C]) -> builtins.list[builtins.list[a.C]]"
1545 [builtins fixtures/list.pyi]
1546
1547 [case testNewAnalyzerAliasToNotReadyMixed]
1548 from typing import List, Union
1549 x: A
1550
1551 class B(List[A]): pass
1552 class C(List[A]): pass
1553
1554 A = Union[B, C]
1555
1556 reveal_type(x) # N: Revealed type is "__main__.B | __main__.C"
1557 reveal_type(x[0]) # N: Revealed type is "__main__.B | __main__.C"
1558 [builtins fixtures/list.pyi]
1559
1560 [case testNewAnalyzerTrickyAliasInFuncDef]
1561 import a
1562 [file a.py]
1563 from b import B
1564 def func() -> B: ...
1565 reveal_type(func()) # N: Revealed type is "builtins.list[tuple[b.C, b.C]]"
1566
1567 [file b.py]
1568 from typing import List, Tuple
1569 from a import func
1570
1571 class A: ...
1572 class C(A): ...
1573 B = List[Tuple[C, C]]
1574 [builtins fixtures/list.pyi]
1575
1576 [case testNewAnalyzerListComprehension]
1577 from typing import List
1578 class A: pass
1579 class B: pass
1580 a: List[A]
1581 a = [x for x in a]
1582 b: List[B] = [x for x in a] # E: List comprehension has incompatible type List[A]; expected List[B]
1583 [builtins fixtures/for.pyi]
1584
1585 [case testNewAnalyzerDictionaryComprehension]
1586 from typing import Dict, List, Tuple
1587 abd: Dict[A, B]
1588 abl: List[Tuple[A, B]]
1589 abd = {a: b for a, b in abl}
1590 x: Dict[B, A] = {a: b for a, b in abl} # E: Key expression in dictionary comprehension has incompatible type "A"; expected type "B" \
1591 # E: Value expression in dictionary comprehension has incompatible type "B"; expected type "A"
1592 y: A = {a: b for a, b in abl} # E: Incompatible types in assignment (expression has type "dict[A, B]", variable has type "A")
1593 class A: pass
1594 class B: pass
1595 [builtins fixtures/dict.pyi]
1596
1597 [case testNewAnalyzerTypeArgBoundCheck]
1598 from typing import TypeVar, Generic
1599
1600 class F(E): pass
1601 class E: pass
1602 T = TypeVar('T', bound=E)
1603 class C(Generic[T]): pass
1604
1605 class D(B): pass
1606
1607 x: C[D] # E: Type argument "D" of "C" must be a subtype of "E"
1608 y: C[F]
1609
1610 class B: pass
1611
1612 [case testNewAnalyzerTypeArgValueRestriction]
1613 from typing import TypeVar, Generic
1614
1615 class F(E): pass
1616 class E: pass
1617 T = TypeVar('T', E, str)
1618 class C(Generic[T]): pass
1619
1620 class D(B): pass
1621
1622 x: C[D] # E: Value of type variable "T" of "C" cannot be "D"
1623 y: C[E]
1624 z: C[str]
1625
1626 class B: pass
1627
1628 [case testNewAnalyzerTypeArgBoundCheckWithContext]
1629 # flags: --show-error-context
1630 import a
1631 [file a.py]
1632 from typing import TypeVar, Generic
1633
1634 T = TypeVar('T', bound=int)
1635 class C(Generic[T]): pass
1636
1637 def f(x: C[str]) -> None: # E
1638 y: C[str] # E
1639 class A(C[str]): # E
1640 z: C[str] # E
1641 def g(self, x: C[str]) -> None: # E
1642 a: C[str] # E
1643 [out]
1644 main:2: note: In module imported here:
1645 tmp/a.py: note: In function "f":
1646 tmp/a.py:6: error: Type argument "str" of "C" must be a subtype of "int"
1647 tmp/a.py:7: error: Type argument "str" of "C" must be a subtype of "int"
1648 tmp/a.py: note: In class "A":
1649 tmp/a.py:8: error: Type argument "str" of "C" must be a subtype of "int"
1650 tmp/a.py:9: error: Type argument "str" of "C" must be a subtype of "int"
1651 tmp/a.py: note: In member "g" of class "A":
1652 tmp/a.py:10: error: Type argument "str" of "C" must be a subtype of "int"
1653 tmp/a.py:11: error: Type argument "str" of "C" must be a subtype of "int"
1654
1655 [case testNewAnalyzerTypeArgBoundCheckDifferentNodes_no_native_parse]
1656 from typing import TypeVar, TypedDict, Generic, NamedTuple, NewType, Union, Any, cast, overload
1657
1658 T = TypeVar('T', bound=int)
1659 class C(Generic[T]): pass
1660 class C2(Generic[T]): pass
1661
1662 A = C[str] # E: Value of type variable "T" of "C" cannot be "str" \
1663 # E: Type argument "str" of "C" must be a subtype of "int"
1664 B = Union[C[str], int] # E: Type argument "str" of "C" must be a subtype of "int"
1665 S = TypeVar('S', bound=C[str]) # E: Type argument "str" of "C" must be a subtype of "int"
1666 U = TypeVar('U', C[str], str) # E: Type argument "str" of "C" must be a subtype of "int"
1667 N = NamedTuple('N', [
1668 ('x', C[str])]) # E: Type argument "str" of "C" must be a subtype of "int"
1669 class N2(NamedTuple):
1670 x: C[str] # E: Type argument "str" of "C" must be a subtype of "int"
1671 class TD(TypedDict):
1672 x: C[str] # E: Type argument "str" of "C" must be a subtype of "int"
1673 class TD2(TD):
1674 y: C2[str] # E: Type argument "str" of "C2" must be a subtype of "int"
1675 NT = NewType('NT',
1676 C[str]) # E: Type argument "str" of "C" must be a subtype of "int"
1677 class D(
1678 C[str]): # E: Type argument "str" of "C" must be a subtype of "int"
1679 pass
1680
1681 TD3 = TypedDict('TD3', {'x': C[str]}) # E: Type argument "str" of "C" must be a subtype of "int"
1682
1683 a: Any
1684 for i in a: # type: C[str] # E: Type argument "str" of "C" must be a subtype of "int"
1685 pass
1686
1687 with a as w: # type: C[str] # E: Type argument "str" of "C" must be a subtype of "int"
1688 pass
1689
1690 cast(C[str], a) # E: Type argument "str" of "C" must be a subtype of "int"
1691 C[str]() # E: Value of type variable "T" of "C" cannot be "str"
1692
1693 def f(s: S, y: U) -> None: pass # No error here
1694
1695 @overload
1696 def g(x: C[str]) -> int: ... # E: Type argument "str" of "C" must be a subtype of "int"
1697 @overload
1698 def g(x: int) -> int: ...
1699 def g(x: Union[C[str], int]) -> int: # E: Type argument "str" of "C" must be a subtype of "int"
1700 y: C[object] # E: Type argument "object" of "C" must be a subtype of "int"
1701 return 0
1702 [builtins fixtures/dict.pyi]
1703 [typing fixtures/typing-typeddict.pyi]
1704
1705 [case testNewAnalyzerTypeArgBoundCheckWithStrictOptional]
1706 # flags: --config-file tmp/mypy.ini
1707 import a
1708
1709 [file b.py]
1710 from typing import TypeVar, Generic
1711
1712 x: C[None]
1713 y: C[str] # E: Type argument "str" of "C" must be a subtype of "int"
1714 z: C[int]
1715
1716 T = TypeVar('T', bound=int)
1717 class C(Generic[T]):
1718 pass
1719
1720 [file a.py]
1721 from b import C
1722
1723 x: C[None] # E: Type argument "None" of "C" must be a subtype of "int"
1724 y: C[str] # E: Type argument "str" of "C" must be a subtype of "int"
1725 z: C[int]
1726
1727 [file mypy.ini]
1728 \[mypy-a]
1729 strict_optional = True
1730 \[mypy-b]
1731 strict_optional = False
1732
1733
1734 [case testNewAnalyzerTypeArgBoundCheckWithStrictOptionalPyProjectTOML]
1735 # flags: --config-file tmp/pyproject.toml
1736 import a
1737
1738 [file b.py]
1739 from typing import TypeVar, Generic
1740
1741 x: C[None]
1742 y: C[str] # E: Type argument "str" of "C" must be a subtype of "int"
1743 z: C[int]
1744
1745 T = TypeVar('T', bound=int)
1746 class C(Generic[T]):
1747 pass
1748
1749 [file a.py]
1750 from b import C
1751
1752 x: C[None] # E: Type argument "None" of "C" must be a subtype of "int"
1753 y: C[str] # E: Type argument "str" of "C" must be a subtype of "int"
1754 z: C[int]
1755
1756 [file pyproject.toml]
1757 \[[tool.mypy.overrides]]
1758 module = 'a'
1759 strict_optional = true
1760 \[[tool.mypy.overrides]]
1761 module = 'b'
1762 strict_optional = false
1763
1764
1765 [case testNewAnalyzerProperty]
1766 class A:
1767 @property
1768 def x(self) -> B:
1769 return 0 # E: Incompatible return value type (got "int", expected "B")
1770
1771 @property
1772 def y(self) -> B:
1773 pass
1774
1775 @y.setter
1776 def y(self, x: B) -> None:
1777 pass
1778
1779 class B: pass
1780
1781 a = A()
1782 reveal_type(a.x) # N: Revealed type is "__main__.B"
1783 a.y = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "B")
1784 [builtins fixtures/property.pyi]
1785
1786 [case testNewAnalyzerAliasesFixedFew]
1787 from typing import List, Generic, TypeVar
1788 T = TypeVar('T')
1789 class C(Generic[T]):
1790 ...
1791 A = List[C]
1792 x: A
1793
1794 def func(x: List[C[T]]) -> T:
1795 ...
1796
1797 reveal_type(x) # N: Revealed type is "builtins.list[__main__.C[Any]]"
1798 reveal_type(func(x)) # N: Revealed type is "Any"
1799 [builtins fixtures/list.pyi]
1800
1801 [case testNewAnalyzerAliasesFixedMany]
1802 from typing import List, Generic, TypeVar
1803
1804 T = TypeVar('T')
1805 class C(Generic[T]):
1806 ...
1807
1808 def func(x: List[C[T]]) -> T:
1809 ...
1810
1811 x: A
1812 A = List[C[int, str]] # E: "C" expects 1 type argument, but 2 given
1813
1814 reveal_type(x) # N: Revealed type is "builtins.list[__main__.C[Any]]"
1815 reveal_type(func(x)) # N: Revealed type is "Any"
1816
1817
1818 [builtins fixtures/list.pyi]
1819
1820 [case testNewAnalyzerBuiltinAliasesFixed]
1821 from typing import List, Optional
1822 x: Optional[List] = None
1823 y: List[str]
1824
1825 reveal_type(x) # N: Revealed type is "builtins.list[Any] | None"
1826 x = ['a', 'b']
1827 reveal_type(x) # N: Revealed type is "builtins.list[Any]"
1828 x.extend(y)
1829 [builtins fixtures/list.pyi]
1830
1831 [case testNewAnalyzerImportPriosB]
1832 import b
1833 [file a.py]
1834 from b import x
1835 reveal_type(x) # N: Revealed type is "tuple[builtins.int, builtins.int]"
1836 [file b.py]
1837 import a
1838 x = (1, 2)
1839 [builtins fixtures/tuple.pyi]
1840
1841 [case testNewAnalyzerImportPriosA]
1842 import a
1843 [file a.py]
1844 from b import x
1845 reveal_type(x) # N: Revealed type is "tuple[builtins.int, builtins.int]"
1846 [file b.py]
1847 import a
1848 x = (1, 2)
1849 [builtins fixtures/tuple.pyi]
1850
1851 [case testNewAnalyzerConditionalFunc]
1852 if int():
1853 def f(x: int) -> None:
1854 pass
1855 def g(x: int) -> None:
1856 pass
1857 elif bool():
1858 def f(x: int) -> None:
1859 1() # E: "int" not callable
1860 def g(x: str) -> None: # E: All conditional function variants must have identical signatures \
1861 # N: Original: \
1862 # N: def g(x: int) -> None \
1863 # N: Redefinition: \
1864 # N: def g(x: str) -> None
1865 pass
1866 else:
1867 def f(x: int) -> None:
1868 ''() # E: "str" not callable
1869
1870 reveal_type(g) # N: Revealed type is "def (x: builtins.int)"
1871
1872 [case testNewAnalyzerConditionalFuncDefer]
1873 if int():
1874 def f(x: A) -> None:
1875 pass
1876 def g(x: A) -> None:
1877 pass
1878 else:
1879 def f(x: A) -> None:
1880 1() # E: "int" not callable
1881 def g(x: str) -> None: # E: All conditional function variants must have identical signatures \
1882 # N: Original: \
1883 # N: def g(x: A) -> None \
1884 # N: Redefinition: \
1885 # N: def g(x: str) -> None
1886
1887 pass
1888
1889 reveal_type(g) # N: Revealed type is "def (x: __main__.A)"
1890
1891 class A: pass
1892
1893 [case testNewAnalyzerConditionalDecoratedFunc]
1894 from typing import Callable
1895
1896 def dec(f: Callable[[int], None]) -> Callable[[str], None]:
1897 pass
1898
1899 if int():
1900 from m import f
1901 else:
1902 @dec
1903 def f(x: int) -> None:
1904 1() # E: "int" not callable
1905 reveal_type(f) # N: Revealed type is "def (builtins.str)"
1906 [file m.py]
1907 def f(x: str, /) -> None: pass
1908
1909 [case testNewAnalyzerConditionallyDefineFuncOverVar]
1910 from typing import Callable
1911
1912 if int():
1913 f: Callable[[str], None]
1914 else:
1915 def f(x: str) -> None:
1916 ...
1917 reveal_type(f) # N: Revealed type is "def (builtins.str)"
1918
1919 [case testNewAnalyzerConditionallyDefineFuncOverClass]
1920 class C:
1921 1() # E: "int" not callable
1922 def C() -> None: # E: Name "C" already defined on line 1
1923 ''() # E: "str" not callable
1924
1925 [case testNewAnalyzerTupleIteration]
1926 from typing import Union, Tuple, NamedTuple
1927
1928 class T(Tuple[B, C]):
1929 pass
1930
1931 class A: pass
1932 class B(A): pass
1933 class C(A): pass
1934
1935 class NTInt(NamedTuple):
1936 x: int
1937 y: int
1938
1939 class NTStr(NamedTuple):
1940 x: str
1941 y: str
1942
1943 t1: T
1944 reveal_type(t1.__iter__) # N: Revealed type is "def () -> typing.Iterator[__main__.B | __main__.C]"
1945
1946 t2: NTInt
1947 reveal_type(t2.__iter__) # N: Revealed type is "def () -> typing.Iterator[builtins.int]"
1948 nt: Union[NTInt, NTStr]
1949 reveal_type(nt.__iter__) # N: Revealed type is "(def () -> typing.Iterator[builtins.int]) | (def () -> typing.Iterator[builtins.str])"
1950 for nx in nt:
1951 reveal_type(nx) # N: Revealed type is "builtins.int | builtins.str"
1952
1953 t: Union[Tuple[int, int], Tuple[str, str]]
1954 for x in t:
1955 reveal_type(x) # N: Revealed type is "builtins.int | builtins.str"
1956 [builtins fixtures/for.pyi]
1957
1958 [case testNewAnalyzerFallbackUpperBoundCheckAndFallbacks]
1959 from typing import TypeVar, Generic, Tuple
1960
1961 class A: pass
1962 class B: pass
1963 class C(B): pass
1964
1965 S = TypeVar('S', bound='Tuple[G[A], ...]')
1966
1967 class GG(Generic[S]): pass
1968
1969 g: GG[Tuple[G[B], G[C]]] # E: Type argument "tuple[G[B], G[C]]" of "GG" must be a subtype of "tuple[G[A], ...]" \
1970 # E: Type argument "B" of "G" must be a subtype of "A" \
1971 # E: Type argument "C" of "G" must be a subtype of "A"
1972
1973 T = TypeVar('T', bound=A, covariant=True)
1974
1975 class G(Generic[T]): pass
1976
1977 t: Tuple[G[B], G[C]] # E: Type argument "B" of "G" must be a subtype of "A" \
1978 # E: Type argument "C" of "G" must be a subtype of "A"
1979 reveal_type(t.__iter__) # N: Revealed type is "def () -> typing.Iterator[__main__.G[__main__.B]]"
1980 [builtins fixtures/tuple.pyi]
1981
1982 [case testNewAnalyzerClassKeywordsForward]
1983 class C(B, other=A): ...
1984 class B: ...
1985 class A: ...
1986
1987 [case testNewAnalyzerClassKeywordsCyclic]
1988 from typing import List
1989
1990 class C(List[C], other=C): ...
1991 [builtins fixtures/list.pyi]
1992
1993 [case testNewAnalyzerClassKeywordsError]
1994 class C(other=asdf): ... # E: Name "asdf" is not defined
1995
1996 [case testNewAnalyzerMissingImport]
1997 # flags: --ignore-missing-imports
1998 import non_existing
1999
2000 x: C
2001 class C: ...
2002
2003 [case testNewAnalyzerMissingImportFrom]
2004 # flags: --ignore-missing-imports
2005 from non_existing import stuff
2006
2007 x: C
2008 class C: ...
2009
2010 [case testNewAnalyzerFollowSkip]
2011 # flags: --follow-imports=skip
2012 from other import y
2013 x: C
2014 class C: ...
2015 [file other.py]
2016 y = 1
2017
2018 [case testNewAnalyzerMissingImportErrors]
2019 # flags: --ignore-missing-imports
2020 from non_existing import stuff, other_stuff
2021
2022 stuff = 1 # OK
2023 other_stuff: int = 1 # E: Name "other_stuff" already defined (possibly by an import)
2024
2025 x: C
2026 class C: ...
2027
2028 [case testNewAnalyzerMissingImportErrorsRedefinition]
2029 # flags: --ignore-missing-imports
2030
2031 class Other: ...
2032 from non_existing import Other # E: Name "Other" already defined on line 3
2033 from non_existing import Cls
2034 class Cls: ... # E: Name "Cls" already defined (possibly by an import)
2035
2036 x: C
2037 class C: ...
2038
2039 [case testNewAnalyzerTupleInit]
2040 from typing import Tuple
2041
2042 c: C
2043 class C(Tuple[int, str]):
2044 def __init__(self) -> None: pass
2045 [builtins fixtures/tuple.pyi]
2046
2047 [case testNewAnalyzerNotAnAlias]
2048 class Meta(type):
2049 x = int()
2050
2051 class C(metaclass=Meta):
2052 pass
2053
2054 y = C.x
2055 reveal_type(y) # N: Revealed type is "builtins.int"
2056
2057 [case testNewAnalyzerFunctionError]
2058 def f(x: asdf) -> None: # E: Name "asdf" is not defined
2059 pass
2060
2061 [case testNewAnalyzerEnumRedefinition]
2062 from enum import Enum
2063
2064 A = Enum('A', ['x', 'y'])
2065 A = Enum('A', ['z', 't']) # E: Name "A" already defined on line 3
2066 [builtins fixtures/tuple.pyi]
2067
2068 [case testNewAnalyzerNewTypeRedefinition]
2069 from typing import NewType
2070
2071 A = NewType('A', int)
2072 A = NewType('A', str) # E: Cannot redefine "A" as a NewType \
2073 # E: Name "A" already defined on line 3
2074
2075 [case testNewAnalyzerNewTypeForwardClass]
2076 from typing import NewType, List
2077
2078 x: C
2079 reveal_type(x[0]) # N: Revealed type is "__main__.C"
2080
2081 C = NewType('C', 'B')
2082
2083 class B(List[C]):
2084 pass
2085 [builtins fixtures/list.pyi]
2086
2087 [case testNewAnalyzerNewTypeForwardClassAlias]
2088 from typing import NewType, List
2089
2090 x: D
2091 reveal_type(x[0]) # N: Revealed type is "__main__.C"
2092
2093 C = NewType('C', 'B')
2094 D = C
2095
2096 class B(List[D]):
2097 pass
2098 [builtins fixtures/list.pyi]
2099
2100 [case testNewAnalyzerNewTypeForwardClassAliasReversed]
2101 from typing import NewType, List
2102
2103 x: D
2104 reveal_type(x[0][0]) # N: Revealed type is "__main__.C"
2105
2106 D = C # E: Name "C" is used before definition
2107 C = NewType('C', 'List[B]')
2108
2109 class B(List[C]):
2110 pass
2111 [builtins fixtures/list.pyi]
2112
2113 [case testNewAnalyzerNewTypeForwardClassAliasDirect]
2114 # flags: --disable-error-code used-before-def
2115 from typing import NewType, List
2116
2117 def test() -> None:
2118 x: D
2119 reveal_type(x[0][0])
2120
2121 D = List[C]
2122 C = NewType('C', 'B')
2123
2124 class B(D):
2125 pass
2126 [builtins fixtures/list.pyi]
2127 [out]
2128 main:5: error: Cannot resolve name "D" (possible cyclic definition)
2129 main:5: note: Recursive types are not allowed at function scope
2130 main:6: note: Revealed type is "Any"
2131 main:8: error: Cannot resolve name "D" (possible cyclic definition)
2132 main:8: note: Recursive types are not allowed at function scope
2133 main:8: error: Cannot resolve name "C" (possible cyclic definition)
2134 main:9: error: Argument 2 to NewType(...) must be a valid type
2135 main:9: error: Cannot resolve name "B" (possible cyclic definition)
2136 main:9: note: Recursive types are not allowed at function scope
2137
2138 -- Copied from check-classes.test (tricky corner cases).
2139 [case testNewAnalyzerNoCrashForwardRefToBrokenDoubleNewTypeClass]
2140 from typing import Any, Dict, List, NewType
2141
2142 Foo = NewType('NotFoo', int) # type: ignore
2143 Foos = NewType('Foos', List[Foo])
2144
2145 x: C
2146 class C:
2147 def frob(self, foos: Dict[Any, Foos]) -> None:
2148 foo = foos.get(1)
2149 assert foo
2150 dict(foo)
2151 [builtins fixtures/dict.pyi]
2152
2153 [case testNewAnalyzerForwardTypeAliasInBase]
2154 from typing import List, Generic, TypeVar, NamedTuple
2155 T = TypeVar('T')
2156
2157 def test() -> None:
2158 class C(A, B): # E: Cannot resolve name "A" (possible cyclic definition) \
2159 # N: Recursive types are not allowed at function scope
2160 pass
2161 class G(Generic[T]): pass
2162 A = G[C] # E: Cannot resolve name "A" (possible cyclic definition) \
2163 # N: Recursive types are not allowed at function scope
2164 class B(NamedTuple):
2165 x: int
2166
2167 y: C
2168 reveal_type(y.x) # N: Revealed type is "builtins.int"
2169 reveal_type(y[0]) # N: Revealed type is "builtins.int"
2170 x: A
2171 reveal_type(x) # N: Revealed type is "__main__.G@7[tuple[builtins.int, fallback=__main__.C@5]]"
2172 [builtins fixtures/list.pyi]
2173
2174 [case testNewAnalyzerDuplicateTypeVar]
2175 from typing import TypeVar, Generic, Any
2176
2177 T = TypeVar('T', bound='B[Any]')
2178 # The "int" error is because of typing fixture.
2179 T = TypeVar('T', bound='C') # E: Cannot redefine "T" as a type variable \
2180 # E: Invalid assignment target
2181
2182 class B(Generic[T]):
2183 x: T
2184 class C: ...
2185
2186 x: B[int] # E: Type argument "int" of "B" must be a subtype of "B[Any]"
2187 y: B[B[Any]]
2188 reveal_type(y.x) # N: Revealed type is "__main__.B[Any]"
2189 [builtins fixtures/tuple.pyi]
2190 [typing fixtures/typing-full.pyi]
2191
2192 [case testNewAnalyzerDuplicateTypeVarImportCycle]
2193 # flags: --disable-error-code used-before-def
2194 import a
2195 [file a.py]
2196 from typing import TypeVar, Any
2197 from b import B, C
2198
2199 T = TypeVar('T', bound=B[Any])
2200 T = TypeVar('T', bound=C) # E: Cannot redefine "T" as a type variable \
2201 # E: Invalid assignment target
2202
2203 [file b.py]
2204 from typing import Generic, Any
2205 from a import T
2206
2207 class B(Generic[T]):
2208 x: T
2209 class C: ...
2210
2211 x: B[int] # E: Type argument "int" of "B" must be a subtype of "B[Any]"
2212 y: B[B[Any]]
2213 reveal_type(y.x) # N: Revealed type is "b.B[Any]"
2214 [builtins fixtures/tuple.pyi]
2215 [typing fixtures/typing-full.pyi]
2216
2217 [case testNewAnalyzerDuplicateTypeVarImportCycleWithAliases]
2218 # flags: --disable-error-code used-before-def
2219 import a
2220 [file a.py]
2221 from typing import TypeVar, Any
2222 from b import BA, C
2223
2224 T = TypeVar('T', bound=BAA[Any])
2225 T = TypeVar('T', bound=C) # E: Cannot redefine "T" as a type variable \
2226 # E: Invalid assignment target
2227 BAA = BA
2228
2229 [file b.py]
2230 from typing import Generic, Any
2231 from a import T
2232
2233 BA = B
2234 class B(Generic[T]):
2235 x: T
2236 class C: ...
2237
2238 x: B[int] # E: Type argument "int" of "B" must be a subtype of "B[Any]"
2239 y: B[B[Any]]
2240 reveal_type(y.x) # N: Revealed type is "b.B[Any]"
2241
2242 [case testNewAnalyzerTypeVarBoundInCycle]
2243 import factory, box
2244
2245 [file factory.py]
2246 from typing import Generic, Type
2247
2248 from box import BoxT
2249
2250 class Factory(Generic[BoxT]):
2251 value: int
2252
2253 def create(self, boxClass: Type[BoxT]) -> BoxT:
2254 reveal_type(boxClass.create(self)) # N: Revealed type is "BoxT`1"
2255 return boxClass.create(self)
2256
2257 [file box.py]
2258 from typing import TYPE_CHECKING, Type, TypeVar
2259
2260 if TYPE_CHECKING:
2261 from factory import Factory
2262
2263 BoxT = TypeVar('BoxT', bound='Box')
2264
2265 class Box:
2266 @classmethod
2267 def create(cls: Type[BoxT], f: Factory) -> BoxT:
2268 return cls(f.value)
2269
2270 def __init__(self, value: int) -> None: ...
2271 [builtins fixtures/classmethod.pyi]
2272 [typing fixtures/typing-medium.pyi]
2273
2274 [case testNewAnalyzerCastForward1]
2275 from typing import cast
2276
2277 x = cast('C', None)
2278 class A:
2279 def foo(self) -> None:
2280 self.x = cast('C', None)
2281
2282 reveal_type(x) # N: Revealed type is "__main__.C"
2283 reveal_type(A().x) # N: Revealed type is "__main__.C"
2284
2285 class C(A): ...
2286
2287 [case testNewAnalyzerCastForward2]
2288 from typing import cast
2289
2290 x = cast('C', None)
2291
2292 reveal_type(x) # N: Revealed type is "builtins.int"
2293
2294 C = int
2295
2296 [case testNewAnalyzerCastForward3]
2297 from typing import cast, NamedTuple
2298
2299 x = cast('C', None)
2300
2301 reveal_type(x) # N: Revealed type is "tuple[builtins.int, fallback=__main__.C]"
2302 reveal_type(x.x) # N: Revealed type is "builtins.int"
2303
2304 C = NamedTuple('C', [('x', int)])
2305 [builtins fixtures/tuple.pyi]
2306
2307 [case testNewAnalyzerApplicationForward1]
2308 # flags: --disable-error-code used-before-def
2309 from typing import Generic, TypeVar
2310
2311 x = C[int]()
2312 reveal_type(x) # N: Revealed type is "__main__.C[builtins.int]"
2313
2314 T = TypeVar('T')
2315 class C(Generic[T]): ...
2316
2317 [case testNewAnalyzerApplicationForward2]
2318 from typing import Generic, TypeVar
2319
2320 T = TypeVar('T')
2321 class C(Generic[T]): ...
2322
2323 x = C['A']()
2324 reveal_type(x) # N: Revealed type is "__main__.C[__main__.A]"
2325
2326 class A: ...
2327
2328 [case testNewAnalyzerApplicationForward3]
2329 from typing import Generic, TypeVar
2330
2331 class A: ...
2332 T = TypeVar('T')
2333 class C(Generic[T]): ...
2334 x = C[A]()
2335 reveal_type(x) # N: Revealed type is "__main__.C[__main__.A]"
2336
2337 [case testNewAnalyzerApplicationForward4]
2338 # flags: --disable-error-code used-before-def
2339 from typing import Generic, TypeVar
2340
2341 x = C[A]() # E: Value of type variable "T" of "C" cannot be "A"
2342 reveal_type(x) # N: Revealed type is "__main__.C[__main__.A]"
2343
2344 T = TypeVar('T', bound='D')
2345 class C(Generic[T]): ...
2346
2347 class A: ...
2348 class D: ...
2349
2350 [case testNewAnalyzerModuleGetattrSerialize_incremental]
2351 import a
2352 [file a.py]
2353 import p
2354 [file a.py.2]
2355 import p
2356 reveal_type(p.y)
2357 [file p.pyi]
2358 from pp import x
2359 y = x
2360 [file pp.pyi]
2361 def __getattr__(attr): ...
2362 [out2]
2363 tmp/a.py:2: note: Revealed type is "Any"
2364
2365 [case testNewAnanlyzerTrickyImportPackage]
2366 from lib import config
2367 import lib
2368
2369 reveal_type(lib.config.x) # N: Revealed type is "builtins.int"
2370 reveal_type(config.x) # N: Revealed type is "builtins.int"
2371
2372 [file lib/__init__.py]
2373 from lib.config import config
2374
2375 [file lib/config.py]
2376 class Config:
2377 x: int
2378
2379 config = Config()
2380 [builtins fixtures/module.pyi]
2381
2382 [case testNewAnanlyzerTrickyImportPackageAlt]
2383 import lib.config
2384 import lib.config as tmp
2385
2386 reveal_type(lib.config.x) # N: Revealed type is "builtins.int"
2387 # TODO: this actually doesn't match runtime behavior, variable wins.
2388 tmp.x # E: Module has no attribute "x"
2389
2390 [file lib/__init__.py]
2391 from lib.config import config
2392
2393 [file lib/config.py]
2394 class Config:
2395 x: int
2396
2397 config = Config()
2398 [builtins fixtures/module.pyi]
2399
2400 [case testNewAnanlyzerTrickyImportPackage_incremental]
2401 import a
2402
2403 [file a.py]
2404 from lib import config
2405 import lib
2406
2407 [file a.py.2]
2408 from lib import config
2409 import lib
2410
2411 reveal_type(lib.config.x)
2412 reveal_type(config.x)
2413
2414 [file lib/__init__.py]
2415 from lib.config import config
2416
2417 [file lib/config.py]
2418 class Config:
2419 x: int
2420
2421 config = Config()
2422 [builtins fixtures/module.pyi]
2423 [out2]
2424 tmp/a.py:4: note: Revealed type is "builtins.int"
2425 tmp/a.py:5: note: Revealed type is "builtins.int"
2426
2427 [case testNewAnalyzerRedefineAsClass]
2428 from typing import Any
2429 from other import C # type: ignore
2430
2431 y = 'bad'
2432
2433 class C: # E: Name "C" already defined (possibly by an import)
2434 def meth(self, other: int) -> None:
2435 y() # E: "str" not callable
2436
2437 [case testNewAnalyzerRedefineAsOverload]
2438 from typing import overload
2439
2440 y = 'bad'
2441
2442 if int():
2443 def f(x: int) -> None:
2444 pass
2445 else:
2446 @overload # E: Name "f" already defined on line 6
2447 def f(x: int) -> None: ...
2448 @overload
2449 def f(x: str) -> None: ...
2450 def f(x) -> None:
2451 y() # E: "str" not callable
2452
2453 [case testNewAnalyzerFirstAliasTargetWins]
2454 class DesiredTarget:
2455 attr: int
2456
2457 if int():
2458 Alias = DesiredTarget
2459 else:
2460 class DummyTarget:
2461 pass
2462 Alias = DummyTarget # type: ignore
2463
2464 x: Alias
2465 reveal_type(x.attr) # N: Revealed type is "builtins.int"
2466 [case testNewAnalyzerFirstVarDefinitionWins]
2467 x = y # E: Name "y" is used before definition
2468 x = 1
2469
2470 # We want to check that the first definition creates the variable.
2471 def x() -> None: ... # E: Name "x" already defined on line 1
2472 y = 2
2473
2474 [case testNewAnalyzerImportStarSpecialCase]
2475 import unittest
2476 [file unittest/__init__.pyi]
2477 from unittest.suite import *
2478
2479 def load_tests() -> TestSuite: ...
2480 [file unittest/suite.pyi]
2481 from typing import Union # Iterable not imported
2482 import unittest.case
2483
2484 _TestType = Union[unittest.case.TestCase]
2485
2486 class BaseTestSuite(Iterable[_TestType]):
2487 ...
2488
2489 class TestSuite(BaseTestSuite):
2490 ...
2491
2492 [file unittest/case.pyi]
2493 class TestCase: ...
2494
2495 [out]
2496 tmp/unittest/suite.pyi:6: error: Name "Iterable" is not defined
2497 tmp/unittest/suite.pyi:6: note: Did you forget to import it from "typing"? (Suggestion: "from typing import Iterable")
2498
2499 [case testNewAnalyzerNewTypeSpecialCase]
2500 from typing import Final, Literal, NewType
2501
2502 X = NewType('X', int)
2503
2504 var1: Final = 1
2505
2506 def force1(x: Literal[1]) -> None: pass
2507
2508 force1(reveal_type(var1)) # N: Revealed type is "Literal[1]"
2509 [builtins fixtures/tuple.pyi]
2510
2511 [case testNewAnalyzerReportLoopInMRO]
2512 class A(A): ... # E: Cannot resolve name "A" (possible cyclic definition)
2513 [out]
2514
2515 [case testNewSemanticAnalyzerUnimportedSpecialCase]
2516 # flags: --ignore-missing-imports
2517 import other
2518 import p.u
2519
2520 [file p/__init__.py]
2521
2522 [file p/u.pyi]
2523 from . import c
2524 x: c.C
2525
2526 [file other.py]
2527 from p.c import B
2528
2529 [out]
2530
2531 [case testNewSemanticAnalyzerQualifiedFunctionAsType]
2532 import m
2533
2534 x: m.C.a.b # E: Name "m.C.a.b" is not defined
2535
2536 [file m.py]
2537 def C(): pass
2538
2539 [case testNewSemanticAnalyzerModulePrivateRefInMiddleOfQualified]
2540 import m
2541
2542 x: m.n.C # E: Name "m.n.C" is not defined
2543 reveal_type(x) # N: Revealed type is "Any"
2544
2545 [file m.pyi]
2546 import n
2547
2548 [file n.pyi]
2549 class C: pass
2550
2551 [case testNewAnalyzerModuleGetAttr]
2552 import m
2553 import n
2554
2555 x: m.n.C
2556 y: n.D
2557 [file m.py]
2558 import n
2559 [file n.py]
2560 def __getattr__(x): pass
2561
2562 [case testNewAnalyzerReportLoopInMRO2]
2563 def f() -> None:
2564 class A(A): ... # E: Cannot resolve name "A" (possible cyclic definition) \
2565 # N: Recursive types are not allowed at function scope
2566
2567 [case testNewAnalyzerUnsupportedBaseClassInsideFunction]
2568 class C:
2569 class E: pass
2570
2571 def f(self) -> None:
2572 # TODO: Error message could be better
2573 class D(self.E): # E: Name "self.E" is not defined
2574 pass
2575
2576 [case testNewAnalyzerShadowOuterDefinitionBasedOnOrderSinglePass]
2577 # Only one semantic analysis pass
2578 class X: pass
2579 class C:
2580 X = X
2581 reveal_type(X) # N: Revealed type is "def () -> __main__.X"
2582 reveal_type(C.X) # N: Revealed type is "def () -> __main__.X"
2583
2584 [case testNewAnalyzerShadowOuterDefinitionBasedOnOrderTwoPasses]
2585 c: C # Force second semantic analysis pass
2586 class X: pass
2587 class C:
2588 X = X
2589 reveal_type(X) # N: Revealed type is "def () -> __main__.X"
2590
2591 reveal_type(C.X) # N: Revealed type is "def () -> __main__.X"
2592
2593 [case testNewAnalyzerAnnotationConflictsWithAttributeSinglePass]
2594 class C:
2595 def x(self) -> int:
2596 return 0
2597
2598 def __init__(self) -> None:
2599 self.int = ''
2600
2601 def y(self) -> int:
2602 return 0
2603
2604 z: str
2605
2606 def str(self) -> str:
2607 return 0 # E: Incompatible return value type (got "int", expected "str")
2608
2609 zz: str # E: Function "__main__.C.str" is not valid as a type \
2610 # N: Perhaps you need "Callable[...]" or a callback protocol?
2611
2612 reveal_type(C().x()) # N: Revealed type is "builtins.int"
2613 reveal_type(C().y()) # N: Revealed type is "builtins.int"
2614 reveal_type(C().z) # N: Revealed type is "builtins.str"
2615 reveal_type(C().str()) # N: Revealed type is "builtins.str"
2616
2617 [case testNewAnalyzerAnnotationConflictsWithAttributeTwoPasses]
2618 c: C # Force second semantic analysis pass
2619
2620 class C:
2621 def x(self) -> int:
2622 return 0
2623
2624 def __init__(self) -> None:
2625 self.int = ''
2626
2627 def y(self) -> int:
2628 return 0
2629
2630 z: str
2631
2632 def str(self) -> str:
2633 return 0 # E: Incompatible return value type (got "int", expected "str")
2634
2635 zz: str # E: Function "__main__.C.str" is not valid as a type \
2636 # N: Perhaps you need "Callable[...]" or a callback protocol?
2637
2638 reveal_type(C().x()) # N: Revealed type is "builtins.int"
2639 reveal_type(C().y()) # N: Revealed type is "builtins.int"
2640 reveal_type(C().z) # N: Revealed type is "builtins.str"
2641 reveal_type(C().str()) # N: Revealed type is "builtins.str"
2642
2643 [case testNewAnalyzerNameConflictsAndMultiLineDefinition]
2644 c: C # Force second semantic analysis pass
2645
2646 class X: pass
2647
2648 class C:
2649 X = (
2650 X)
2651
2652 def str(self
2653 ) -> str:
2654 return 0 # E: Incompatible return value type (got "int", expected "str")
2655
2656 reveal_type(C.X) # E: # N: Revealed type is "def () -> __main__.X"
2657 reveal_type(C().str()) # N: Revealed type is "builtins.str"
2658
2659 [case testNewAnalyzerNameNotDefinedYetInClassBody]
2660 class C:
2661 X = Y # E: Name "Y" is not defined
2662 Y = 1
2663
2664 f = g # E: Name "g" is not defined
2665
2666 def g(self) -> None: pass
2667
2668 reveal_type(C.X) # N: Revealed type is "Any"
2669
2670 [case testNewAnalyzerImportedNameUsedInClassBody]
2671 import m
2672
2673 [file m.py]
2674 class C:
2675 from mm import f # E: Unsupported class scoped import
2676 @dec(f)
2677 def m(self): pass
2678
2679 def dec(f): pass
2680 [file mm.py]
2681 # 1 padding to increase line number of 'f'
2682 # 2 padding
2683 # 3 padding
2684 # 4 padding
2685 # 5 padding
2686 # 6 padding
2687 def f(): pass
2688
2689
2690 [case testNewAnalyzerImportedNameUsedInClassBody2]
2691 import m
2692
2693 [file m/__init__.py]
2694 class C:
2695 from m.m import f # E: Unsupported class scoped import
2696 @dec(f)
2697 def m(self): pass
2698
2699 def dec(f): pass
2700 [file m/m.py]
2701 # 1 padding to increase line number of 'f'
2702 # 2 padding
2703 # 3 padding
2704 # 4 padding
2705 # 5 padding
2706 # 6 padding
2707 def f(): pass
2708
2709 [case testNewAnalyzerOverrideClassWithTypeAlias]
2710 from typing import Generic, TypeVar
2711
2712 T = TypeVar('T')
2713
2714 class C(Generic[T]):
2715 pass
2716
2717 C = C[int] # E: Cannot assign to a type \
2718 # E: Incompatible types in assignment (expression has type "type[C[int]]", variable has type "type[C[T]]")
2719 x: C
2720 reveal_type(x) # N: Revealed type is "__main__.C[Any]"
2721
2722 [case testNewAnalyzerClassVariableOrdering]
2723 def foo(x: str) -> None: pass
2724
2725 class Something:
2726 def run(self) -> None:
2727 foo(self.IDS[0]) # E: Argument 1 to "foo" has incompatible type "int"; expected "str"
2728
2729 IDS = [87]
2730 [builtins fixtures/list.pyi]
2731
2732 [case testNewAnalyzerPlaceholderFromOuterScope]
2733 import b
2734 [file a.py]
2735 import b
2736 class A(B): ...
2737 class B: ...
2738
2739 [file b.py]
2740 from a import A
2741
2742 class C:
2743 A = A # Initially rvalue will be a placeholder
2744
2745 reveal_type(C.A) # N: Revealed type is "def () -> a.A"
2746
2747 [case testNewAnalyzerFinalLiteralInferredAsLiteralWithDeferral]
2748 from typing import Final, Literal
2749
2750 defer: Yes
2751
2752 var: Final = 42
2753 def force(x: Literal[42]) -> None: pass
2754 force(reveal_type(var)) # N: Revealed type is "Literal[42]"
2755
2756 class Yes: ...
2757 [builtins fixtures/tuple.pyi]
2758
2759 [case testNewAnalyzerImportCycleWithIgnoreMissingImports]
2760 # flags: --ignore-missing-imports
2761 import p
2762 reveal_type(p.get) # N: Revealed type is "def () -> builtins.int"
2763
2764 [file p/__init__.pyi]
2765 from . import api
2766 get = api.get
2767
2768 [file p/api.pyi]
2769 import p
2770
2771 def get() -> int: ...
2772
2773 [case testUseObsoleteNameForTypeVar3]
2774 import typing
2775 t = typing.typevar('t') # E: Module has no attribute "typevar"
2776 [builtins fixtures/module.pyi]
2777 [typing fixtures/typing-full.pyi]
2778
2779 [case testNewAnalyzerImportFromTopLevelFunction]
2780 import a.b # This works at runtime
2781 reveal_type(a.b) # N: Revealed type is "def ()"
2782 [file a/__init__.py]
2783 from .b import B
2784 from . import b as c
2785 def b() -> None: pass
2786 reveal_type(b) # N: Revealed type is "def ()"
2787 reveal_type(c.B()) # N: Revealed type is "a.b.B"
2788 x: Forward
2789 class Forward:
2790 ...
2791
2792 [file a/b.py]
2793 class B: ...
2794 [builtins fixtures/module.pyi]
2795
2796 [case testNewAnalyzerImportFromTopLevelAlias]
2797 import a.b # This works at runtime
2798 reveal_type(a.b) # N: Revealed type is "def () -> builtins.int"
2799 [file a/__init__.py]
2800 from .b import B
2801 from . import b as c
2802 b = int
2803 y: b
2804 reveal_type(y) # N: Revealed type is "builtins.int"
2805 reveal_type(c.B) # N: Revealed type is "def () -> a.b.B"
2806 x: Forward
2807 class Forward:
2808 ...
2809
2810 [file a/b.py]
2811 class B: ...
2812 [builtins fixtures/module.pyi]
2813
2814 [case testNewAnalyzerImportAmbiguousWithTopLevelFunction]
2815 import a.b # This works at runtime
2816 x: a.b.B # E: Name "a.b.B" is not defined
2817 reveal_type(a.b) # N: Revealed type is "def ()"
2818 [file a/__init__.py]
2819 import a.b
2820 import a.b as c
2821 def b() -> None: pass
2822 reveal_type(b) # N: Revealed type is "def ()"
2823 reveal_type(c.B()) # N: Revealed type is "a.b.B"
2824 x: Forward
2825 class Forward:
2826 ...
2827
2828 [file a/b.py]
2829 class B: ...
2830 [builtins fixtures/module.pyi]
2831
2832 [case testNewAnalyzerConfusingImportConflictingNames]
2833 # flags: --follow-imports=skip --ignore-missing-imports
2834 # cmd: mypy -m other a.b a
2835 [file a/__init__.py]
2836 [file a/b/__init__.py]
2837 import other
2838 import a.b.a
2839 import a.b.c
2840
2841 [file other.py]
2842 from a.b.a import foo
2843 [builtins fixtures/module.pyi]
2844 [out]
2845
2846 [case testNewAnalyzerNamedTupleMethod]
2847 from typing import NamedTuple
2848
2849 g: N
2850
2851 class N(NamedTuple):
2852 def f(self) -> None:
2853 b = (
2854 a
2855 for a in [1]
2856 )
2857 b
2858 [builtins fixtures/tuple.pyi]
2859
2860 [case testWithMultipleTargetsDeferred]
2861 a: A
2862
2863 class A:
2864 def __enter__(self) -> int: pass
2865 def __exit__(self, x, y, z): pass
2866
2867 with A() as x, A() as y: # type: int, int
2868 pass
2869
2870 [case testNewAnalyzerLessErrorsNeedAnnotation]
2871 from typing import TypeVar, Optional
2872
2873 T = TypeVar('T')
2874
2875 def f(x: Optional[T] = None) -> T: ...
2876
2877 x = f() # E: Need type annotation for "x"
2878 y = x
2879
2880 def g() -> None:
2881 x = f() # E: Need type annotation for "x"
2882 y = x
2883
2884 [case testNewAnalyzerLessErrorsNeedAnnotationList]
2885 x = [] # type: ignore
2886 reveal_type(x) # N: Revealed type is "builtins.list[Any]"
2887
2888 def g() -> None:
2889 x = [] # type: ignore
2890 reveal_type(x) # N: Revealed type is "builtins.list[Any]"
2891 [builtins fixtures/list.pyi]
2892
2893 [case testNewAnalyzerLessErrorsNeedAnnotationNested]
2894 from typing import TypeVar, Optional, Generic
2895
2896 T = TypeVar('T')
2897 class G(Generic[T]): ...
2898
2899 def f(x: Optional[T] = None) -> G[T]: ...
2900
2901 x = f() # E: Need type annotation for "x"
2902 y = x
2903 reveal_type(y) # N: Revealed type is "__main__.G[Any]"
2904
2905 def g() -> None:
2906 x = f() # E: Need type annotation for "x"
2907 y = x
2908 reveal_type(y) # N: Revealed type is "__main__.G[Any]"
2909
2910 [case testNewAnalyzerRedefinedNonlocal]
2911 # flags: --disable-error-code=annotation-unchecked
2912 import typing
2913
2914 def f():
2915 bar = [] # type: typing.List[int]
2916
2917 def foo():
2918 nonlocal bar
2919 bar = [] # type: typing.List[int]
2920
2921 def g() -> None:
2922 bar = [] # type: typing.List[int]
2923
2924 def foo() -> None:
2925 nonlocal bar
2926 bar = [] # type: typing.List[int] # E: Name "bar" already defined on line 12
2927 [builtins fixtures/list.pyi]
2928
2929 [case testNewAnalyzerMoreInvalidTypeVarArgumentsDeferred]
2930 from typing import TypeVar, Generic
2931
2932 defer: Yes
2933
2934 S = TypeVar('S', covariant=True, contravariant=True) # E: TypeVar cannot be both covariant and contravariant \
2935 # E: "int" not callable
2936
2937 class Yes: ...
2938 [builtins fixtures/bool.pyi]
2939
2940 [case testNewAnalyzerDisallowAnyGenericsMessages]
2941 # mypy: disallow-any-generics
2942 from a import B
2943 x: B
2944
2945 [file a.py]
2946 from typing import TypeVar, List
2947
2948 T = TypeVar('T')
2949
2950 A = List[T]
2951 B = A
2952
2953 [builtins fixtures/list.pyi]
2954
2955 [case testNewAnalyzerVarTypeVarNoCrash]
2956 from typing import Callable, TypeVar
2957
2958 FooT = TypeVar('FooT', bound='Foo')
2959 class Foo: ...
2960
2961 f = lambda x: True # type: Callable[[FooT], bool]
2962 reveal_type(f) # N: Revealed type is "def [FooT <: __main__.Foo] (FooT`-1) -> builtins.bool"
2963 [builtins fixtures/bool.pyi]
2964
2965 [case testNewAnalyzerVarTypeVarNoCrashImportCycle]
2966 import a
2967
2968 [file a.py]
2969 from b import B
2970 from typing import TypeVar
2971
2972 FooT = TypeVar('FooT', bound='Foo')
2973 class Foo: ...
2974
2975 [file b.py]
2976 from a import FooT
2977 from typing import Callable
2978
2979 f = lambda x: True # type: Callable[[FooT], bool]
2980 reveal_type(f) # N: Revealed type is "def [FooT <: a.Foo] (FooT`-1) -> builtins.bool"
2981
2982 class B: ...
2983 [builtins fixtures/bool.pyi]
2984
2985 [case testNewAnalyzerFuncTypeVarNoCrashImportCycle]
2986 import a
2987
2988 [file a.py]
2989 from b import B
2990 from typing import TypeVar
2991
2992 FooT = TypeVar('FooT', bound='Foo')
2993 class Foo: ...
2994
2995 [file b.py]
2996 from a import FooT
2997 from typing import Callable
2998
2999 def f(x: FooT) -> bool: ...
3000 reveal_type(f) # N: Revealed type is "def [FooT <: a.Foo] (x: FooT`-1) -> builtins.bool"
3001
3002 class B: ...
3003 [builtins fixtures/bool.pyi]
3004
3005 [case testNewAnalyzerNoCrashOnStarInference]
3006 from typing import Tuple
3007
3008 def f() -> None:
3009 t: Tuple[str, Tuple[str, str, str]]
3010 x, (y, *z) = t
3011 reveal_type(z) # N: Revealed type is "builtins.list[builtins.str]"
3012 [builtins fixtures/list.pyi]
3013
3014 [case testNewAnalyzerIdentityAssignment1]
3015 from foo import *
3016
3017 try:
3018 X = X
3019 except:
3020 class X: # E: Name "X" already defined (possibly by an import)
3021 pass
3022
3023 reveal_type(X()) # N: Revealed type is "foo.X"
3024
3025 [file foo.py]
3026 class X: pass
3027
3028 [case testNewAnalyzerIdentityAssignment2]
3029 try:
3030 int = int
3031 reveal_type(int()) # N: Revealed type is "builtins.int"
3032 except:
3033 class int: # E: Name "int" already defined (possibly by an import)
3034 pass
3035
3036 reveal_type(int()) # N: Revealed type is "builtins.int"
3037
3038 [case testNewAnalyzerIdentityAssignment3]
3039 forwardref: C
3040
3041 try:
3042 int = int
3043 reveal_type(int()) # N: Revealed type is "builtins.int"
3044 except:
3045 class int: # E: Name "int" already defined (possibly by an import)
3046 pass
3047
3048 reveal_type(int()) # N: Revealed type is "builtins.int"
3049
3050 class C: pass
3051
3052 [case testNewAnalyzerIdentityAssignment4]
3053 try:
3054 C = C
3055 C
3056 except:
3057 class C:
3058 pass
3059
3060 reveal_type(C()) # N: Revealed type is "__main__.C"
3061
3062 [case testNewAnalyzerIdentityAssignment5]
3063 forwardref: D
3064
3065 try:
3066 C = C
3067 C
3068 except:
3069 class C:
3070 pass
3071
3072 class D: pass
3073
3074 reveal_type(C()) # N: Revealed type is "__main__.C"
3075
3076 [case testNewAnalyzerIdentityAssignment6]
3077 x: C
3078 class C:
3079 pass
3080 C = C
3081
3082 reveal_type(C()) # N: Revealed type is "__main__.C"
3083 reveal_type(x) # N: Revealed type is "__main__.C"
3084
3085 [case testNewAnalyzerIdentityAssignment7]
3086 C = C # E: Name "C" is not defined
3087
3088 reveal_type(C) # E: Name "C" is not defined \
3089 # N: Revealed type is "Any"
3090
3091 [case testNewAnalyzerIdentityAssignment8]
3092 from typing import Final
3093 x: Final = 0
3094 x = x # E: Cannot assign to final name "x"
3095
3096 [case testNewAnalyzerIdentityAssignmentClassImplicit]
3097 class C: ...
3098 class A:
3099 C = C[str] # E: "C" expects no type arguments, but 1 given
3100 [builtins fixtures/tuple.pyi]
3101
3102 [case testNewAnalyzerIdentityAssignmentClassExplicit]
3103 from typing_extensions import TypeAlias
3104
3105 class A:
3106 C: TypeAlias = C
3107 class C: ...
3108 c: A.C
3109 reveal_type(c) # N: Revealed type is "__main__.C"
3110 [builtins fixtures/tuple.pyi]
3111
3112 [case testNewAnalyzerClassPropertiesInAllScopes]
3113 from abc import abstractmethod, ABCMeta
3114
3115 class TopLevel(metaclass=ABCMeta):
3116 @abstractmethod
3117 def f(self) -> None: pass
3118
3119 TopLevel() # E: Cannot instantiate abstract class "TopLevel" with abstract attribute "f"
3120
3121 def func() -> None:
3122 class Function(metaclass=ABCMeta):
3123 @abstractmethod
3124 def f(self) -> None: pass
3125
3126 Function() # E: Cannot instantiate abstract class "Function" with abstract attribute "f"
3127
3128 class C:
3129 def meth(self) -> None:
3130 class Method(metaclass=ABCMeta):
3131 @abstractmethod
3132 def f(self) -> None: pass
3133
3134 Method() # E: Cannot instantiate abstract class "Method" with abstract attribute "f"
3135
3136 [case testModulesAndFuncsTargetsInCycle]
3137 import a
3138 [file a.py]
3139 import b
3140 defer: Yes
3141 def func() -> int: ...
3142 class Yes: ...
3143 [file b.py]
3144 import a
3145
3146 def func() -> int: ...
3147 [targets b, a, a, b.func, a.func, __main__]
3148
3149 [case testNewAnalyzerForwardReferenceInFunction]
3150 def f(x: 'A') -> 'A':
3151 return A()
3152
3153 class A:
3154 pass
3155 [targets __main__, __main__.f]
3156
3157 [case testNewAnalyzerSimpleImportStarNoDeferral]
3158 from m import *
3159
3160 x: A
3161 f()
3162
3163 [file m.py]
3164 class A: pass
3165 def f() -> None: pass
3166
3167 [targets m, m.f, __main__]
3168
3169 [case testNewAnalyzerNoCrashOnCustomProperty]
3170 # flags: --ignore-missing-imports
3171 from unimported import custom
3172
3173 class User:
3174 first_name: str
3175
3176 @custom
3177 def name(self) -> str:
3178 return self.first_name
3179
3180 @name.setter # type: ignore
3181 def name(self, value: str) -> None:
3182 self.first_name = value
3183
3184 def __init__(self, name: str) -> None:
3185 self.name = name # E: Cannot assign to a method
3186
3187 [case testNewAnalyzerMemberNameMatchesTypedDict]
3188 from typing import TypedDict, Union, Any
3189
3190 class T(TypedDict):
3191 b: b.T
3192
3193 class b:
3194 T: Union[Any]
3195 [builtins fixtures/dict.pyi]
3196 [typing fixtures/typing-typeddict.pyi]
3197
3198 [case testNewAnalyzerMemberNameMatchesNamedTuple]
3199 from typing import Union, Any, NamedTuple
3200
3201 class T(NamedTuple):
3202 b: b.T
3203
3204 class b:
3205 T = Union[Any]
3206
3207 [builtins fixtures/tuple.pyi]
3208
3209 [case testSelfReferentialSubscriptExpression]
3210 x = x[1] # E: Cannot resolve name "x" (possible cyclic definition)
3211 y = 1[y] # E: Value of type "int" is not indexable \
3212 # E: Cannot determine type of "y"
3213
3214 [case testForwardBaseDeferAttr]
3215 from typing import Optional, Callable, TypeVar
3216
3217 class C(B):
3218 def a(self) -> None:
3219 reveal_type(self._foo) # N: Revealed type is "builtins.int | None"
3220 self._foo = defer()
3221
3222 class B:
3223 def __init__(self) -> None:
3224 self._foo: Optional[int] = None
3225
3226 T = TypeVar("T")
3227 def deco(fn: Callable[[], T]) -> Callable[[], T]: ...
3228
3229 @deco
3230 def defer() -> int: ...
3231
3232 [case testNestedClassSelfNoPlaceholder]
3233 from typing import Generic, TypeVar
3234
3235 def test() -> None:
3236 T = TypeVar("T", bound="Model")
3237
3238 class Query(Generic[T]):
3239 def filter(self, value: T) -> None:
3240 ...
3241
3242 class Model:
3243 pass