Debian Codegraph @

test-data/unit/merge.test

raw
1 -- Test cases for AST merge (used for fine-grained incremental checking)
2 --
3 -- Each test case has two versions of the module 'target' (target.py and
4 -- target.py.next). A test cases type checks both of them, merges the ASTs,
5 -- and finally dumps certain parts of the ASTs for both versions (==>
6 -- separates the first and second versions). A test case passes if the
7 -- dumped output is as expected.
8 --
9 -- The dumped output uses <N> to denote identities of objects. Objects
10 -- suffixed by the same <N> refer to the same object; <N> and <M> (if
11 -- N != M) refer to different objects. The objective of these test cases
12 -- is to verify that identities of publicly visible AST nodes is
13 -- preserved across merge. Other AST nodes may get new identities.
14 --
15 -- Each test case dumps one of four kinds of information:
16 --
17 -- 1) ASTs (test case has no magic suffix)
18 -- 2) Symbol tables (_symtable test case name suffix)
19 -- 3) TypeInfos (_typeinfo suffix)
20 -- 4) Inferred types (_types suffix)
21 --
22 -- If you need to dump multiple different kinds of information, write
23 -- multiple test cases.
24
25 [case testFunction]
26 import target
27 [file target.py]
28 def f() -> int:
29 pass
30 [file target.py.next]
31 def f() -> int:
32 pass
33 [out]
34 MypyFile:1<0>(
35 tmp/main
36 Import:1(target))
37 MypyFile:1<1>(
38 tmp/target.py
39 FuncDef:1<2>(
40 f
41 def () -> builtins.int<3>
42 Block:2<4>(
43 PassStmt:2<5>())))
44 ==>
45 MypyFile:1<0>(
46 tmp/main
47 Import:1(target))
48 MypyFile:1<1>(
49 tmp/target.py
50 FuncDef:1<2>(
51 f
52 def () -> builtins.int<3>
53 Block:2<6>(
54 PassStmt:2<7>())))
55
56 [case testClass]
57 import target
58 [file target.py]
59 class A:
60 def f(self, x: str) -> int:
61 pass
62 [file target.py.next]
63 class A:
64 def f(self, x: int) -> str:
65 pass
66 [out]
67 MypyFile:1<0>(
68 tmp/main
69 Import:1(target))
70 MypyFile:1<1>(
71 tmp/target.py
72 ClassDef:1<2>(
73 A
74 FuncDef:2<3>(
75 f
76 Args(
77 Var(self)
78 Var(x))
79 def (self: target.A<4>, x: builtins.str<5>) -> builtins.int<6>
80 Block:3<7>(
81 PassStmt:3<8>()))))
82 ==>
83 MypyFile:1<0>(
84 tmp/main
85 Import:1(target))
86 MypyFile:1<1>(
87 tmp/target.py
88 ClassDef:1<9>(
89 A
90 FuncDef:2<3>(
91 f
92 Args(
93 Var(self)
94 Var(x))
95 def (self: target.A<4>, x: builtins.int<6>) -> builtins.str<5>
96 Block:3<10>(
97 PassStmt:3<11>()))))
98
99 [case testClass_typeinfo]
100 import target
101 [file target.py]
102 class A:
103 def f(self, x: str) -> int: pass
104 def g(self, x: str) -> int: pass
105 [file target.py.next]
106 class A:
107 def f(self, x: int) -> str: pass
108 def h(self, x: int) -> str: pass
109 [out]
110 TypeInfo<0>(
111 Name(target.A)
112 Bases(builtins.object<1>)
113 Mro(target.A<0>, builtins.object<1>)
114 Names(
115 f<2>
116 g<3>))
117 ==>
118 TypeInfo<0>(
119 Name(target.A)
120 Bases(builtins.object<1>)
121 Mro(target.A<0>, builtins.object<1>)
122 Names(
123 f<2>
124 h<4>))
125
126 [case testConstructInstance]
127 import target
128 [file target.py]
129 class A:
130 def f(self) -> B:
131 return B()
132 class B: pass
133 [file target.py.next]
134 class B: pass
135 class A:
136 def f(self) -> B:
137 1
138 return B()
139 [out]
140 MypyFile:1<0>(
141 tmp/main
142 Import:1(target))
143 MypyFile:1<1>(
144 tmp/target.py
145 ClassDef:1<2>(
146 A
147 FuncDef:2<3>(
148 f
149 Args(
150 Var(self))
151 def (self: target.A<4>) -> target.B<5>
152 Block:3<6>(
153 ReturnStmt:3<7>(
154 CallExpr:3<8>(
155 NameExpr(B [target.B<5>])
156 Args())))))
157 ClassDef:4<9>(
158 B
159 PassStmt:4<10>()))
160 ==>
161 MypyFile:1<0>(
162 tmp/main
163 Import:1(target))
164 MypyFile:1<1>(
165 tmp/target.py
166 ClassDef:1<11>(
167 B
168 PassStmt:1<12>())
169 ClassDef:2<13>(
170 A
171 FuncDef:3<3>(
172 f
173 Args(
174 Var(self))
175 def (self: target.A<4>) -> target.B<5>
176 Block:4<14>(
177 ExpressionStmt:4<15>(
178 IntExpr(1))
179 ReturnStmt:5<16>(
180 CallExpr:5<17>(
181 NameExpr(B [target.B<5>])
182 Args()))))))
183
184 [case testCallMethod]
185 import target
186 [file target.py]
187 class A:
188 def f(self) -> None:
189 self.f()
190 [file target.py.next]
191 class A:
192 def f(self) -> None:
193 self.f()
194 [out]
195 MypyFile:1<0>(
196 tmp/main
197 Import:1(target))
198 MypyFile:1<1>(
199 tmp/target.py
200 ClassDef:1<2>(
201 A
202 FuncDef:2<3>(
203 f
204 Args(
205 Var(self))
206 def (self: target.A<4>)
207 Block:3<5>(
208 ExpressionStmt:3<6>(
209 CallExpr:3<7>(
210 MemberExpr:3<8>(
211 NameExpr(self [l<9>])
212 f)
213 Args()))))))
214 ==>
215 MypyFile:1<0>(
216 tmp/main
217 Import:1(target))
218 MypyFile:1<1>(
219 tmp/target.py
220 ClassDef:1<10>(
221 A
222 FuncDef:2<3>(
223 f
224 Args(
225 Var(self))
226 def (self: target.A<4>)
227 Block:3<11>(
228 ExpressionStmt:3<12>(
229 CallExpr:3<13>(
230 MemberExpr:3<14>(
231 NameExpr(self [l<15>])
232 f)
233 Args()))))))
234
235 [case testClassAttribute]
236 import target
237 [file target.py]
238 class A:
239 def f(self) -> None:
240 self.x = 1
241 self.x
242 [file target.py.next]
243 class A:
244 def f(self) -> None:
245 self.x = 1
246 self.x
247 [out]
248 MypyFile:1<0>(
249 tmp/main
250 Import:1(target))
251 MypyFile:1<1>(
252 tmp/target.py
253 ClassDef:1<2>(
254 A
255 FuncDef:2<3>(
256 f
257 Args(
258 Var(self))
259 def (self: target.A<4>)
260 Block:3<5>(
261 AssignmentStmt:3<6>(
262 MemberExpr:3<8>(
263 NameExpr(self [l<9>])
264 x*<7>)
265 IntExpr(1))
266 ExpressionStmt:4<10>(
267 MemberExpr:4<11>(
268 NameExpr(self [l<9>])
269 x))))))
270 ==>
271 MypyFile:1<0>(
272 tmp/main
273 Import:1(target))
274 MypyFile:1<1>(
275 tmp/target.py
276 ClassDef:1<12>(
277 A
278 FuncDef:2<3>(
279 f
280 Args(
281 Var(self))
282 def (self: target.A<4>)
283 Block:3<13>(
284 AssignmentStmt:3<14>(
285 MemberExpr:3<15>(
286 NameExpr(self [l<16>])
287 x*<7>)
288 IntExpr(1))
289 ExpressionStmt:4<17>(
290 MemberExpr:4<18>(
291 NameExpr(self [l<16>])
292 x))))))
293
294 [case testClassAttribute_typeinfo]
295 import target
296 [file target.py]
297 class A:
298 def f(self) -> None:
299 self.x = 1
300 self.x
301 self.y = A()
302 [file target.py.next]
303 class A:
304 def f(self) -> None:
305 self.x = 1
306 self.x
307 self.y = A()
308 [out]
309 TypeInfo<0>(
310 Name(target.A)
311 Bases(builtins.object<1>)
312 Mro(target.A<0>, builtins.object<1>)
313 Names(
314 f<2>
315 x<3> (builtins.int<4>)
316 y<5> (target.A<0>)))
317 ==>
318 TypeInfo<0>(
319 Name(target.A)
320 Bases(builtins.object<1>)
321 Mro(target.A<0>, builtins.object<1>)
322 Names(
323 f<2>
324 x<3> (builtins.int<4>)
325 y<5> (target.A<0>)))
326
327 [case testFunction_symtable]
328 import target
329 [file target.py]
330 def f() -> int:
331 pass
332 [file target.py.next]
333 def f() -> int:
334 pass
335 [out]
336 __main__:
337 target: MypyFile<0>
338 target:
339 f: FuncDef<1>
340 ==>
341 __main__:
342 target: MypyFile<0>
343 target:
344 f: FuncDef<1>
345
346 [case testClass_symtable]
347 import target
348 [file target.py]
349 class A: pass
350 class B: pass
351 [file target.py.next]
352 class A: pass
353 class C: pass
354 [out]
355 __main__:
356 target: MypyFile<0>
357 target:
358 A: TypeInfo<1>
359 B: TypeInfo<2>
360 ==>
361 __main__:
362 target: MypyFile<0>
363 target:
364 A: TypeInfo<1>
365 C: TypeInfo<3>
366
367 [case testTopLevelExpression]
368 import target
369 [file target.py]
370 class A: pass
371 A()
372 [file target.py.next]
373 class A: pass
374 class B: pass
375 A()
376 B()
377 [out]
378 MypyFile:1<0>(
379 tmp/main
380 Import:1(target))
381 MypyFile:1<1>(
382 tmp/target.py
383 ClassDef:1<2>(
384 A
385 PassStmt:1<3>())
386 ExpressionStmt:2<4>(
387 CallExpr:2<5>(
388 NameExpr(A [target.A<6>])
389 Args())))
390 ==>
391 MypyFile:1<0>(
392 tmp/main
393 Import:1(target))
394 MypyFile:1<1>(
395 tmp/target.py
396 ClassDef:1<7>(
397 A
398 PassStmt:1<8>())
399 ClassDef:2<9>(
400 B
401 PassStmt:2<10>())
402 ExpressionStmt:3<11>(
403 CallExpr:3<12>(
404 NameExpr(A [target.A<6>])
405 Args()))
406 ExpressionStmt:4<13>(
407 CallExpr:4<14>(
408 NameExpr(B [target.B<15>])
409 Args())))
410
411 [case testExpression_types]
412 import target
413 [file target.py]
414 class A: pass
415 def f(a: A) -> None:
416 1
417 a
418 [file target.py.next]
419 class A: pass
420 def f(a: A) -> None:
421 a
422 1
423 [out]
424 ## target
425 IntExpr:3: Literal[1]?<0>
426 NameExpr:4: target.A<1>
427 ==>
428 ## target
429 NameExpr:3: target.A<1>
430 IntExpr:4: Literal[1]?<0>
431
432 [case testClassAttribute_types]
433 import target
434 [file target.py]
435 class A:
436 def f(self) -> None:
437 self.x = A()
438 self.x
439 self.y = 1
440 self.y
441 [file target.py.next]
442 class A:
443 def f(self) -> None:
444 self.y = 1
445 self.y
446 self.x = A()
447 self.x
448 [out]
449 ## target
450 CallExpr:3: target.A<0>
451 MemberExpr:3: target.A<0>
452 NameExpr:3: def () -> target.A<0>
453 NameExpr:3: target.A<0>
454 MemberExpr:4: target.A<0>
455 NameExpr:4: target.A<0>
456 IntExpr:5: Literal[1]?<1>
457 MemberExpr:5: builtins.int<1>
458 NameExpr:5: target.A<0>
459 MemberExpr:6: builtins.int<1>
460 NameExpr:6: target.A<0>
461 ==>
462 ## target
463 IntExpr:3: Literal[1]?<1>
464 MemberExpr:3: builtins.int<1>
465 NameExpr:3: target.A<0>
466 MemberExpr:4: builtins.int<1>
467 NameExpr:4: target.A<0>
468 CallExpr:5: target.A<0>
469 MemberExpr:5: target.A<0>
470 NameExpr:5: def () -> target.A<0>
471 NameExpr:5: target.A<0>
472 MemberExpr:6: target.A<0>
473 NameExpr:6: target.A<0>
474
475 [case testMethod_types]
476 import target
477 [file target.py]
478 class A:
479 def f(self) -> A:
480 return self.f()
481 [file target.py.next]
482 class A:
483 # Extra line to change line numbers
484 def f(self) -> A:
485 return self.f()
486 [out]
487 ## target
488 CallExpr:3: target.A<0>
489 MemberExpr:3: def () -> target.A<0>
490 NameExpr:3: target.A<0>
491 ==>
492 ## target
493 CallExpr:4: target.A<0>
494 MemberExpr:4: def () -> target.A<0>
495 NameExpr:4: target.A<0>
496
497 [case testRenameFunction]
498 import target
499 [file target.py]
500 def f() -> int: pass
501 [file target.py.next]
502 def g() -> int: pass
503 [out]
504 MypyFile:1<0>(
505 tmp/main
506 Import:1(target))
507 MypyFile:1<1>(
508 tmp/target.py
509 FuncDef:1<2>(
510 f
511 def () -> builtins.int<3>
512 Block:1<4>(
513 PassStmt:1<5>())))
514 ==>
515 MypyFile:1<0>(
516 tmp/main
517 Import:1(target))
518 MypyFile:1<1>(
519 tmp/target.py
520 FuncDef:1<6>(
521 g
522 def () -> builtins.int<3>
523 Block:1<7>(
524 PassStmt:1<8>())))
525
526 [case testRenameFunction_symtable]
527 import target
528 [file target.py]
529 def f() -> int: pass
530 [file target.py.next]
531 def g() -> int: pass
532 [out]
533 __main__:
534 target: MypyFile<0>
535 target:
536 f: FuncDef<1>
537 ==>
538 __main__:
539 target: MypyFile<0>
540 target:
541 g: FuncDef<2>
542
543 [case testMergeWithBaseClass_typeinfo]
544 import target
545 [file target.py]
546 class A: pass
547 class B(A):
548 def f(self) -> None: pass
549 [file target.py.next]
550 class C: pass
551 class A: pass
552 class B(A):
553 def f(self) -> None: pass
554 [out]
555 TypeInfo<0>(
556 Name(target.A)
557 Bases(builtins.object<1>)
558 Mro(target.A<0>, builtins.object<1>)
559 Names())
560 TypeInfo<2>(
561 Name(target.B)
562 Bases(target.A<0>)
563 Mro(target.B<2>, target.A<0>, builtins.object<1>)
564 Names(
565 f<3>))
566 ==>
567 TypeInfo<0>(
568 Name(target.A)
569 Bases(builtins.object<1>)
570 Mro(target.A<0>, builtins.object<1>)
571 Names())
572 TypeInfo<2>(
573 Name(target.B)
574 Bases(target.A<0>)
575 Mro(target.B<2>, target.A<0>, builtins.object<1>)
576 Names(
577 f<3>))
578 TypeInfo<4>(
579 Name(target.C)
580 Bases(builtins.object<1>)
581 Mro(target.C<4>, builtins.object<1>)
582 Names())
583
584 [case testModuleAttribute]
585 import target
586 [file target.py]
587 x = 1
588 [file target.py.next]
589 x = 2
590 [out]
591 MypyFile:1<0>(
592 tmp/main
593 Import:1(target))
594 MypyFile:1<1>(
595 tmp/target.py
596 AssignmentStmt:1<2>(
597 NameExpr(x [target.x<3>])
598 IntExpr(1)
599 builtins.int<4>))
600 ==>
601 MypyFile:1<0>(
602 tmp/main
603 Import:1(target))
604 MypyFile:1<1>(
605 tmp/target.py
606 AssignmentStmt:1<5>(
607 NameExpr(x [target.x<3>])
608 IntExpr(2)
609 builtins.int<4>))
610
611 [case testNestedClassMethod_typeinfo]
612 import target
613 [file target.py]
614 class A:
615 class B:
616 def f(self) -> None: pass
617 [file target.py.next]
618 class A:
619 class B:
620 def f(self) -> None: pass
621 [out]
622 TypeInfo<0>(
623 Name(target.A)
624 Bases(builtins.object<1>)
625 Mro(target.A<0>, builtins.object<1>)
626 Names(
627 B<2>))
628 TypeInfo<2>(
629 Name(target.A.B)
630 Bases(builtins.object<1>)
631 Mro(target.A.B<2>, builtins.object<1>)
632 Names(
633 f<3>))
634 ==>
635 TypeInfo<0>(
636 Name(target.A)
637 Bases(builtins.object<1>)
638 Mro(target.A<0>, builtins.object<1>)
639 Names(
640 B<2>))
641 TypeInfo<2>(
642 Name(target.A.B)
643 Bases(builtins.object<1>)
644 Mro(target.A.B<2>, builtins.object<1>)
645 Names(
646 f<3>))
647
648 [case testNamedTuple_typeinfo]
649 import target
650 [file target.py]
651 from typing import NamedTuple
652 class A: pass
653 N = NamedTuple('N', [('x', A)])
654 [file target.py.next]
655 from typing import NamedTuple
656 class A: pass
657 N = NamedTuple('N', [('x', A), ('y', A)])
658 [builtins fixtures/tuple.pyi]
659 [out]
660 TypeInfo<0>(
661 Name(target.A)
662 Bases(builtins.object<1>)
663 Mro(target.A<0>, builtins.object<1>)
664 Names())
665 TypeInfo<2>(
666 Name(target.N)
667 Bases(builtins.tuple[target.A<0>, ...]<3>)
668 Mro(target.N<2>, builtins.tuple<3>, typing.Sequence<4>, typing.Iterable<5>, builtins.object<1>)
669 Names(
670 _NT<6>
671 __annotations__<7> (builtins.dict[builtins.str<8>, Any]<9>)
672 __doc__<10> (builtins.str<8>)
673 __match_args__<11> (tuple[Literal['x']])
674 __new__<12>
675 _asdict<13>
676 _field_defaults<14> (builtins.dict[builtins.str<8>, Any]<9>)
677 _field_types<15> (builtins.dict[builtins.str<8>, Any]<9>)
678 _fields<16> (tuple[builtins.str<8>])
679 _make<17>
680 _replace<18>
681 _source<19> (builtins.str<8>)
682 x<20> (target.A<0>)))
683 ==>
684 TypeInfo<0>(
685 Name(target.A)
686 Bases(builtins.object<1>)
687 Mro(target.A<0>, builtins.object<1>)
688 Names())
689 TypeInfo<2>(
690 Name(target.N)
691 Bases(builtins.tuple[target.A<0>, ...]<3>)
692 Mro(target.N<2>, builtins.tuple<3>, typing.Sequence<4>, typing.Iterable<5>, builtins.object<1>)
693 Names(
694 _NT<6>
695 __annotations__<7> (builtins.dict[builtins.str<8>, Any]<9>)
696 __doc__<10> (builtins.str<8>)
697 __match_args__<11> (tuple[Literal['x'], Literal['y']])
698 __new__<12>
699 _asdict<13>
700 _field_defaults<14> (builtins.dict[builtins.str<8>, Any]<9>)
701 _field_types<15> (builtins.dict[builtins.str<8>, Any]<9>)
702 _fields<16> (tuple[builtins.str<8>, builtins.str<8>])
703 _make<17>
704 _replace<18>
705 _source<19> (builtins.str<8>)
706 x<20> (target.A<0>)
707 y<21> (target.A<0>)))
708
709 [case testUnionType_types]
710 import target
711 [file target.py]
712 from typing import Union
713 class A: pass
714 a: A
715 [file target.py.next]
716 from typing import Union
717 class A: pass
718 a: Union[A, int]
719 [out]
720 ## target
721 NameExpr:3: target.A<0>
722 ==>
723 ## target
724 NameExpr:3: target.A<0> | builtins.int<1>
725
726 [case testTypeType_types]
727 import target
728 [file target.py]
729 from typing import Type
730 class A: pass
731 a: Type[A]
732 [file target.py.next]
733 from typing import Type
734 class A: pass
735 a: Type[A]
736 [out]
737 ## target
738 NameExpr:3: type[target.A<0>]
739 ==>
740 ## target
741 NameExpr:3: type[target.A<0>]
742
743 [case testTypeVar_types]
744 import target
745 [file target.py]
746 from typing import TypeVar, Generic
747 T = TypeVar('T', bound=int)
748 class A(Generic[T]):
749 x: T
750 [file target.py.next]
751 from typing import TypeVar
752 T = TypeVar('T', bound='A')
753 class A(Generic[T]):
754 x: T
755 [out]
756 ## target
757 CallExpr:2: Any
758 NameExpr:2: Any
759 TypeVarExpr:2: Any
760 NameExpr:4: T`1(upper_bound=builtins.int<0>)
761 ==>
762 ## target
763 CallExpr:2: Any
764 NameExpr:2: Any
765 TypeVarExpr:2: Any
766 NameExpr:4: T`1(upper_bound=target.A[Any]<1>)
767
768 [case testUnboundType_types]
769
770 import target
771 [file target.py]
772 from typing import TypeVar, Generic
773 class A: pass
774 foo: int
775 x: foo[A]
776 [file target.py.next]
777 from typing import TypeVar, Generic
778 class A: pass
779 foo: int
780 x: foo[A]
781 [out]
782 tmp/target.py:4: error: Variable "target.foo" is not valid as a type
783 tmp/target.py:4: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
784 ## target
785 NameExpr:3: builtins.int<0>
786 NameExpr:4: foo?[target.A<1>]
787 ==>
788 ## target
789 NameExpr:3: builtins.int<0>
790 NameExpr:4: foo?[target.A<1>]
791
792 [case testOverloaded_types]
793 import target
794 [file target.py]
795 from typing import overload
796 class A: pass
797
798 @overload
799 def f(x: A) -> A: pass
800 @overload
801 def f(x: int) -> int: pass
802
803 def f(x): pass
804
805 g = f
806 [file target.py.next]
807 from typing import overload
808
809 class A: pass
810
811 @overload
812 def f(x: A) -> A: pass
813 @overload
814 def f(x: str) -> str: pass
815
816 def f(x): pass
817
818 g = f
819 [out]
820 -- TODO: It is unclear why this works correctly...
821 ## target
822 NameExpr:11: Overload(def (x: target.A<0>) -> target.A<0>, def (x: builtins.int<1>) -> builtins.int<1>)
823 NameExpr:11: Overload(def (x: target.A<0>) -> target.A<0>, def (x: builtins.int<1>) -> builtins.int<1>)
824 ==>
825 ## target
826 NameExpr:12: Overload(def (x: target.A<0>) -> target.A<0>, def (x: builtins.str<2>) -> builtins.str<2>)
827 NameExpr:12: Overload(def (x: target.A<0>) -> target.A<0>, def (x: builtins.str<2>) -> builtins.str<2>)
828
829 [case testOverloaded]
830 import target
831 [file target.py]
832 from typing import overload
833 class A: pass
834
835 @overload
836 def f(x: A) -> A: pass
837 @overload
838 def f(x: int) -> int: pass
839
840 def f(x): pass
841 [file target.py.next]
842 from typing import overload
843
844 class A: pass
845 class B: pass
846
847 @overload
848 def f(x: A) -> B: pass
849 @overload
850 def f(x: str) -> str: pass
851
852 def f(x): pass
853 [out]
854 MypyFile:1<0>(
855 tmp/main
856 Import:1(target))
857 MypyFile:1<1>(
858 tmp/target.py
859 ImportFrom:1(typing, [overload])
860 ClassDef:2<2>(
861 A
862 PassStmt:2<3>())
863 OverloadedFuncDef:4<4>(
864 FuncDef:9<5>(
865 f
866 Args(
867 Var(x))
868 Block:9<6>(
869 PassStmt:9<7>()))
870 Overload(def (x: target.A<8>) -> target.A<8>, def (x: builtins.int<9>) -> builtins.int<9>)
871 Decorator:4<10>(
872 Var(f)
873 NameExpr(overload [typing.overload<11>])
874 FuncDef:5<12>(
875 f
876 Args(
877 Var(x))
878 def (x: target.A<8>) -> target.A<8>
879 Block:5<13>(
880 PassStmt:5<14>())))
881 Decorator:6<15>(
882 Var(f)
883 NameExpr(overload [typing.overload<11>])
884 FuncDef:7<16>(
885 f
886 Args(
887 Var(x))
888 def (x: builtins.int<9>) -> builtins.int<9>
889 Block:7<17>(
890 PassStmt:7<18>())))))
891 ==>
892 MypyFile:1<0>(
893 tmp/main
894 Import:1(target))
895 MypyFile:1<1>(
896 tmp/target.py
897 ImportFrom:1(typing, [overload])
898 ClassDef:3<19>(
899 A
900 PassStmt:3<20>())
901 ClassDef:4<21>(
902 B
903 PassStmt:4<22>())
904 OverloadedFuncDef:6<4>(
905 FuncDef:11<23>(
906 f
907 Args(
908 Var(x))
909 Block:11<24>(
910 PassStmt:11<25>()))
911 Overload(def (x: target.A<8>) -> target.B<26>, def (x: builtins.str<27>) -> builtins.str<27>)
912 Decorator:6<28>(
913 Var(f)
914 NameExpr(overload [typing.overload<11>])
915 FuncDef:7<29>(
916 f
917 Args(
918 Var(x))
919 def (x: target.A<8>) -> target.B<26>
920 Block:7<30>(
921 PassStmt:7<31>())))
922 Decorator:8<32>(
923 Var(f)
924 NameExpr(overload [typing.overload<11>])
925 FuncDef:9<33>(
926 f
927 Args(
928 Var(x))
929 def (x: builtins.str<27>) -> builtins.str<27>
930 Block:9<34>(
931 PassStmt:9<35>())))))
932
933 [case testTypeVar_symtable]
934 import target
935
936 [file target.py]
937 from typing import TypeVar
938 T = TypeVar('T')
939
940 [file target.py.next]
941 from typing import TypeVar
942 T = TypeVar('T', bound=int)
943
944 [out]
945 __main__:
946 target: MypyFile<0>
947 target:
948 T: TypeVarExpr<1>
949 TypeVar: Var<2>
950 ==>
951 __main__:
952 target: MypyFile<0>
953 target:
954 T: TypeVarExpr<1>
955 TypeVar: Var<2>
956
957 [case testTypeAlias_symtable]
958 import target
959
960 [file target.py]
961 from typing import TypeVar, Generic
962 T = TypeVar('T')
963 class A(Generic[T]): pass
964 X = A[int]
965
966 [file target.py.next]
967 from typing import TypeVar, Generic
968 T = TypeVar('T')
969 class A(Generic[T]): pass
970 X = A[str]
971
972 [out]
973 __main__:
974 target: MypyFile<0>
975 target:
976 A: TypeInfo<1>
977 Generic: Var<2>
978 T: TypeVarExpr<3>
979 TypeVar: Var<4>
980 X: TypeAlias<5>
981 ==>
982 __main__:
983 target: MypyFile<0>
984 target:
985 A: TypeInfo<1>
986 Generic: Var<2>
987 T: TypeVarExpr<3>
988 TypeVar: Var<4>
989 X: TypeAlias<5>
990
991 [case testGenericFunction_types]
992 import target
993
994 [file target.py]
995 from typing import TypeVar
996 class A: pass
997 T = TypeVar('T', bound=A)
998 def f(x: T) -> T: pass
999 f
1000
1001 [file target.py.next]
1002 from typing import TypeVar
1003 class A: pass
1004 T = TypeVar('T', bound=A)
1005 def f(x: T, y: A) -> T: pass
1006 f
1007
1008 [out]
1009 ## target
1010 CallExpr:3: Any
1011 NameExpr:3: Any
1012 TypeVarExpr:3: Any
1013 NameExpr:5: def [T <: target.A<0>] (x: T`-1(upper_bound=target.A<0>)) -> T`-1(upper_bound=target.A<0>)
1014 ==>
1015 ## target
1016 CallExpr:3: Any
1017 NameExpr:3: Any
1018 TypeVarExpr:3: Any
1019 NameExpr:5: def [T <: target.A<0>] (x: T`-1(upper_bound=target.A<0>), y: target.A<0>) -> T`-1(upper_bound=target.A<0>)
1020
1021 [case testMergeOverloaded_types]
1022 import target
1023 [file target.py]
1024 from _x import A
1025 a: A
1026 [file target.py.next]
1027 from _x import A
1028 a: A
1029 [fixture _x.pyi]
1030 from typing import Generic, TypeVar, overload
1031
1032 T = TypeVar('T')
1033
1034 class C(Generic[T]):
1035 @overload
1036 def __init__(self) -> None: pass
1037 @overload
1038 def __init__(self, x: int) -> None: pass
1039 A = C[int]
1040 [out]
1041 ## target
1042 NameExpr:2: _x.C[builtins.int<0>]<1>
1043 ==>
1044 ## target
1045 NameExpr:2: _x.C[builtins.int<0>]<1>
1046
1047 [case testRefreshVar_symtable]
1048 from typing import TypeVar
1049 from target import f
1050 x = 1
1051 y = '' # type: str
1052 [file target.py]
1053 f = 1
1054 [file target.py.next]
1055 [out]
1056 __main__:
1057 TypeVar: Var<0>
1058 f: Var<1>(builtins.int<2>)
1059 x: Var<3>(builtins.int<2>)
1060 y: Var<4>(builtins.str<5>)
1061 target:
1062 f: Var<1>(builtins.int<2>)
1063 ==>
1064 __main__:
1065 TypeVar: Var<0>
1066 f: Var<6>(Any)
1067 x: Var<3>(builtins.int<2>)
1068 y: Var<4>(builtins.str<5>)
1069 target:
1070
1071 [case testRefreshTypeVar_symtable]
1072 from typing import TypeVar
1073 from target import f
1074 T = TypeVar('T')
1075 [file target.py]
1076 f = 1
1077 [file target.py.next]
1078 [out]
1079 __main__:
1080 T: TypeVarExpr<0>
1081 TypeVar: Var<1>
1082 f: Var<2>(builtins.int<3>)
1083 target:
1084 f: Var<2>(builtins.int<3>)
1085 ==>
1086 __main__:
1087 T: TypeVarExpr<0>
1088 TypeVar: Var<1>
1089 f: Var<4>(Any)
1090 target:
1091
1092 [case testRefreshNamedTuple_symtable]
1093 from typing import NamedTuple
1094 from target import f
1095 N = NamedTuple('N', [('x', int)])
1096 [file target.py]
1097 f = 1
1098 [file target.py.next]
1099 [builtins fixtures/tuple.pyi]
1100 [out]
1101 __main__:
1102 N: TypeInfo<0>
1103 NamedTuple: Var<1>
1104 f: Var<2>(builtins.int<3>)
1105 target:
1106 f: Var<2>(builtins.int<3>)
1107 ==>
1108 __main__:
1109 N: TypeInfo<0>
1110 NamedTuple: Var<1>
1111 f: Var<4>(Any)
1112 target:
1113
1114 [case testRefreshAttributeDefinedInClassBody_typeinfo]
1115 from target import f
1116 class A:
1117 a = 1
1118 b = '' # type: str
1119 [file target.py]
1120 f = 1
1121 [file target.py.next]
1122 [out]
1123 TypeInfo<0>(
1124 Name(__main__.A)
1125 Bases(builtins.object<1>)
1126 Mro(__main__.A<0>, builtins.object<1>)
1127 Names(
1128 a<2> (builtins.int<3>)
1129 b<4> (builtins.str<5>)))
1130 ==>
1131 TypeInfo<0>(
1132 Name(__main__.A)
1133 Bases(builtins.object<1>)
1134 Mro(__main__.A<0>, builtins.object<1>)
1135 Names(
1136 a<2> (builtins.int<3>)
1137 b<4> (builtins.str<5>)))
1138
1139 [case testDecorator_symtable]
1140 import target
1141 [file target.py]
1142 from contextlib import contextmanager
1143 from typing import Iterator, List, Tuple
1144
1145 @contextmanager
1146 def f(x: List[Tuple[int]]) -> Iterator[None]:
1147 yield
1148 [file target.py.next]
1149 from contextlib import contextmanager
1150 from typing import Iterator, List, Tuple
1151
1152 @contextmanager
1153 def f(x: List[Tuple[int]]) -> Iterator[None]:
1154 yield
1155 [typing fixtures/typing-medium.pyi]
1156 [builtins fixtures/list.pyi]
1157 [out]
1158 __main__:
1159 target: MypyFile<0>
1160 target:
1161 Iterator: TypeInfo<1>
1162 List: TypeAlias<2>
1163 Tuple: Var<3>
1164 contextmanager: FuncDef<4>
1165 f: Decorator<5>
1166 ==>
1167 __main__:
1168 target: MypyFile<0>
1169 target:
1170 Iterator: TypeInfo<1>
1171 List: TypeAlias<2>
1172 Tuple: Var<3>
1173 contextmanager: FuncDef<4>
1174 f: Decorator<5>
1175
1176 [case testConditionalFunctionDefinition]
1177 import target
1178 [file target.py]
1179 import sys
1180 class A: pass
1181 class B: pass
1182 if sys.platform == 'nonexistent':
1183 def f(x: A) -> None: pass
1184 else:
1185 def f(x: B) -> None: pass
1186 [file target.py.next]
1187 import sys
1188 class A: pass
1189 class B: pass
1190 if sys.platform == 'nonexistent':
1191 def f(x: A, y: int) -> None: pass
1192 else:
1193 def f(x: B, y: int) -> None: pass
1194 [builtins fixtures/ops.pyi]
1195 [out]
1196 MypyFile:1<0>(
1197 tmp/main
1198 Import:1(target))
1199 MypyFile:1<1>(
1200 tmp/target.py
1201 Import:1(sys)
1202 ClassDef:2<2>(
1203 A
1204 PassStmt:2<3>())
1205 ClassDef:3<4>(
1206 B
1207 PassStmt:3<5>())
1208 IfStmt:4<6>(
1209 If(
1210 ComparisonExpr:4<7>(
1211 ==
1212 MemberExpr:4<9>(
1213 NameExpr(sys<10>)
1214 platform [sys.platform<8>])
1215 StrExpr(nonexistent)))
1216 Then(
1217 FuncDef:5<11>(
1218 f
1219 Args(
1220 Var(x))
1221 def (x: A?) -> None?
1222 Block:5<12>(
1223 PassStmt:5<13>())))
1224 Else(
1225 FuncDef:7<14>(
1226 f
1227 Args(
1228 Var(x))
1229 def (x: target.B<15>)
1230 Block:7<16>(
1231 PassStmt:7<17>())))))
1232 ==>
1233 MypyFile:1<0>(
1234 tmp/main
1235 Import:1(target))
1236 MypyFile:1<1>(
1237 tmp/target.py
1238 Import:1(sys)
1239 ClassDef:2<18>(
1240 A
1241 PassStmt:2<19>())
1242 ClassDef:3<20>(
1243 B
1244 PassStmt:3<21>())
1245 IfStmt:4<22>(
1246 If(
1247 ComparisonExpr:4<23>(
1248 ==
1249 MemberExpr:4<24>(
1250 NameExpr(sys<10>)
1251 platform [sys.platform<8>])
1252 StrExpr(nonexistent)))
1253 Then(
1254 FuncDef:5<25>(
1255 f
1256 Args(
1257 Var(x)
1258 Var(y))
1259 def (x: A?, y: int?) -> None?
1260 Block:5<26>(
1261 PassStmt:5<27>())))
1262 Else(
1263 FuncDef:7<14>(
1264 f
1265 Args(
1266 Var(x)
1267 Var(y))
1268 def (x: target.B<15>, y: builtins.int<28>)
1269 Block:7<29>(
1270 PassStmt:7<30>())))))
1271
1272 [case testMergeTypedDict_symtable]
1273 import target
1274 [file target.py]
1275 from typing import TypedDict
1276 class A: pass
1277 D = TypedDict('D', {'a': A})
1278 d: D
1279 [file target.py.next]
1280 from typing import TypedDict
1281 class A: pass
1282 D = TypedDict('D', {'a': A, 'b': int})
1283 d: D
1284 [builtins fixtures/dict.pyi]
1285 [typing fixtures/typing-typeddict.pyi]
1286
1287 [out]
1288 __main__:
1289 target: MypyFile<0>
1290 target:
1291 A: TypeInfo<1>
1292 D: TypeInfo<2>
1293 TypedDict: Var<3>
1294 d: Var<4>(TypedDict('target.D', {'a': target.A<1>}))
1295 ==>
1296 __main__:
1297 target: MypyFile<0>
1298 target:
1299 A: TypeInfo<1>
1300 D: TypeInfo<2>
1301 TypedDict: Var<3>
1302 d: Var<4>(TypedDict('target.D', {'a': target.A<1>, 'b': builtins.int<5>}))
1303
1304 [case testNewType_symtable]
1305 import target
1306 [file target.py]
1307 from typing import NewType
1308 class A: pass
1309 B = NewType('B', A)
1310 C = NewType('C', A)
1311 [file target.py.next]
1312 from typing import NewType
1313 class A: pass
1314 B = NewType('B', A)
1315 C = NewType('C', B)
1316 D = NewType('D', int)
1317 [out]
1318 __main__:
1319 target: MypyFile<0>
1320 target:
1321 A: TypeInfo<1>
1322 B: TypeInfo<2>
1323 C: TypeInfo<3>
1324 NewType: Var<4>
1325 ==>
1326 __main__:
1327 target: MypyFile<0>
1328 target:
1329 A: TypeInfo<1>
1330 B: TypeInfo<2>
1331 C: TypeInfo<3>
1332 D: TypeInfo<5>
1333 NewType: Var<4>
1334
1335 [case testCallable_symtable-skip]
1336 # The TypeInfo is currently not being merged correctly
1337 import target
1338 [file target.py]
1339 def g(o: object) -> None:
1340 if callable(o):
1341 pass
1342 [file target.py.next]
1343 def g(o: object) -> None:
1344 if callable(o):
1345 o()
1346 [builtins fixtures/callable.pyi]
1347 [out]
1348 __main__:
1349 target: MypyFile<0>
1350 target:
1351 <callable subtype of object>: TypeInfo<1>
1352 g: FuncDef<2>
1353 ==>
1354 __main__:
1355 target: MypyFile<0>
1356 target:
1357 <callable subtype of object>: TypeInfo<1>
1358 g: FuncDef<2>
1359
1360 [case testMetaclass_typeinfo]
1361 import target
1362 [file target.py]
1363 class M(type): pass
1364 class C(metaclass=M):
1365 pass
1366 [file target.py.next]
1367 class M(type): pass
1368 class C(metaclass=M):
1369 pass # dummy change
1370 [out]
1371 TypeInfo<0>(
1372 Name(target.C)
1373 Bases(builtins.object<1>)
1374 Mro(target.C<0>, builtins.object<1>)
1375 Names()
1376 DeclaredMetaclass(target.M<2>)
1377 MetaclassType(target.M<2>))
1378 TypeInfo<2>(
1379 Name(target.M)
1380 Bases(builtins.type<3>)
1381 Mro(target.M<2>, builtins.type<3>, builtins.object<1>)
1382 Names())
1383 ==>
1384 TypeInfo<0>(
1385 Name(target.C)
1386 Bases(builtins.object<1>)
1387 Mro(target.C<0>, builtins.object<1>)
1388 Names()
1389 DeclaredMetaclass(target.M<2>)
1390 MetaclassType(target.M<2>))
1391 TypeInfo<2>(
1392 Name(target.M)
1393 Bases(builtins.type<3>)
1394 Mro(target.M<2>, builtins.type<3>, builtins.object<1>)
1395 Names())
1396
1397 [case testCast_symtable]
1398 import target
1399 [file target.py]
1400 from typing import cast
1401 class Thing:
1402 pass
1403 thing = cast(Thing, Thing())
1404 [file target.py.next]
1405 from typing import cast
1406 class Thing:
1407 pass
1408 thing = cast(Thing, Thing())
1409 [out]
1410 __main__:
1411 target: MypyFile<0>
1412 target:
1413 Thing: TypeInfo<1>
1414 cast: Var<2>
1415 thing: Var<3>(target.Thing<1>)
1416 ==>
1417 __main__:
1418 target: MypyFile<0>
1419 target:
1420 Thing: TypeInfo<1>
1421 cast: Var<2>
1422 thing: Var<3>(target.Thing<1>)
1423
1424 [case testClassBasedEnum_typeinfo]
1425 import target
1426 [file target.py]
1427 from enum import Enum
1428 class A(Enum):
1429 X = 0
1430 [file target.py.next]
1431 from enum import Enum
1432 class A(Enum):
1433 X = 0
1434 Y = 1
1435 [builtins fixtures/enum.pyi]
1436 [out]
1437 TypeInfo<0>(
1438 Name(target.A)
1439 Bases(enum.Enum<1>)
1440 Mro(target.A<0>, enum.Enum<1>, builtins.object<2>)
1441 Names(
1442 X<3> (Literal[0]?<4>))
1443 MetaclassType(enum.EnumMeta<5>))
1444 ==>
1445 TypeInfo<0>(
1446 Name(target.A)
1447 Bases(enum.Enum<1>)
1448 Mro(target.A<0>, enum.Enum<1>, builtins.object<2>)
1449 Names(
1450 X<3> (Literal[0]?<4>)
1451 Y<6> (Literal[1]?<4>))
1452 MetaclassType(enum.EnumMeta<5>))
1453
1454 [case testLiteralMerge]
1455 import target
1456 [file target.py]
1457 from typing import Literal
1458 def foo(x: Literal[3]) -> Literal['a']: pass
1459 bar: Literal[4] = 4
1460 [file target.py.next]
1461 from typing import Literal
1462 def foo(x: Literal['3']) -> Literal['b']: pass
1463 bar: Literal[5] = 5
1464 [builtins fixtures/tuple.pyi]
1465 [out]
1466 MypyFile:1<0>(
1467 tmp/main
1468 Import:1(target))
1469 MypyFile:1<1>(
1470 tmp/target.py
1471 ImportFrom:1(typing, [Literal])
1472 FuncDef:2<2>(
1473 foo
1474 Args(
1475 Var(x))
1476 def (x: Literal[3]) -> Literal['a']
1477 Block:2<3>(
1478 PassStmt:2<4>()))
1479 AssignmentStmt:3<5>(
1480 NameExpr(bar [target.bar<6>])
1481 IntExpr(4)
1482 Literal[4]))
1483 ==>
1484 MypyFile:1<0>(
1485 tmp/main
1486 Import:1(target))
1487 MypyFile:1<1>(
1488 tmp/target.py
1489 ImportFrom:1(typing, [Literal])
1490 FuncDef:2<2>(
1491 foo
1492 Args(
1493 Var(x))
1494 def (x: Literal['3']) -> Literal['b']
1495 Block:2<7>(
1496 PassStmt:2<8>()))
1497 AssignmentStmt:3<9>(
1498 NameExpr(bar [target.bar<6>])
1499 IntExpr(5)
1500 Literal[5]))