Debian Codegraph @

test-data/unit/diff.test

raw
1 -- Test cases for taking a diff of two module ASTs/symbol tables.
2 -- The diffs are used for fined-grained incremental checking.
3
4 [case testChangeTypeOfModuleAttribute]
5 x = 1
6 y = 1
7 [file next.py]
8 x = ''
9 y = 1
10 [out]
11 __main__.x
12
13 [case testChangeSignatureOfModuleFunction]
14 def f(x: int) -> None:
15 pass
16 def g(y: str) -> None:
17 pass
18 [file next.py]
19 def f(x: str) -> None:
20 x = ''
21 def g(y: str) -> None:
22 y = ''
23 [out]
24 __main__.f
25
26 [case testAddModuleAttribute]
27 x = 1
28 [file next.py]
29 x = 1
30 y = 1
31 [out]
32 __main__.y
33
34 [case testRemoveModuleAttribute]
35 x = 1
36 y = 1
37 [file next.py]
38 x = 1
39 [out]
40 __main__.y
41
42 --
43 -- Classes
44 --
45
46 [case testChangeMethodSignature]
47 class A:
48 def f(self) -> None: pass
49 def g(self) -> None: pass
50 [file next.py]
51 class A:
52 def f(self, x: int) -> None: pass
53 def g(self) -> None: pass
54 [out]
55 __main__.A.f
56
57 [case testChangeAttributeType]
58 class A:
59 def f(self) -> None:
60 self.x = 1
61 self.y = 1
62 [file next.py]
63 class A:
64 def f(self) -> None:
65 self.x = 1
66 self.y = ''
67 [out]
68 __main__.A.y
69
70 [case testAddAttribute]
71 class A: pass
72 [file next.py]
73 class A:
74 def f(self) -> None:
75 self.x = 1
76 [out]
77 __main__.A.f
78 __main__.A.x
79
80 [case testAddAttribute2]
81 class A:
82 def f(self) -> None: pass
83 [file next.py]
84 class A:
85 def f(self) -> None:
86 self.x = 1
87 [out]
88 __main__.A.x
89
90 [case testRemoveAttribute]
91 class A:
92 def f(self) -> None:
93 self.x = 1
94 [file next.py]
95 class A: pass
96 [out]
97 __main__.A.f
98 __main__.A.x
99
100 [case testAddMethod]
101 class A:
102 def f(self) -> None: pass
103 [file next.py]
104 class A:
105 def f(self) -> None: pass
106 def g(self) -> None: pass
107 [out]
108 __main__.A.g
109
110 [case testRemoveMethod]
111 class A:
112 def f(self) -> None: pass
113 def g(self) -> None: pass
114 [file next.py]
115 class A:
116 def f(self) -> None: pass
117 [out]
118 __main__.A.g
119
120 [case testAddImport]
121 import nn
122 [file next.py]
123 import n
124 import nn
125 [file n.py]
126 x = 1
127 [file nn.py]
128 y = 1
129 [out]
130 __main__.n
131
132 [case testRemoveImport]
133 import n
134 [file next.py]
135 [file n.py]
136 x = 1
137 [out]
138 __main__.n
139
140 [case testChangeClassIntoFunction]
141 class A: pass
142 [file next.py]
143 def A() -> None: pass
144 [out]
145 __main__.A
146
147 [case testDeleteClass]
148 class A: pass
149 [file next.py]
150 [out]
151 __main__.A
152
153 [case testAddBaseClass]
154 class A: pass
155 [file next.py]
156 class B: pass
157 class A(B): pass
158 [out]
159 __main__.A
160 __main__.B
161
162 [case testChangeBaseClass]
163 class A: pass
164 class B: pass
165 class C(A): pass
166 [file next.py]
167 class A: pass
168 class B: pass
169 class C(B): pass
170 [out]
171 __main__.C
172
173 [case testRemoveBaseClass]
174 class A: pass
175 class B(A): pass
176 [file next.py]
177 class A: pass
178 class B: pass
179 [out]
180 __main__.B
181
182 [case testRemoveClassFromMiddleOfMro]
183 class A: pass
184 class B(A): pass
185 class C(B): pass
186 [file next.py]
187 class A: pass
188 class B: pass
189 class C(B): pass
190 [out]
191 __main__.B
192 __main__.C
193
194 [case testDifferenceInConstructor]
195 class A:
196 def __init__(self) -> None: pass
197 [file next.py]
198 class A:
199 def __init__(self, x: int) -> None: pass
200 [out]
201 __main__.A.__init__
202
203 [case testChangeSignatureOfMethodInNestedClass]
204 class A:
205 class B:
206 def f(self) -> int: pass
207 [file next.py]
208 class A:
209 class B:
210 def f(self) -> str: pass
211 [out]
212 __main__.A.B.f
213
214 [case testChangeTypeOfAttributeInNestedClass]
215 class A:
216 class B:
217 def f(self) -> None:
218 self.x = 1
219 [file next.py]
220 class A:
221 class B:
222 def f(self) -> None:
223 self.x = ''
224 [out]
225 __main__.A.B.x
226
227 [case testAddMethodToNestedClass]
228 class A:
229 class B: pass
230 [file next.py]
231 class A:
232 class B:
233 def f(self) -> str: pass
234 [out]
235 __main__.A.B.f
236
237 [case testAddNestedClass]
238 class A: pass
239 [file next.py]
240 class A:
241 class B:
242 def f(self) -> None: pass
243 [out]
244 __main__.A.B
245
246 [case testRemoveNestedClass]
247 class A:
248 class B:
249 def f(self) -> None: pass
250 [file next.py]
251 class A: pass
252 [out]
253 __main__.A.B
254
255 [case testChangeNestedClassToMethod]
256 class A:
257 class B: pass
258 [file next.py]
259 class A:
260 def B(self) -> None: pass
261
262 [out]
263 __main__.A.B
264
265 [case testChangeNamedTupleAttribute]
266
267 from typing import NamedTuple
268 class A:
269 x: str
270 N = NamedTuple('N', [('x', int), ('y', str)])
271 M = NamedTuple('M', [('x', int), ('y', str)])
272 [file next.py]
273 from typing import NamedTuple
274 N = NamedTuple('N', [('x', int), ('y', int)])
275 M = NamedTuple('M', [('x', int), ('y', str)])
276 [builtins fixtures/tuple.pyi]
277 [out]
278 __main__.A
279 __main__.N
280 __main__.N._NT
281 __main__.N.__new__
282 __main__.N._asdict
283 __main__.N._make
284 __main__.N._replace
285 __main__.N.y
286
287 [case testSimpleDecoratedFunction]
288 from a import dec
289 @dec
290 def f() -> None: pass
291 @dec
292 def g() -> None: pass
293 [file next.py]
294 from a import dec
295 @dec
296 def f(x: int) -> None: pass
297 @dec
298 def g() -> None: pass
299 [file a.py]
300 from typing import TypeVar
301
302 T = TypeVar('T')
303
304 def dec(f: T) -> T:
305 return f
306 [out]
307 __main__.f
308
309 [case testSimpleDecoratedMethod]
310 from a import dec
311 class A:
312 @dec
313 def f(self) -> None:
314 self.g()
315 @dec
316 def g(self) -> None: pass
317 [file next.py]
318 from a import dec
319 class A:
320 @dec
321 def f(self, x: int) -> None:
322 self.g()
323 @dec
324 def g(self) -> None: pass
325 [file a.py]
326 from typing import TypeVar
327
328 T = TypeVar('T')
329
330 def dec(f: T) -> T:
331 return f
332 [out]
333 __main__.A.f
334
335 [case testTypeVarBound]
336 from typing import TypeVar
337 T = TypeVar('T')
338 S = TypeVar('S')
339 [file next.py]
340 from typing import TypeVar
341 T = TypeVar('T', bound=int)
342 S = TypeVar('S')
343 [out]
344 __main__.T
345
346 [case testTypeVarVariance]
347 from typing import TypeVar
348 A = TypeVar('A', covariant=True)
349 B = TypeVar('B', covariant=True)
350 C = TypeVar('C', covariant=True)
351 [file next.py]
352 from typing import TypeVar
353 A = TypeVar('A', covariant=True)
354 B = TypeVar('B', contravariant=True)
355 C = TypeVar('C')
356 [out]
357 __main__.B
358 __main__.C
359
360 [case testTypeVarValues]
361 from typing import TypeVar
362 A = TypeVar('A', int, str)
363 B = TypeVar('B', int, str)
364 C = TypeVar('C', int, str)
365 [file next.py]
366 from typing import TypeVar
367 A = TypeVar('A', int, str)
368 B = TypeVar('B', int, str, object)
369 C = TypeVar('C')
370 [out]
371 __main__.B
372 __main__.C
373
374 [case testGenericFunction]
375 from typing import TypeVar
376 T = TypeVar('T')
377 S = TypeVar('S')
378 def f(x: T) -> T: pass
379 def g(x: S) -> S: pass
380 [file next.py]
381 from typing import TypeVar
382 T = TypeVar('T', int, str)
383 S = TypeVar('S')
384 def f(x: T) -> T: pass
385 def g(x: S) -> S: pass
386 [out]
387 __main__.T
388 __main__.f
389
390 [case testGenericTypes]
391 from typing import List
392 x: List[int]
393 y: List[int]
394 [file next.py]
395 from typing import List
396 x: List[int]
397 y: List[str]
398 [builtins fixtures/list.pyi]
399 [out]
400 __main__.y
401
402 [case testTypeAliasOfList]
403 from typing import List
404 X = List[int]
405 Y = List[int]
406 [file next.py]
407 from typing import List
408 X = List[str]
409 Y = List[int]
410 [builtins fixtures/list.pyi]
411 [out]
412 __main__.X
413
414 [case testTypeAliasOfCallable]
415 from typing import Callable
416 A = Callable[[int], str]
417 B = Callable[[int], str]
418 C = Callable[[int], str]
419 [file next.py]
420 from typing import Callable
421 A = Callable[[int], str]
422 B = Callable[[], str]
423 C = Callable[[int], int]
424 [out]
425 __main__.B
426 __main__.C
427
428 [case testGenericTypeAlias]
429 from typing import Callable, TypeVar
430 T = TypeVar('T')
431 A = Callable[[T], T]
432 B = Callable[[T], T]
433 [file next.py]
434 from typing import Callable, TypeVar
435 T = TypeVar('T')
436 S = TypeVar('S')
437 A = Callable[[T], T]
438 B = Callable[[T], S]
439 [out]
440 __main__.B
441 __main__.S
442
443 [case testDifferentListTypes]
444 from typing import List
445 A = List
446 B = list
447 C = List
448 [file next.py]
449 from typing import List
450 A = List
451 B = list
452 C = list
453 [builtins fixtures/list.pyi]
454 [out]
455 __main__.C
456
457 [case testDecoratorChangesSignature]
458 from contextlib import contextmanager
459 from typing import Iterator, List, Tuple
460
461 @contextmanager
462 def f(x: List[Tuple[int]]) -> Iterator[None]:
463 yield
464
465 @contextmanager
466 def g(x: str) -> Iterator[None]:
467 yield
468 [file next.py]
469 from contextlib import contextmanager
470 from typing import Iterator, List, Tuple
471
472 @contextmanager
473 def f(x: List[Tuple[int]]) -> Iterator[None]:
474 yield
475
476 @contextmanager
477 def g(x: object) -> Iterator[None]:
478 yield
479 [typing fixtures/typing-medium.pyi]
480 [builtins fixtures/list.pyi]
481 [out]
482 __main__.g
483
484 [case testOverloadedMethod]
485 from typing import overload
486
487 class A:
488 @overload
489 def f(self, x: int) -> int: pass
490 @overload
491 def f(self, x: str) -> str: pass
492 def f(self, x): pass
493
494 @overload
495 def g(self, x: int) -> int: pass
496 @overload
497 def g(self, x: str) -> str: pass
498 def g(self, x): pass
499 [file next.py]
500 from typing import overload
501
502 class A:
503 @overload
504 def f(self, x: int) -> int: pass
505 @overload
506 def f(self, x: str) -> str: pass
507 def f(self, x): pass
508
509 @overload
510 def g(self, x: int) -> int: pass
511 @overload
512 def g(self, x: object) -> object: pass
513 def g(self, x): pass
514 [out]
515 __main__.A.g
516
517 [case testPropertyWithSetter]
518 class A:
519 @property
520 def x(self) -> int:
521 pass
522
523 @x.setter
524 def x(self, o: int) -> None:
525 pass
526
527 class B:
528 @property
529 def x(self) -> int:
530 pass
531
532 @x.setter
533 def x(self, o: int) -> None:
534 pass
535 [file next.py]
536 class A:
537 @property
538 def x(self) -> int:
539 pass
540
541 @x.setter
542 def x(self, o: int) -> None:
543 pass
544
545 class B:
546 @property
547 def x(self) -> str:
548 pass
549
550 @x.setter
551 def x(self, o: str) -> None:
552 pass
553 [builtins fixtures/property.pyi]
554 [out]
555 __main__.B.x
556
557 [case testFunctionalEnum]
558 from enum import Enum, IntEnum
559 A = Enum('A', 'x')
560 B = Enum('B', 'x')
561 C = Enum('C', 'x')
562 D = IntEnum('D', 'x')
563 [file next.py]
564 from enum import Enum, IntEnum
565 A = Enum('A', 'x')
566 B = Enum('B', 'y')
567 C = IntEnum('C', 'x')
568 D = IntEnum('D', 'x y')
569 [builtins fixtures/enum.pyi]
570 [out]
571 __main__.B.x
572 __main__.B.y
573 __main__.C
574 __main__.D.y
575
576 [case testClassBasedEnum]
577 from enum import Enum
578 class A(Enum):
579 X = 0
580 Y = 1
581 class B(Enum):
582 X = 0
583 Y = 1
584 class C(Enum):
585 X = 0
586 Y = 1
587 class D(Enum):
588 X = 0
589 Y = 1
590 class E(Enum):
591 X = 0
592 [file next.py]
593 from enum import Enum
594 class A(Enum):
595 X = 0
596 Y = 1
597 class B(Enum):
598 X = 0
599 Z = 1
600 class C(Enum):
601 X = 0
602 Y = 1
603 Z = 2
604 class D(Enum):
605 X = 'a'
606 Y = 'b'
607 class F(Enum):
608 X = 0
609 [builtins fixtures/enum.pyi]
610 [out]
611 __main__.B.Y
612 __main__.B.Z
613 __main__.C.Z
614 __main__.D.X
615 __main__.D.Y
616 __main__.E
617 __main__.F
618
619 [case testTypedDict]
620 from typing import TypedDict
621 Point = TypedDict('Point', {'x': int, 'y': int})
622 p = Point(dict(x=42, y=1337))
623 [file next.py]
624 from typing import TypedDict
625 Point = TypedDict('Point', {'x': int, 'y': str})
626 p = Point(dict(x=42, y='lurr'))
627 [builtins fixtures/dict.pyi]
628 [typing fixtures/typing-typeddict.pyi]
629 [out]
630 __main__.Point
631 __main__.p
632
633 [case testTypedDict2]
634 from typing import TypedDict
635 class Point(TypedDict):
636 x: int
637 y: int
638 p = Point(dict(x=42, y=1337))
639 [file next.py]
640 from typing import TypedDict
641 class Point(TypedDict):
642 x: int
643 y: str
644 p = Point(dict(x=42, y='lurr'))
645 [builtins fixtures/dict.pyi]
646 [typing fixtures/typing-typeddict.pyi]
647 [out]
648 __main__.Point
649 __main__.p
650
651 [case testTypedDict3]
652 from typing import TypedDict
653 Point = TypedDict('Point', {'x': int, 'y': int})
654 p = Point(dict(x=42, y=1337))
655 [file next.py]
656 from typing import TypedDict
657 Point = TypedDict('Point', {'x': int})
658 p = Point(dict(x=42))
659 [builtins fixtures/dict.pyi]
660 [typing fixtures/typing-typeddict.pyi]
661 [out]
662 __main__.Point
663 __main__.p
664
665 [case testTypedDict4]
666 from typing import TypedDict
667 Point = TypedDict('Point', {'x': int, 'y': int})
668 p = Point(dict(x=42, y=1337))
669 [file next.py]
670 from typing import TypedDict
671 Point = TypedDict('Point', {'x': int, 'y': int}, total=False)
672 p = Point(dict(x=42, y=1337))
673 [builtins fixtures/dict.pyi]
674 [typing fixtures/typing-typeddict.pyi]
675 [out]
676 __main__.Point
677 __main__.p
678
679 [case testTypedDict5]
680 from typing import TypedDict
681 class Point(TypedDict):
682 x: int
683 y: int
684 p = Point(dict(x=42, y=1337))
685 [file next.py]
686 from typing import TypedDict
687 class Point(TypedDict, closed=True):
688 x: int
689 y: int
690 p = Point(dict(x=42, y=1337))
691 [builtins fixtures/dict.pyi]
692 [typing fixtures/typing-typeddict.pyi]
693 [out]
694 __main__.Point
695 __main__.p
696
697 [case testTypeAliasSimple]
698 A = int
699 B = int
700 [file next.py]
701 A = str
702 B = int
703 [out]
704 __main__.A
705
706 [case testTypeAliasGeneric]
707 from typing import List
708 A = List[int]
709 B = List[int]
710 [file next.py]
711 from typing import List
712 A = List[str]
713 B = List[int]
714 [builtins fixtures/list.pyi]
715 [out]
716 __main__.A
717
718 [case testTypeAliasGenToNonGen]
719 from typing import List
720 A = List[str]
721 B = List
722 [file next.py]
723 from typing import List
724 A = List
725 B = List
726 [builtins fixtures/list.pyi]
727 [out]
728 __main__.A
729
730 [case testTypeAliasNonGenToGen]
731 from typing import List
732 A = List
733 B = List
734 [file next.py]
735 from typing import List
736 A = List[str]
737 B = List
738 [builtins fixtures/list.pyi]
739 [out]
740 __main__.A
741
742 [case testTypeAliasGenericTypeVar]
743 from typing import TypeVar, Dict
744 T = TypeVar('T')
745 S = TypeVar('S')
746 A = Dict[str, T]
747 B = Dict[str, S]
748 [file next.py]
749 from typing import TypeVar, Dict
750 class T: pass
751 S = TypeVar('S')
752 A = Dict[str, T]
753 B = Dict[str, S]
754 [builtins fixtures/dict.pyi]
755 [out]
756 __main__.A
757 __main__.T
758
759 [case testNewType]
760 from typing import NewType
761 class C: pass
762 class D: pass
763 N1 = NewType('N1', C)
764 N2 = NewType('N2', D)
765 N3 = NewType('N3', C)
766 class N4(C): pass
767 [file next.py]
768 from typing import NewType
769 class C: pass
770 class D(C): pass
771 N1 = NewType('N1', C)
772 N2 = NewType('N2', D)
773 class N3(C): pass
774 N4 = NewType('N4', C)
775 [out]
776 __main__.D
777 __main__.N2
778 __main__.N3
779 __main__.N3.__init__
780 __main__.N4
781 __main__.N4.__init__
782
783 [case testChangeGenericBaseClassOnly]
784 from typing import List
785 class C(List[int]): pass
786 [file next.py]
787 from typing import List
788 class C(List[str]): pass
789 [builtins fixtures/list.pyi]
790 [out]
791 __main__.C
792
793 [case testOverloads]
794 from typing import overload
795 class C: pass
796 @overload
797 def a(x: int) -> None: pass
798 @overload
799 def a(x: str) -> str: pass
800 def a(x):
801 pass
802 @overload
803 def b(x: int) -> None: pass
804 @overload
805 def b(x: str) -> str: pass
806 def b(x):
807 pass
808 @overload
809 def c(x: int) -> None: pass
810 @overload
811 def c(x: str) -> str: pass
812 def c(x):
813 pass
814 @overload
815 def d(x: int) -> None: pass
816 @overload
817 def d(x: str) -> str: pass
818 def d(x):
819 pass
820 [file next.py]
821 from typing import overload
822 class C: pass
823 @overload
824 def a(x: int) -> None: pass
825 @overload
826 def a(x: str) -> str: pass
827 def a(x):
828 pass
829 @overload
830 def b(x: str) -> str: pass
831 @overload
832 def b(x: int) -> None: pass
833 def b(x):
834 pass
835 @overload
836 def c(x: int) -> None: pass
837 @overload
838 def c(x: str) -> str: pass
839 @overload
840 def c(x: C) -> C: pass
841 def c(x):
842 pass
843 def d(x: int) -> None:
844 pass
845 @overload
846 def e(x: int) -> None: pass
847 @overload
848 def e(x: str) -> str: pass
849 def e(x):
850 pass
851 [out]
852 __main__.b
853 __main__.c
854 __main__.d
855 __main__.e
856
857 [case testOverloadsExternalOnly]
858 from typing import overload
859 class Base: pass
860 class A(Base): pass
861 class B(Base): pass
862 class C(Base): pass
863 @overload
864 def f(x: A) -> A: pass
865 @overload
866 def f(x: B) -> B: pass
867 def f(x: Base) -> Base:
868 pass
869 @overload
870 def g(x: A) -> A: pass
871 @overload
872 def g(x: B) -> B: pass
873 def g(x: Base) -> Base:
874 pass
875 [file next.py]
876 from typing import overload
877 class Base: pass
878 class A(Base): pass
879 class B(Base): pass
880 class C(Base): pass
881 @overload
882 def f(x: A) -> A: pass
883 @overload
884 def f(x: B) -> B: pass
885 def f(x: object) -> object:
886 pass
887 @overload
888 def g(x: A) -> A: pass
889 @overload
890 def g(x: C) -> C: pass
891 def g(x: Base) -> Base:
892 pass
893 [out]
894 __main__.g
895
896 [case testNestedFunctionDoesntMatter]
897 class A: pass
898 class B: pass
899 def outer() -> None:
900 def inner(x: A) -> B:
901 pass
902 [file next.py]
903 class A: pass
904 class B: pass
905 def outer() -> None:
906 def inner(y: B) -> A:
907 pass
908 [out]
909
910 [case testProtocolVsNominal]
911 from typing import Protocol
912 class A(Protocol):
913 x: int
914 class B(Protocol):
915 x: int
916 class C(Protocol):
917 x: int
918 class D(Protocol):
919 x: int
920 [file next.py]
921 from typing import Protocol
922 class A(Protocol):
923 x: int
924 class B(Protocol):
925 x: str
926 class C(Protocol):
927 x: int
928 y: int
929 class D:
930 x: int
931 [out]
932 __main__.B.x
933 __main__.C.(abstract)
934 __main__.C.y
935 __main__.D
936 __main__.D.(abstract)
937
938 [case testProtocolNormalVsGeneric]
939 from typing import Protocol, TypeVar
940 T = TypeVar('T')
941 class P(Protocol[T]):
942 x: T
943 class P2(Protocol[T]):
944 x: T
945 y: T
946 [file next.py]
947 from typing import Protocol, TypeVar
948 T = TypeVar('T')
949 class P(Protocol):
950 x: int
951 class P2(Protocol[T]):
952 x: T
953 [out]
954 __main__.P
955 __main__.P.x
956 __main__.P2.(abstract)
957 __main__.P2.y
958
959 [case testAddAbstractMethod]
960 from abc import abstractmethod
961 class A:
962 @abstractmethod
963 def f(self) -> None: pass
964 [file next.py]
965 from abc import abstractmethod
966 class A:
967 @abstractmethod
968 def f(self) -> None: pass
969 @abstractmethod
970 def g(self) -> None: pass
971 [out]
972 __main__.A.(abstract)
973 __main__.A.g
974
975 [case testFinalFlagsTriggerVar]
976 from typing import Final
977
978 v: Final = 1
979 w: Final = 1
980 x: Final = 1
981 y: Final[int] = 1
982 z: Final[int] = 1
983 same1: Final = 1
984 same2: Final[int] = 1
985 class C:
986 v: Final = 1
987 w: Final = 1
988 x: Final = 1
989 y: Final[int] = 1
990 z: Final[int] = 1
991 same1: Final = 1
992 same2: Final[int] = 1
993 def __init__(self) -> None:
994 self.vi: Final = 1
995 self.wi: Final = 1
996 self.xi: Final = 1
997 self.yi: Final[int] = 1
998 self.zi: Final[int] = 1
999 self.same1_instance: Final = 1
1000 self.same2_instance: Final[int] = 1
1001
1002 [file next.py]
1003 from typing import Final
1004
1005 v: Final = 0
1006 w = 1
1007 x: Final[int] = 1
1008 y: int = 1
1009 z: Final = 1
1010 same1: Final = 1
1011 same2: Final[int] = 0
1012 class C:
1013 v: Final = 0
1014 w = 1
1015 x: Final[int] = 1
1016 y: int = 1
1017 z: Final = 1
1018 same1: Final = 1
1019 same2: Final[int] = 0
1020 def __init__(self) -> None:
1021 self.vi: Final = 0
1022 self.wi = 1
1023 self.xi: Final[int] = 1
1024 self.yi: int = 1
1025 self.zi: Final = 1
1026 self.same1_instance: Final = 1
1027 self.same2_instance: Final[int] = 0
1028 [out]
1029 __main__.C.v
1030 __main__.C.vi
1031 __main__.C.w
1032 __main__.C.wi
1033 __main__.C.x
1034 __main__.C.xi
1035 __main__.C.y
1036 __main__.C.yi
1037 __main__.C.z
1038 __main__.C.zi
1039 __main__.v
1040 __main__.w
1041 __main__.x
1042 __main__.y
1043 __main__.z
1044
1045 [case testFinalFlagsTriggerMethod]
1046 from typing import final
1047
1048 class C:
1049 def meth(self) -> int: pass
1050 @final
1051 def same(self) -> int: pass
1052 @classmethod
1053 def cmeth(cls) -> int: pass
1054
1055 [file next.py]
1056 from typing import final
1057
1058 class C:
1059 @final
1060 def meth(self) -> int: pass
1061 @final
1062 def same(self) -> int: pass
1063 @final
1064 @classmethod
1065 def cmeth(cls) -> int: pass
1066 [builtins fixtures/classmethod.pyi]
1067 [out]
1068 __main__.C.cmeth
1069 __main__.C.meth
1070
1071 [case testFinalFlagsTriggerProperty]
1072 from typing import final
1073
1074 class C:
1075 @final
1076 @property
1077 def p(self) -> int: pass
1078 @final
1079 @property
1080 def same(self) -> str: pass
1081
1082 [file next.py]
1083 from typing import final
1084
1085 class C:
1086 @property
1087 def p(self) -> int: pass
1088 @final
1089 @property
1090 def same(self) -> str: pass
1091 [builtins fixtures/property.pyi]
1092 [out]
1093 __main__.C.p
1094
1095 [case testFinalFlagsTriggerMethodOverload]
1096 from typing import final, overload
1097
1098 class C:
1099 @overload
1100 def m(self, x: int) -> int: ...
1101 @overload
1102 def m(self, x: str) -> str: ...
1103 @final
1104 def m(self, x):
1105 pass
1106 @overload
1107 def same(self, x: int) -> int: ...
1108 @overload
1109 def same(self, x: str) -> str: ...
1110 @final
1111 def same(self, x):
1112 pass
1113
1114 [file next.py]
1115 from typing import final, overload
1116
1117 class C:
1118 @overload
1119 def m(self, x: int) -> int: ...
1120 @overload
1121 def m(self, x: str) -> str: ...
1122 def m(self, x):
1123 pass
1124 @overload
1125 def same(self, x: int) -> int: ...
1126 @overload
1127 def same(self, x: str) -> str: ...
1128 @final
1129 def same(self, x):
1130 pass
1131 [out]
1132 __main__.C.m
1133
1134 [case testDynamicBasePluginDiff]
1135 # flags: --config-file tmp/mypy.ini
1136 from mod import declarative_base, Column, Instr
1137
1138 Base = declarative_base()
1139
1140 class Model(Base):
1141 x: Column[int]
1142 class Other:
1143 x: Column[int]
1144 class Diff:
1145 x: Column[int]
1146 [file next.py]
1147 from mod import declarative_base, Column, Instr
1148
1149 Base = declarative_base()
1150
1151 class Model(Base):
1152 x: Column[int]
1153 class Other:
1154 x: Column[int]
1155 class Diff(Base):
1156 x: Column[int]
1157 [file mod.py]
1158 from typing import Generic, TypeVar
1159 def declarative_base(): ...
1160
1161 T = TypeVar('T')
1162
1163 class Column(Generic[T]): ...
1164 class Instr(Generic[T]): ...
1165
1166 [file mypy.ini]
1167 \[mypy]
1168 plugins=<ROOT>/test-data/unit/plugins/dyn_class.py
1169 [out]
1170 __main__.Diff
1171 __main__.Diff.x
1172
1173 [case testLiteralTriggersVar]
1174 from typing import Literal
1175
1176 x: Literal[1] = 1
1177 y = 1
1178 z: Literal[1] = 1
1179 same: Literal[1] = 1
1180 class C:
1181 x_class: Literal[1] = 1
1182 y_class = 1
1183 z_class: Literal[1] = 1
1184 same_class: Literal[1] = 1
1185 def __init__(self) -> None:
1186 self.x_instance: Literal[1] = 1
1187 self.y_instance = 1
1188 self.z_instance: Literal[1] = 1
1189 self.same_instance: Literal[1] = 1
1190
1191 [file next.py]
1192 from typing import Literal
1193
1194 x = 1
1195 y: Literal[1] = 1
1196 z: Literal[2] = 2
1197 same: Literal[1] = 1
1198 class C:
1199 x_class = 1
1200 y_class: Literal[1] = 1
1201 z_class: Literal[2] = 2
1202 same_class: Literal[1] = 1
1203 def __init__(self) -> None:
1204 self.x_instance = 1
1205 self.y_instance: Literal[1] = 1
1206 self.z_instance: Literal[2] = 2
1207 self.same_instance: Literal[1] = 1
1208 [builtins fixtures/tuple.pyi]
1209 [out]
1210 __main__.C.x_class
1211 __main__.C.x_instance
1212 __main__.C.y_class
1213 __main__.C.y_instance
1214 __main__.C.z_class
1215 __main__.C.z_instance
1216 __main__.x
1217 __main__.y
1218 __main__.z
1219
1220 [case testLiteralTriggersFunctions]
1221 from typing import Literal
1222
1223 def function_1() -> int: pass
1224 def function_2() -> Literal[1]: pass
1225 def function_3() -> Literal[1]: pass
1226
1227 def function_4(x: int) -> None: pass
1228 def function_5(x: Literal[1]) -> None: pass
1229 def function_6(x: Literal[1]) -> None: pass
1230
1231 def function_same_1() -> Literal[1]: pass
1232 def function_same_2(x: Literal[1]) -> None: pass
1233
1234 class C:
1235 def method_1(self) -> int: pass
1236 def method_2(self) -> Literal[1]: pass
1237 def method_3(self) -> Literal[1]: pass
1238
1239 def method_4(self, x: int) -> None: pass
1240 def method_5(self, x: Literal[1]) -> None: pass
1241 def method_6(self, x: Literal[1]) -> None: pass
1242
1243 def method_same_1(self) -> Literal[1]: pass
1244 def method_same_2(self, x: Literal[1]) -> None: pass
1245
1246 @classmethod
1247 def classmethod_1(cls) -> int: pass
1248 @classmethod
1249 def classmethod_2(cls) -> Literal[1]: pass
1250 @classmethod
1251 def classmethod_3(cls) -> Literal[1]: pass
1252
1253 @classmethod
1254 def classmethod_4(cls, x: int) -> None: pass
1255 @classmethod
1256 def classmethod_5(cls, x: Literal[1]) -> None: pass
1257 @classmethod
1258 def classmethod_6(cls, x: Literal[1]) -> None: pass
1259
1260 @classmethod
1261 def classmethod_same_1(cls) -> Literal[1]: pass
1262 @classmethod
1263 def classmethod_same_2(cls, x: Literal[1]) -> None: pass
1264
1265 @staticmethod
1266 def staticmethod_1() -> int: pass
1267 @staticmethod
1268 def staticmethod_2() -> Literal[1]: pass
1269 @staticmethod
1270 def staticmethod_3() -> Literal[1]: pass
1271
1272 @staticmethod
1273 def staticmethod_4(x: int) -> None: pass
1274 @staticmethod
1275 def staticmethod_5(x: Literal[1]) -> None: pass
1276 @staticmethod
1277 def staticmethod_6(x: Literal[1]) -> None: pass
1278
1279 @staticmethod
1280 def staticmethod_same_1() -> Literal[1]: pass
1281 @staticmethod
1282 def staticmethod_same_2(x: Literal[1]) -> None: pass
1283
1284 [file next.py]
1285 from typing import Literal
1286
1287 def function_1() -> Literal[1]: pass
1288 def function_2() -> int: pass
1289 def function_3() -> Literal[2]: pass
1290
1291 def function_4(x: Literal[1]) -> None: pass
1292 def function_5(x: int) -> None: pass
1293 def function_6(x: Literal[2]) -> None: pass
1294
1295 def function_same_1() -> Literal[1]: pass
1296 def function_same_2(x: Literal[1]) -> None: pass
1297
1298 class C:
1299 def method_1(self) -> Literal[1]: pass
1300 def method_2(self) -> int: pass
1301 def method_3(self) -> Literal[2]: pass
1302
1303 def method_4(self, x: Literal[1]) -> None: pass
1304 def method_5(self, x: int) -> None: pass
1305 def method_6(self, x: Literal[2]) -> None: pass
1306
1307 def method_same_1(self) -> Literal[1]: pass
1308 def method_same_2(self, x: Literal[1]) -> None: pass
1309
1310 @classmethod
1311 def classmethod_1(cls) -> Literal[1]: pass
1312 @classmethod
1313 def classmethod_2(cls) -> int: pass
1314 @classmethod
1315 def classmethod_3(cls) -> Literal[2]: pass
1316
1317 @classmethod
1318 def classmethod_4(cls, x: Literal[1]) -> None: pass
1319 @classmethod
1320 def classmethod_5(cls, x: int) -> None: pass
1321 @classmethod
1322 def classmethod_6(cls, x: Literal[2]) -> None: pass
1323
1324 @classmethod
1325 def classmethod_same_1(cls) -> Literal[1]: pass
1326 @classmethod
1327 def classmethod_same_2(cls, x: Literal[1]) -> None: pass
1328
1329 @staticmethod
1330 def staticmethod_1() -> Literal[1]: pass
1331 @staticmethod
1332 def staticmethod_2() -> int: pass
1333 @staticmethod
1334 def staticmethod_3() -> Literal[2]: pass
1335
1336 @staticmethod
1337 def staticmethod_4(x: Literal[1]) -> None: pass
1338 @staticmethod
1339 def staticmethod_5(x: int) -> None: pass
1340 @staticmethod
1341 def staticmethod_6(x: Literal[2]) -> None: pass
1342
1343 @staticmethod
1344 def staticmethod_same_1() -> Literal[1]: pass
1345 @staticmethod
1346 def staticmethod_same_2(x: Literal[1]) -> None: pass
1347 [builtins fixtures/classmethod.pyi]
1348 [out]
1349 __main__.C.classmethod_1
1350 __main__.C.classmethod_2
1351 __main__.C.classmethod_3
1352 __main__.C.classmethod_4
1353 __main__.C.classmethod_5
1354 __main__.C.classmethod_6
1355 __main__.C.method_1
1356 __main__.C.method_2
1357 __main__.C.method_3
1358 __main__.C.method_4
1359 __main__.C.method_5
1360 __main__.C.method_6
1361 __main__.C.staticmethod_1
1362 __main__.C.staticmethod_2
1363 __main__.C.staticmethod_3
1364 __main__.C.staticmethod_4
1365 __main__.C.staticmethod_5
1366 __main__.C.staticmethod_6
1367 __main__.function_1
1368 __main__.function_2
1369 __main__.function_3
1370 __main__.function_4
1371 __main__.function_5
1372 __main__.function_6
1373
1374 [case testLiteralTriggersProperty]
1375 from typing import Literal
1376
1377 class C:
1378 @property
1379 def p1(self) -> Literal[1]: pass
1380
1381 @property
1382 def p2(self) -> int: pass
1383
1384 @property
1385 def same(self) -> Literal[1]: pass
1386
1387 [file next.py]
1388 from typing import Literal
1389
1390 class C:
1391 @property
1392 def p1(self) -> int: pass
1393
1394 @property
1395 def p2(self) -> Literal[1]: pass
1396
1397 @property
1398 def same(self) -> Literal[1]: pass
1399 [builtins fixtures/property.pyi]
1400 [out]
1401 __main__.C.p1
1402 __main__.C.p2
1403
1404 [case testLiteralsTriggersOverload]
1405 from typing import Literal, overload
1406
1407 @overload
1408 def func(x: str) -> str: ...
1409 @overload
1410 def func(x: Literal[1]) -> int: ...
1411 def func(x):
1412 pass
1413
1414 @overload
1415 def func_same(x: str) -> str: ...
1416 @overload
1417 def func_same(x: Literal[1]) -> int: ...
1418 def func_same(x):
1419 pass
1420
1421 class C:
1422 @overload
1423 def method(self, x: str) -> str: ...
1424 @overload
1425 def method(self, x: Literal[1]) -> int: ...
1426 def method(self, x):
1427 pass
1428
1429 @overload
1430 def method_same(self, x: str) -> str: ...
1431 @overload
1432 def method_same(self, x: Literal[1]) -> int: ...
1433 def method_same(self, x):
1434 pass
1435
1436 [file next.py]
1437 from typing import Literal, overload
1438
1439 @overload
1440 def func(x: str) -> str: ...
1441 @overload
1442 def func(x: Literal[2]) -> int: ...
1443 def func(x):
1444 pass
1445
1446 @overload
1447 def func_same(x: str) -> str: ...
1448 @overload
1449 def func_same(x: Literal[1]) -> int: ...
1450 def func_same(x):
1451 pass
1452
1453 class C:
1454 @overload
1455 def method(self, x: str) -> str: ...
1456 @overload
1457 def method(self, x: Literal[2]) -> int: ...
1458 def method(self, x):
1459 pass
1460
1461 @overload
1462 def method_same(self, x: str) -> str: ...
1463 @overload
1464 def method_same(self, x: Literal[1]) -> int: ...
1465 def method_same(self, x):
1466 pass
1467 [builtins fixtures/tuple.pyi]
1468 [out]
1469 __main__.C.method
1470 __main__.func
1471
1472 [case testUnionOfLiterals]
1473 from typing import Literal
1474 x: Literal[1, '2']
1475 [file next.py]
1476 from typing import Literal
1477 x: Literal[1, 2]
1478 [builtins fixtures/tuple.pyi]
1479 [out]
1480 __main__.x
1481
1482 [case testUnionOfCallables]
1483 from typing import Callable, Union
1484 from mypy_extensions import Arg
1485 x: Union[Callable[[Arg(int, 'x')], None],
1486 Callable[[int], None]]
1487 [file next.py]
1488 from typing import Callable, Union
1489 from mypy_extensions import Arg
1490 x: Union[Callable[[Arg(int, 'y')], None],
1491 Callable[[int], None]]
1492 [builtins fixtures/tuple.pyi]
1493 [out]
1494 __main__.x
1495
1496 [case testChangeParamSpec]
1497 from typing import ParamSpec, TypeVar
1498 A = ParamSpec('A')
1499 B = ParamSpec('B')
1500 C = TypeVar('C')
1501 [file next.py]
1502 from typing import ParamSpec, TypeVar
1503 A = ParamSpec('A')
1504 B = TypeVar('B')
1505 C = ParamSpec('C')
1506 [out]
1507 __main__.B
1508 __main__.C
1509
1510 [case testEmptyBodySuper]
1511 from abc import abstractmethod
1512 class C:
1513 @abstractmethod
1514 def meth(self) -> int: ...
1515 [file next.py]
1516 from abc import abstractmethod
1517 class C:
1518 @abstractmethod
1519 def meth(self) -> int: return 0
1520 [out]
1521 __main__.C.meth
1522
1523 [case testGenericFunctionWithOptionalReturnType]
1524 from typing import Type, TypeVar
1525
1526 T = TypeVar("T")
1527
1528 class C:
1529 @classmethod
1530 def get_by_team_and_id(
1531 cls: Type[T],
1532 raw_member_id: int,
1533 include_removed: bool = False,
1534 ) -> T:
1535 pass
1536
1537 [file next.py]
1538 from typing import Type, TypeVar, Optional
1539
1540 T = TypeVar("T")
1541
1542 class C:
1543 @classmethod
1544 def get_by_team_and_id(
1545 cls: Type[T],
1546 raw_member_id: int,
1547 include_removed: bool = False,
1548 ) -> Optional[T]:
1549 pass
1550
1551 [builtins fixtures/classmethod.pyi]
1552 [out]
1553 __main__.C.get_by_team_and_id
1554 __main__.Optional
1555
1556 [case testPEP695TypeAlias]
1557 # flags: --python-version=3.12
1558 from typing_extensions import TypeAlias, TypeAliasType
1559 type A = int
1560 type B = str
1561 type C = int
1562 D = int
1563 E: TypeAlias = int
1564 F = TypeAliasType("F", int)
1565 G = TypeAliasType("G", int)
1566 type H = int
1567
1568 [file next.py]
1569 # flags: --python-version=3.12
1570 from typing_extensions import TypeAlias, TypeAliasType
1571 type A = str
1572 type B = str
1573 type C[T] = int
1574 type D = int
1575 type E = int
1576 type F = int
1577 type G = str
1578 type H[T] = int
1579
1580 [builtins fixtures/tuple.pyi]
1581 [typing fixtures/typing-full.pyi]
1582 [out]
1583 __main__.A
1584 __main__.C
1585 __main__.D
1586 __main__.E
1587 __main__.G
1588 __main__.H
1589
1590 [case testPEP695TypeAlias2]
1591 # flags: --python-version=3.12
1592 type A[T: int] = list[T]
1593 type B[T: int] = list[T]
1594 type C[T: (int, str)] = list[T]
1595 type D[T: (int, str)] = list[T]
1596 type E[T: int] = list[T]
1597 type F[T: (int, str)] = list[T]
1598
1599 [file next.py]
1600 # flags: --python-version=3.12
1601 type A[T] = list[T]
1602 type B[T: str] = list[T]
1603 type C[T: (int, None)] = list[T]
1604 type D[T] = list[T]
1605 type E[T: int] = list[T]
1606 type F[T: (int, str)] = list[T]
1607
1608 [out]
1609 __main__.A
1610 __main__.B
1611 __main__.C
1612 __main__.D
1613
1614 [case testPEP695GenericFunction]
1615 # flags: --python-version=3.12
1616 def f[T](x: T) -> T:
1617 return x
1618 def g[T](x: T, y: T) -> T:
1619 return x
1620 [file next.py]
1621 # flags: --python-version=3.12
1622 def f[T](x: T) -> T:
1623 return x
1624 def g[T, S](x: T, y: S) -> S:
1625 return y
1626 [out]
1627 __main__.g
1628
1629 [case testPEP695GenericClass]
1630 # flags: --python-version=3.12
1631 class C[T]:
1632 pass
1633 class D[T]:
1634 pass
1635 class E[T]:
1636 pass
1637 class F[T]:
1638 def f(self, x: object) -> T: ...
1639 [file next.py]
1640 # flags: --python-version=3.12
1641 class C[T]:
1642 pass
1643 class D[T: int]:
1644 pass
1645 class E:
1646 pass
1647 class F[T]:
1648 def f(self, x: T) -> T: ...
1649 [out]
1650 __main__.D
1651 __main__.E
1652 __main__.F
1653 __main__.F.f