Debian Codegraph @

test-data/unit/deps.test

raw
1 -- Test cases for generating dependencies between ASTs nodes.
2 --
3 -- The dependencies are used for fined-grained incremental checking and
4 -- the daemon mode.
5 --
6 -- The output of each test case includes the dependency map for whitelisted
7 -- modules (includes the main module and the modules 'pkg' and 'pkg.mod' at
8 -- least).
9 --
10 -- Dependencies are formatted as "<trigger> -> affected locations".
11 --
12 -- Look at the docstring of mypy.server.deps for an explanation of
13 -- how fine-grained dependencies are represented.
14
15 [case testCallFunction]
16 def f() -> None:
17 g()
18 def g() -> None:
19 pass
20 [out]
21 <m.g> -> m.f
22
23 [case testCallMethod]
24 def f(a: A) -> None:
25 a.g()
26 class A:
27 def g(self) -> None: pass
28 [out]
29 <m.A.g> -> m.f
30 <m.A> -> <m.f>, m.A, m.f
31
32 [case testAccessAttribute]
33 def f(a: A) -> None:
34 a.x
35 class A:
36 def g(self) -> None:
37 self.x = 1
38 [out]
39 <m.A.x> -> m.A.g, m.f
40 <m.A> -> <m.f>, m.A, m.f
41
42 [case testConstructInstance]
43 def f() -> None:
44 A()
45 class A: pass
46 [out]
47 <m.A.__init__> -> m.f
48 <m.A.__new__> -> m.f
49 <m.A> -> m.A, m.f
50
51 [case testAccessModuleAttribute]
52 class A: pass
53 x = A()
54 def f() -> None:
55 x
56 [out]
57 <m.A.__init__> -> m
58 <m.A.__new__> -> m
59 <m.A> -> <m.x>, m, m.A
60 <m.x> -> m, m.f
61
62 [case testAccessModuleAttribute2]
63 import n
64 def f() -> None:
65 n.x
66 [file n.py]
67 x = 1
68 [out]
69 <n.x> -> m.f
70 <n> -> m, m.f
71
72 [case testAccessReexportedModuleAttribute]
73 import pkg
74 pkg.f()
75 [file pkg/__init__.py]
76 from pkg.sub import *
77 [file pkg/sub.py]
78 def f() -> None: pass
79 [out]
80 <pkg.f> -> m
81 <pkg.sub.f> -> m
82 <pkg.sub[wildcard]> -> pkg
83 <pkg> -> m
84
85 [case testImport]
86 import n
87 [file n.py]
88 x = 1
89 [out]
90 <n> -> m
91
92 [case testCallImportedFunction]
93 import n
94 n.f()
95 [file n.py]
96 def f() -> None: pass
97 [out]
98 <n.f> -> m
99 <n> -> m
100
101 [case testImportModuleAs]
102 import n as x
103 x.f()
104 [file n.py]
105 def f() -> None: pass
106 [out]
107 <n.f> -> m
108 <n> -> m
109
110 [case testCallImportedFunctionInFunction]
111 import n
112 def g() -> None:
113 n.f()
114 [file n.py]
115 def f() -> None: pass
116 [out]
117 <n.f> -> m.g
118 <n> -> m, m.g
119
120 [case testInheritanceSimple]
121 class A:
122 pass
123 class B(A):
124 pass
125 [out]
126 <m.A.(abstract)> -> <m.B.__init__>, m
127 <m.A.__init__> -> <m.B.__init__>
128 <m.A.__new__> -> <m.B.__new__>
129 <m.A> -> m, m.A, m.B
130 <m.B> -> m.B
131
132 [case testInheritanceWithMethodAndAttribute]
133 class A:
134 pass
135 class B(A):
136 def f(self) -> None:
137 self.x = 1
138 [out]
139 <m.A.(abstract)> -> <m.B.__init__>, m
140 <m.A.__init__> -> <m.B.__init__>
141 <m.A.__new__> -> <m.B.__new__>
142 <m.A.f> -> m.B.f
143 <m.A.x> -> <m.B.x>
144 <m.A> -> m, m.A, m.B
145 <m.B.x> -> m.B.f
146 <m.B> -> m.B
147
148 [case testInheritanceWithMethodAndAttributeAndDeepHierarchy]
149 class A:
150 pass
151 class B(A):
152 pass
153 class C(B):
154 def f(self) -> None:
155 self.x = 1
156 [out]
157 <m.A.(abstract)> -> <m.B.__init__>, <m.C.__init__>, m
158 <m.A.__init__> -> <m.B.__init__>, <m.C.__init__>
159 <m.A.__new__> -> <m.B.__new__>, <m.C.__new__>
160 <m.A.f> -> m.C.f
161 <m.A.x> -> <m.C.x>
162 <m.A> -> m, m.A, m.B
163 <m.B.(abstract)> -> <m.C.__init__>, m
164 <m.B.__init__> -> <m.C.__init__>
165 <m.B.__new__> -> <m.C.__new__>
166 <m.B.f> -> m.C.f
167 <m.B.x> -> <m.C.x>
168 <m.B> -> m, m.B, m.C
169 <m.C.x> -> m.C.f
170 <m.C> -> m.C
171
172 [case testInheritAttribute]
173 import n
174 class B(n.A):
175 def f(self) -> None:
176 a = 1
177 a = self.x
178 [file n.py]
179 class A:
180 def g(self) -> None:
181 self.x = 1
182 [out]
183 <m.B.x> -> m.B.f
184 <m.B> -> m.B
185 <n.A.(abstract)> -> <m.B.__init__>, m
186 <n.A.__init__> -> <m.B.__init__>
187 <n.A.__new__> -> <m.B.__new__>
188 <n.A.f> -> m.B.f
189 <n.A.g> -> <m.B.g>
190 <n.A.x> -> <m.B.x>
191 <n.A> -> m, m.B
192 <n> -> m
193
194 [case testInheritMethod]
195 class A:
196 def g(self) -> None: pass
197 class B(A):
198 def f(self) -> None:
199 self.g()
200 [out]
201 <m.A.(abstract)> -> <m.B.__init__>, m
202 <m.A.__init__> -> <m.B.__init__>
203 <m.A.__new__> -> <m.B.__new__>
204 <m.A.f> -> m.B.f
205 <m.A.g> -> <m.B.g>
206 <m.A> -> m, m.A, m.B
207 <m.B.g> -> m.B.f
208 <m.B> -> m.B
209
210 [case testPackage]
211 import a.b
212 def f() -> None:
213 a.b.g()
214 [file a/__init__.py]
215 [file a/b.py]
216 def g() -> None: pass
217 [out]
218 <a.b.g> -> m.f
219 <a.b> -> m, m.f
220 <a> -> m.f
221
222 [case testClassInPackage]
223 import a.b
224 def f(x: a.b.A) -> None:
225 x.g()
226 x.y
227 [file a/__init__.py]
228 [file a/b.py]
229 class A:
230 def g(self) -> None:
231 self.y = 1
232 [out]
233 <a.b.A.g> -> m.f
234 <a.b.A.y> -> m.f
235 <a.b.A> -> <m.f>, m.f
236 <a.b> -> m
237
238 [case testPackage__init__]
239 import a
240 def f() -> None:
241 a.g()
242 [file a/__init__.py]
243 def g() -> None: pass
244 [out]
245 <a.g> -> m.f
246 <a> -> m, m.f
247
248 [case testClassInPackage__init__]
249 import a
250 def f(x: a.A) -> None:
251 x.g()
252 x.y
253 [file a/__init__.py]
254 class A:
255 def g(self) -> None:
256 self.y = 1
257 [out]
258 <a.A.g> -> m.f
259 <a.A.y> -> m.f
260 <a.A> -> <m.f>, m.f
261 <a> -> m
262
263 [case testConstructor]
264 class A:
265 def __init__(self, x: C) -> None: pass
266 class C: pass
267 def f() -> None:
268 A(C())
269 [out]
270 <m.A.__init__> -> m.f
271 <m.A.__new__> -> m.f
272 <m.A> -> m.A, m.f
273 <m.C.__init__> -> m.f
274 <m.C.__new__> -> m.f
275 <m.C> -> <m.A.__init__>, m.A.__init__, m.C, m.f
276
277 [case testNonTrivialConstructor]
278 class C:
279 def __init__(self) -> None:
280 self.x = 1
281 [out]
282 <m.C.x> -> m.C.__init__
283 <m.C> -> m.C
284
285 [case testImportFrom]
286 from n import f
287
288 def g() -> None:
289 f()
290 [file n.py]
291 def f() -> None: pass
292 [out]
293 <n.f> -> m, m.g
294 <n> -> m
295
296 [case testImportFromAs]
297 from n import f as ff
298
299 def g() -> None:
300 ff()
301 [file n.py]
302 def f() -> None: pass
303 [out]
304 <n.f> -> m, m.g
305 <n> -> m
306
307 [case testNestedClass]
308 def f() -> None:
309 b = A.B()
310 b.f()
311 class A:
312 class B:
313 def f(self) -> None: pass
314 [out]
315 <m.A.B.__init__> -> m.f
316 <m.A.B.__new__> -> m.f
317 <m.A.B.f> -> m.f
318 <m.A.B> -> m.A.B, m.f
319 <m.A> -> m.A, m.f
320
321 [case testNestedClassAttribute]
322 def f() -> None:
323 b = A.B()
324 b.x
325 class A:
326 class B:
327 def f(self) -> None:
328 self.x = 1
329 [out]
330 <m.A.B.__init__> -> m.f
331 <m.A.B.__new__> -> m.f
332 <m.A.B.x> -> m.A.B.f, m.f
333 <m.A.B> -> m.A.B, m.f
334 <m.A> -> m.A, m.f
335
336 [case testNestedClassInAnnotation]
337 def f(x: A.B) -> None:
338 pass
339 class A:
340 class B: pass
341 [out]
342 <m.A.B> -> <m.f>, m.A.B, m.f
343 <m.A> -> m.A
344
345 [case testNestedClassInAnnotation2]
346 def f(x: A.B) -> None:
347 x.f()
348 class A:
349 class B:
350 def f(self) -> None: pass
351 [out]
352 <m.A.B.f> -> m.f
353 <m.A.B> -> <m.f>, m.A.B, m.f
354 <m.A> -> m.A
355
356 [case NestedFunctionType]
357 from mod import A, B, C, D
358 def outer() -> None:
359 def inner(x: A, *args: B, **kwds: C) -> D:
360 pass
361 [file mod.py]
362 class A: pass
363 class B: pass
364 class C: pass
365 class D: pass
366 [builtins fixtures/dict.pyi]
367 [out]
368 <mod.A> -> <m.outer>, m, m.outer
369 <mod.B> -> <m.outer>, m, m.outer
370 <mod.C> -> <m.outer>, m, m.outer
371 <mod.D> -> <m.outer>, m, m.outer
372 <mod> -> m
373
374 [case NestedFunctionBody]
375 from mod import A, B, C
376 def outer() -> None:
377 def inner() -> None:
378 A()
379 x: B
380 y: C
381 y.x
382 [file mod.py]
383 class A: pass
384 class B: pass
385 class C:
386 x: int
387 [builtins fixtures/dict.pyi]
388 [out]
389 <mod.A.__init__> -> m.outer
390 <mod.A.__new__> -> m.outer
391 <mod.A> -> m, m.outer
392 <mod.B> -> m, m.outer
393 <mod.C.x> -> m.outer
394 <mod.C> -> m, m.outer
395 <mod> -> m
396
397 [case testDefaultArgValue]
398 def f1(x: int) -> int: pass
399 def f2() -> int: pass
400 def g(x: int = f1(f2())) -> None: pass
401 [out]
402 <m.f1> -> m.g
403 <m.f2> -> m.g
404
405 [case testIsInstance]
406 class A:
407 def g(self) -> None: pass
408
409 def f(x: object) -> None:
410 if isinstance(x, A):
411 x.g()
412 def ff(x: object) -> None:
413 if isinstance(x, A):
414 pass
415 [builtins fixtures/isinstancelist.pyi]
416 [out]
417 -- The dependencies on the ctor are basically spurious but not a problem
418 <m.A.g> -> m.f
419 <m.A> -> m.A, m.f, m.ff
420
421 [case testUnreachableIsInstance]
422 class A:
423 x: int
424
425 class B:
426 x: str
427
428 def f(x: A) -> None:
429 if isinstance(x, B):
430 x.y
431 [builtins fixtures/isinstancelist.pyi]
432 [out]
433 <m.A> -> <m.f>, m.A, m.f
434 <m.B> -> m.B, m.f
435
436 [case testIsInstanceAdHocIntersectionDeps]
437 class A:
438 x: int
439
440 class B:
441 y: int
442
443 def f(x: A) -> None:
444 if isinstance(x, B):
445 x.y
446 [builtins fixtures/isinstancelist.pyi]
447 [out]
448 <m.<subclass of "m.A" and "m.B">.y> -> m.f
449 <m.A> -> <m.f>, m.A, m.f
450 <m.B> -> m.B, m.f
451
452 [case testAttributeWithClassType1]
453 from n import A
454
455 class B:
456 def h(self, z: A) -> None:
457 self.z = z
458 [file n.py]
459 class A: pass
460 [out]
461 <m.B.z> -> m.B.h
462 <m.B> -> m.B
463 <n.A> -> <m.B.h>, <m.B.z>, m, m.B.h
464 <n> -> m
465
466 [case testAttributeWithClassType2]
467 from m import A
468
469 class B:
470 def f(self) -> None:
471 self.x = A()
472 [file m.py]
473 class A: pass
474 [out]
475 <m.B.x> -> m.B.f
476 <m.B> -> m.B
477 <m.A.__init__> -> m.B.f
478 <m.A.__new__> -> m.B.f
479 <m.A> -> <m.B.x>, m, m.B.f
480 <m> -> m
481
482 [case testAttributeWithClassType3]
483 from n import A, x
484
485 class B:
486 def g(self) -> None:
487 self.x = x
488 [file n.py]
489 class A: pass
490
491 x = A()
492 [out]
493 <m.B.x> -> m.B.g
494 <m.B> -> m.B
495 <n.A> -> <m.B.x>, m
496 <n.x> -> m, m.B.g
497 <n> -> m
498
499 [case testAttributeWithClassType4]
500 from n import A
501
502 class B:
503 def g(self) -> None:
504 self.x: A
505 [file n.py]
506 class A: pass
507 [out]
508 <m.B.x> -> m.B.g
509 <m.B> -> m.B
510 <n.A> -> <m.B.x>, m, m.B.g
511 <n> -> m
512
513 [case testClassBody]
514 def f() -> int: pass
515 def g() -> int: pass
516 def h() -> int: pass
517
518 class A:
519 h()
520 if f():
521 g()
522 [out]
523 <m.A> -> m.A
524 <m.f> -> m
525 <m.g> -> m
526 <m.h> -> m
527
528 [case testVariableInitializedInClass]
529 from n import A
530
531 class B:
532 x = None # type: A # type: ignore
533 [file n.py]
534 class A: pass
535 [out]
536 <m.B> -> m.B
537 <n.A> -> <m.B.x>, m
538 <n> -> m
539
540 [case testVariableAnnotationInClass]
541 from n import A
542
543 class B:
544 x: A
545
546 def f(self) -> None:
547 y = self.x
548 [file n.py]
549 class A: pass
550 [out]
551 <m.B.x> -> m.B.f
552 <m.B> -> m.B
553 <n.A> -> <m.B.x>, m
554 <n> -> m
555
556 [case testGlobalVariableInitialized]
557 from n import A
558
559 x = A()
560 [file n.py]
561 class A: pass
562 [out]
563 <m.x> -> m
564 <n.A.__init__> -> m
565 <n.A.__new__> -> m
566 <n.A> -> <m.x>, m
567 <n> -> m
568
569 [case testGlobalVariableAnnotation]
570 from n import A
571
572 x: A
573 [file n.py]
574 class A: pass
575 [out]
576 <m.x> -> m
577 <n.A> -> <m.x>, m
578 <n> -> m
579
580 [case testProperty]
581 class B: pass
582
583 class A:
584 @property
585 def x(self) -> B: pass
586
587 def f(a: A) -> None:
588 b = a.x
589 [builtins fixtures/property.pyi]
590 [out]
591 <m.A.x> -> m, m.f
592 <m.A> -> <m.f>, m.A, m.f
593 <m.B> -> <m.A.x>, m.A.x, m.B
594
595 [case testUnreachableAssignment]
596 from typing import List, Tuple
597
598 def f() -> None: pass
599
600 class C:
601 def __init__(self, x: int) -> None:
602 if isinstance(x, int):
603 self.y = 1
604 else:
605 self.y = f()
606 [builtins fixtures/isinstancelist.pyi]
607 [out]
608 <m.C.y> -> m.C.__init__
609 <m.C> -> m.C
610 <m.f> -> m.C.__init__
611
612 [case testPartialNoneTypeAttributeCrash1]
613 # flags: --no-local-partial-types
614 class C: pass
615
616 class A:
617 x = None
618
619 def f(self) -> None:
620 self.x = C()
621 [out]
622 <m.A.x> -> m.A.f
623 <m.A> -> m.A
624 <m.C.__init__> -> m.A.f
625 <m.C.__new__> -> m.A.f
626 <m.C> -> <m.A.x>, m.A.f, m.C
627
628 [case testPartialNoneTypeAttributeCrash2]
629 # flags: --no-local-partial-types
630 class C: pass
631
632 class A:
633 x = None
634
635 def f(self) -> None:
636 self.x = C()
637 [out]
638 <m.A.x> -> m.A.f
639 <m.A> -> m.A
640 <m.C.__init__> -> m.A.f
641 <m.C.__new__> -> m.A.f
642 <m.C> -> <m.A.x>, m.A.f, m.C
643
644 [case testRelativeImport]
645 import pkg # Magic package name in test runner
646 [file pkg/__init__.py]
647 from . import mod
648 from .a import x
649 [file pkg/mod.py]
650 from . import a
651 [file pkg/a.py]
652 x = 1
653 [out]
654 <pkg.a.x> -> pkg
655 <pkg.a> -> pkg, pkg.mod
656 <pkg.mod> -> pkg
657 <pkg> -> m, pkg, pkg.mod
658
659 [case testTypedDict]
660 from typing import TypedDict
661 Point = TypedDict('Point', {'x': int, 'y': int})
662 p = Point(dict(x=42, y=1337))
663 def foo(x: Point) -> int:
664 return x['x'] + x['y']
665 [builtins fixtures/dict.pyi]
666 [typing fixtures/typing-typeddict.pyi]
667 [out]
668 <m.Point.__init__> -> m
669 <m.Point.__new__> -> m
670 <m.Point> -> <m.foo>, <m.p>, m, m.foo
671 <m.p> -> m
672
673 [case testTypedDict2]
674 from typing import TypedDict
675 class A: pass
676 Point = TypedDict('Point', {'x': int, 'y': A})
677 p = Point(dict(x=42, y=A()))
678 def foo(x: Point) -> int:
679 return x['x']
680 [builtins fixtures/dict.pyi]
681 [typing fixtures/typing-typeddict.pyi]
682 [out]
683 <m.A.__init__> -> m
684 <m.A.__new__> -> m
685 <m.A> -> <m.Point>, <m.foo>, <m.p>, m, m.A, m.foo
686 <m.Point.__init__> -> m
687 <m.Point.__new__> -> m
688 <m.Point> -> <m.foo>, <m.p>, m, m.foo
689 <m.p> -> m
690
691 [case testTypedDict3]
692 from typing import TypedDict
693 class A: pass
694 class Point(TypedDict):
695 x: int
696 y: A
697 p = Point(dict(x=42, y=A()))
698 def foo(x: Point) -> int:
699 return x['x']
700 [builtins fixtures/dict.pyi]
701 [typing fixtures/typing-typeddict.pyi]
702 [out]
703 <m.A.__init__> -> m
704 <m.A.__new__> -> m
705 <m.A> -> <m.Point>, <m.foo>, <m.p>, m, m.A, m.foo
706 <m.Point.__init__> -> m
707 <m.Point.__new__> -> m
708 <m.Point> -> <m.foo>, <m.p>, m, m.Point, m.foo
709 <m.p> -> m
710
711 [case testImportStar]
712 from a import *
713 [file a.py]
714 x = 0
715 [out]
716 <a[wildcard]> -> m
717
718 [case testDecoratorDepsMod]
719 import mod
720
721 @mod.deca
722 @mod.decb(mod.C())
723 def func(x: int) -> int:
724 pass
725 [file mod.py]
726 from typing import Callable, TypeVar
727 F = TypeVar('F', bound=Callable)
728
729 def deca(func: Callable[[int], int]) -> Callable[[str], str]:
730 pass
731 def decb(arg: C) -> Callable[[F], F]:
732 pass
733 class C:
734 pass
735 [out]
736 <m.func> -> m
737 <mod.C.__init__> -> m
738 <mod.C.__new__> -> m
739 <mod.C> -> m
740 <mod.deca> -> m
741 <mod.decb> -> m
742 <mod> -> m
743
744 [case testDecoratorDepsFunc]
745 import mod
746
747 def outer() -> None:
748 @mod.deca
749 @mod.decb(mod.C())
750 def func(x: int) -> int:
751 pass
752 [file mod.py]
753 from typing import Callable, TypeVar
754 F = TypeVar('F', bound=Callable)
755
756 def deca(func: Callable[[int], int]) -> Callable[[str], str]:
757 pass
758 def decb(arg: C) -> Callable[[F], F]:
759 pass
760 class C:
761 pass
762 [out]
763 <mod.C.__init__> -> m.outer
764 <mod.C.__new__> -> m.outer
765 <mod.C> -> m.outer
766 <mod.deca> -> m.outer
767 <mod.decb> -> m.outer
768 <mod> -> m, m.outer
769
770 [case testDecoratorDepsDeepNested]
771 import mod
772
773 def outer() -> None:
774 def inner() -> None:
775 @mod.dec
776 def func(x: int) -> int:
777 pass
778 [file mod.py]
779 from typing import Callable
780 def dec(func: Callable[[int], int]) -> Callable[[str], str]:
781 pass
782 [out]
783 <mod.dec> -> m.outer
784 <mod> -> m, m.outer
785
786 [case testDecoratorDepsNestedClass]
787 import mod
788
789 class Outer:
790 class Inner:
791 c = mod.C()
792 @c.dec
793 def func(self, x: int) -> int:
794 pass
795 [file mod.py]
796 from typing import Callable
797 class C:
798 def dec(self, func: Callable[..., int]) -> Callable[..., str]:
799 pass
800 [out]
801 <m.Outer.Inner.func> -> m
802 <m.Outer.Inner> -> m.Outer.Inner
803 <m.Outer> -> m.Outer
804 <mod.C.__init__> -> m
805 <mod.C.__new__> -> m
806 <mod.C.dec> -> m
807 <mod.C> -> <m.Outer.Inner.c>, m
808 <mod> -> m
809
810 [case testDecoratorDepsClassInFunction]
811 import mod
812
813 def outer() -> None:
814 class Inner:
815 c = mod.C()
816 @c.dec
817 def func(self, x: int) -> int:
818 pass
819 [file mod.py]
820 from typing import Callable
821 class C:
822 def dec(self, func: Callable[..., int]) -> Callable[..., str]:
823 pass
824 [out]
825 <m.outer> -> m.outer
826 <mod.C.__init__> -> m.outer
827 <mod.C.__new__> -> m.outer
828 <mod.C.dec> -> m.outer
829 <mod.C> -> <m.outer.c>, m.outer
830 <mod> -> m, m.outer
831
832 [case DecoratorDepsMethod]
833 import mod
834
835 class D:
836 @mod.deca
837 @mod.decb(mod.C())
838 def func(self, x: int) -> int:
839 pass
840 [file mod.py]
841 from typing import Callable, TypeVar
842 F = TypeVar('F', bound=Callable)
843
844 def deca(func: Callable[..., int]) -> Callable[..., str]:
845 pass
846 def decb(arg: C) -> Callable[[F], F]:
847 pass
848 class C:
849 pass
850 [out]
851 <m.D.func> -> m
852 <m.D> -> m.D
853 <mod.C.__init__> -> m
854 <mod.C.__new__> -> m
855 <mod.C> -> m
856 <mod.deca> -> m
857 <mod.decb> -> m
858 <mod> -> m
859
860 [case testMissingModuleClass1]
861 from b import A # type: ignore
862 def f(x: A) -> None:
863 x.foo()
864 [out]
865 <m.A> -> <m.f>, m.f
866 <b.A> -> m
867 <b> -> m
868
869 [case testMissingModuleClass2]
870 from p.b import A # type: ignore
871 def f(x: A) -> None:
872 x.foo()
873 [out]
874 <m.A> -> <m.f>, m.f
875 <p.b.A> -> m
876 <p.b> -> m
877
878 [case testIgnoredMissingModuleAttribute]
879 import a
880 a.x # type: ignore
881
882 import b.c
883 b.c.x # type: ignore
884
885 from b import c
886 c.y # type: ignore
887 [file a.py]
888 [file b/__init__.py]
889 [file b/c.py]
890 [builtins fixtures/module.pyi]
891 [out]
892 <a.x> -> m
893 <a> -> m
894 <b.c.x> -> m
895 <b.c.y> -> m
896 <b.c> -> m
897 <b> -> m
898 <types.ModuleType.__await__> -> <types.ModuleType>
899 <types.ModuleType> -> typing.Awaitable
900
901 [case testIgnoredMissingInstanceAttribute]
902 from a import C
903 C().x # type: ignore
904 [file a.py]
905 class C: pass
906 [out]
907 <a.C.__await__> -> <a.C>
908 <a.C.__init__> -> m
909 <a.C.__new__> -> m
910 <a.C.x> -> m
911 <a.C> -> m, typing.Awaitable
912 <a> -> m
913
914 [case testIgnoredMissingClassAttribute]
915 from a import C
916 C.x # type: ignore
917 [file a.py]
918 class C: pass
919 [out]
920 <a.C.x> -> m
921 <a.C> -> m
922 <a> -> m
923
924 [case testDepsFromOverloadMod]
925 # __dump_all__
926 import mod
927 x = mod.f
928 [file mod.py]
929 from typing import overload, Any
930 import submod
931 @overload
932 def f(x: int) -> submod.A: pass
933 @overload
934 def f(x: str) -> submod.B: pass
935 def f(x) -> Any:
936 y: submod.C
937 y.x
938 [file submod.py]
939 class A: pass
940 class B: pass
941 class C:
942 x: D
943 class D: pass
944 [out]
945 <m.x> -> m
946 <mod.f> -> m
947 <mod> -> m
948 <submod.A> -> <m.x>, <mod.f>, mod.f, submod.A
949 <submod.B> -> <m.x>, <mod.f>, mod.f, submod.B
950 <submod.C.x> -> mod.f
951 <submod.C> -> mod.f, submod.C
952 <submod.D> -> <submod.C.x>, submod, submod.D
953 <submod> -> mod
954
955 [case testDepsFromOverloadFunc]
956 import mod
957 def f() -> None:
958 x = mod.f
959 [file mod.py]
960 from typing import overload
961 @overload
962 def f(x: int) -> None: pass
963 @overload
964 def f(x: str) -> str: pass
965 def f(x):
966 pass
967 [out]
968 <mod.f> -> m.f
969 <mod> -> m, m.f
970
971 [case testDepsToOverloadMod]
972 from typing import overload, Any
973 import mod
974 @overload
975 def f(x: int) -> None: pass
976 @overload
977 def f(x: str) -> str: pass
978 def f(x: Any) -> Any:
979 mod.g()
980 [file mod.py]
981 def g() -> int:
982 pass
983 [out]
984 <mod.g> -> m.f
985 <mod> -> m, m.f
986
987 [case testDepsToOverloadFunc]
988 from typing import overload, Any
989 import mod
990 def outer() -> None:
991 @overload
992 def f(x: int) -> mod.A: pass
993 @overload
994 def f(x: str) -> mod.B: pass
995 def f(x: Any) -> Any:
996 mod.g()
997 [file mod.py]
998 def g() -> int:
999 pass
1000 class A: pass
1001 class B: pass
1002 [out]
1003 <mod.A> -> <m.outer>, m.outer
1004 <mod.B> -> <m.outer>, m.outer
1005 <mod.g> -> m.outer
1006 <mod> -> m, m.outer
1007
1008 [case testDepsFromOverloadToOverload]
1009 from typing import overload, Any
1010 import mod
1011 def outer() -> None:
1012 @overload
1013 def f(x: int) -> None: pass
1014 @overload
1015 def f(x: str) -> str: pass
1016 def f(x: Any) -> Any:
1017 mod.g(str())
1018 [file mod.py]
1019 from typing import overload
1020 @overload
1021 def g(x: int) -> None: pass
1022 @overload
1023 def g(x: str) -> str: pass
1024 def g(x):
1025 pass
1026 [out]
1027 <mod.g> -> m.outer
1028 <mod> -> m, m.outer
1029
1030 [case testDepsFromOverloadToOverloadDefaultClass]
1031 from typing import overload, Any
1032 import mod
1033 class Outer:
1034 @overload
1035 def f(self, x: int) -> None: pass
1036 @overload
1037 def f(self, x: str, cb=mod.g) -> str: pass
1038 def f(self, *args: Any, **kwargs: Any) -> Any:
1039 pass
1040 [file mod.py]
1041 from typing import overload
1042 @overload
1043 def g(x: int) -> None: pass
1044 @overload
1045 def g(x: str) -> str: pass
1046 def g(x):
1047 pass
1048 [builtins fixtures/dict.pyi]
1049 [out]
1050 <m.Outer> -> m.Outer
1051 <mod.g> -> m.Outer.f
1052 <mod> -> m, m.Outer.f
1053
1054 [case testDepsFromOverloadToOverloadDefaultNested]
1055 from typing import overload, Any
1056 import mod
1057 def outer() -> None:
1058 @overload
1059 def f(x: int) -> None: pass
1060 @overload
1061 def f(x: str, cb=mod.g) -> str: pass
1062 def f(*args: Any, **kwargs: Any) -> Any:
1063 pass
1064 [file mod.py]
1065 from typing import overload
1066 @overload
1067 def g(x: int) -> None: pass
1068 @overload
1069 def g(x: str) -> str: pass
1070 def g(x):
1071 pass
1072 [builtins fixtures/dict.pyi]
1073 [out]
1074 <mod.g> -> m.outer
1075 <mod> -> m, m.outer
1076
1077 [case testDepsToOverloadGeneric]
1078 # __dump_all__
1079 import mod
1080 from typing import overload, Any
1081 @overload
1082 def f(x: mod.TA) -> mod.TA: pass
1083 @overload
1084 def f(x: mod.TB, y: int) -> mod.TB: pass
1085 def f(*args: Any, **kwargs: Any) -> Any:
1086 pass
1087 [file mod.py]
1088 from typing import TypeVar
1089 import submod
1090 TA = TypeVar('TA', submod.A, submod.B)
1091 TB = TypeVar('TB', bound=submod.C)
1092 [file submod.py]
1093 class A: pass
1094 class B: pass
1095 class C: pass
1096 [builtins fixtures/dict.pyi]
1097 [out]
1098 <mod.TA> -> <m.f>, m.f
1099 <mod.TB> -> <m.f>, m.f
1100 <mod> -> m
1101 <submod.A> -> <m.f>, <mod.TA>, m.f, mod, submod.A
1102 <submod.B> -> <m.f>, <mod.TA>, m.f, mod, submod.B
1103 <submod.C> -> <m.f>, <mod.TB>, m.f, mod, submod.C
1104 <submod> -> mod
1105
1106 [case testDepsOverloadBothExternalAndImplementationType]
1107 import mod
1108 from typing import overload
1109 @overload
1110 def f(x: mod.A) -> mod.A: pass
1111 @overload
1112 def f(x: mod.B) -> mod.B: pass
1113 def f(x: mod.Base) -> mod.Base:
1114 pass
1115 [file mod.py]
1116 class Base:
1117 pass
1118 class A(Base):
1119 pass
1120 class B(Base):
1121 pass
1122 [out]
1123 <mod.A> -> <m.f>, m.f
1124 <mod.B> -> <m.f>, m.f
1125 <mod.Base> -> <m.f>, m.f
1126 <mod> -> m
1127
1128 [case testCustomIterator]
1129 class A:
1130 def __iter__(self) -> B: pass
1131 class B:
1132 def __iter__(self) -> B: pass
1133 def __next__(self) -> C: pass
1134 class C:
1135 pass
1136 def f() -> None:
1137 for x in A(): pass
1138 [out]
1139 <m.A.__getitem__> -> m.f
1140 <m.A.__init__> -> m.f
1141 <m.A.__iter__> -> m.f
1142 <m.A.__new__> -> m.f
1143 <m.A> -> m.A, m.f
1144 <m.B.__next__> -> m.f
1145 <m.B> -> <m.A.__iter__>, <m.B.__iter__>, m.A.__iter__, m.B, m.B.__iter__
1146 <m.C> -> <m.B.__next__>, m.B.__next__, m.C
1147
1148 [case testDepsLiskovClass]
1149 from mod import A, C
1150 class D(C):
1151 x: A
1152 [file mod.py]
1153 class C:
1154 x: B
1155 class B:
1156 pass
1157 class A(B):
1158 pass
1159 [out]
1160 <m.D.x> -> m
1161 <m.D> -> m.D
1162 <mod.A> -> <m.D.x>, m
1163 <mod.C.(abstract)> -> <m.D.__init__>, m
1164 <mod.C.__init__> -> <m.D.__init__>
1165 <mod.C.__new__> -> <m.D.__new__>
1166 <mod.C.x> -> <m.D.x>
1167 <mod.C> -> m, m.D
1168 <mod> -> m
1169
1170 [case testDepsLiskovMethod]
1171 from mod import A, C
1172 class D(C):
1173 def __init__(self) -> None:
1174 self.x: A
1175 [file mod.py]
1176 class C:
1177 def __init__(self) -> None:
1178 self.x: B
1179 class B:
1180 pass
1181 class A(B):
1182 pass
1183 [out]
1184 <m.D.x> -> m.D.__init__
1185 <m.D> -> m.D
1186 <mod.A> -> <m.D.x>, m, m.D.__init__
1187 <mod.C.(abstract)> -> <m.D.__init__>, m
1188 <mod.C.__init__> -> <m.D.__init__>, m.D.__init__
1189 <mod.C.__new__> -> <m.D.__new__>
1190 <mod.C.x> -> <m.D.x>
1191 <mod.C> -> m, m.D
1192 <mod> -> m
1193
1194 [case testIndexedStarLvalue]
1195 from typing import List, Tuple
1196
1197 class B:
1198 def __setitem__(self, i: int, v: List[str]) -> None: pass
1199
1200 def g() -> Tuple[int, str, str]: pass
1201
1202 def f(b: B) -> None:
1203 a, *b[0] = g()
1204 [builtins fixtures/list.pyi]
1205 [out]
1206 <m.B.__getitem__> -> m.f
1207 <m.B.__setitem__> -> m.f
1208 <m.B> -> <m.f>, m.B, m.f
1209 <m.g> -> m.f
1210
1211 [case testLogicalDecorator]
1212 # flags: --logical-deps
1213 from mod import dec
1214 @dec
1215 def f() -> None:
1216 pass
1217 [file mod.py]
1218 from typing import Callable
1219 def dec(f: Callable[[], None]) -> Callable[[], None]:
1220 pass
1221 [out]
1222 <mod.dec> -> <m.f>, m
1223
1224 [case testLogicalDecoratorWithArgs]
1225 # flags: --logical-deps
1226 from mod import dec
1227 @dec(str())
1228 def f() -> None:
1229 pass
1230 [file mod.py]
1231 from typing import Callable
1232 def dec(arg: str) -> Callable[[Callable[[], None]], Callable[[], None]]:
1233 pass
1234 [out]
1235 <mod.dec> -> <m.f>, m
1236
1237 [case testLogicalDecoratorMember]
1238 # flags: --logical-deps
1239 import mod
1240 @mod.dec
1241 def f() -> None:
1242 pass
1243 [file mod.py]
1244 from typing import Callable
1245 def dec(f: Callable[[], None]) -> Callable[[], None]:
1246 pass
1247 [out]
1248 <mod.dec> -> <m.f>, m
1249 <mod> -> m
1250
1251 [case testLogicalDefinition]
1252 # flags: --logical-deps
1253 from mod import func
1254 b = func()
1255 [file mod.py]
1256 def func() -> int:
1257 pass
1258 [out]
1259 <m.b> -> m
1260 <mod.func> -> <m.b>, m
1261
1262 [case testLogicalDefinitionIrrelevant]
1263 # flags: --logical-deps
1264 from mod import func
1265 def outer() -> None:
1266 a = func()
1267 b: int = func()
1268 c = int()
1269 c = func()
1270 [file mod.py]
1271 def func() -> int:
1272 pass
1273 [out]
1274 <m.b> -> m
1275 <m.c> -> m
1276 <mod.func> -> m, m.outer
1277
1278 [case testLogicalDefinitionMember]
1279 # flags: --logical-deps
1280 import mod
1281 b = mod.func()
1282 [file mod.py]
1283 def func() -> int:
1284 pass
1285 [out]
1286 <m.b> -> m
1287 <mod.func> -> <m.b>, m
1288 <mod> -> m
1289
1290 [case testLogicalDefinitionClass]
1291 # flags: --logical-deps
1292 from mod import Cls
1293 b = Cls()
1294 [file mod.py]
1295 class Base:
1296 def __init__(self) -> None: pass
1297 class Cls(Base): pass
1298 [out]
1299 <m.b> -> m
1300 <mod.Base.__init__> -> <m.b>
1301 <mod.Cls.__init__> -> m
1302 <mod.Cls.__new__> -> m
1303 <mod.Cls> -> <m.b>, m
1304
1305 [case testLogicalBaseAttribute]
1306 # flags: --logical-deps
1307 from mod import C
1308 class D(C):
1309 x: int
1310 [file mod.py]
1311 class C:
1312 x: int
1313 y: int
1314 [out]
1315 <m.D.x> -> m
1316 <m.D> -> m.D
1317 <mod.C.x> -> <m.D.x>
1318 <mod.C> -> m, m.D
1319
1320 [case testLogicalIgnoredImport1]
1321 # flags: --logical-deps --ignore-missing-imports
1322 import foo
1323 def f() -> None:
1324 foo.x
1325 [out]
1326 <foo.x> -> m.f
1327 <foo> -> m, m.f
1328
1329 [case testLogicalIgnoredImport2]
1330 # flags: --logical-deps --ignore-missing-imports
1331 import foo.bar
1332 import a.b.c.d
1333 def f() -> None:
1334 foo.bar.x
1335 foo.bar.x.y
1336 a.b.c.d.e
1337 [out]
1338 <a.b.c.d.e> -> m.f
1339 <a.b.c.d> -> m, m.f
1340 <a.b.c> -> m.f
1341 <a.b> -> m.f
1342 <a> -> m.f
1343 <foo.bar.x.y> -> m.f
1344 <foo.bar.x> -> m.f
1345 <foo.bar> -> m, m.f
1346 <foo> -> m.f
1347
1348 [case testLogicalIgnoredFromImport]
1349 # flags: --logical-deps --ignore-missing-imports
1350 from foo.bar import f, C
1351 def g() -> None:
1352 f()
1353 C.ff()
1354 def gg(x: C) -> None:
1355 z = x.y
1356 z.zz
1357 [out]
1358 <foo.bar.C.ff> -> m.g
1359 <foo.bar.C.y> -> m.gg
1360 <foo.bar.C> -> <m.gg>, m.g, m.gg
1361 <foo.bar.f> -> m.g
1362
1363 [case testLogical__init__]
1364 # flags: --logical-deps
1365 class A:
1366 def __init__(self) -> None: pass
1367 class B(A): pass
1368 class C(B): pass
1369 class D(A):
1370 def __init__(self, x: int) -> None: pass
1371 def f() -> None:
1372 A()
1373 def g() -> None:
1374 C()
1375 def h() -> None:
1376 D(1)
1377 [out]
1378 <m.A.__init__> -> m.f
1379 <m.A.__new__> -> m.f
1380 <m.A> -> m, m.A, m.B, m.D, m.f
1381 <m.B> -> m, m.B, m.C
1382 <m.C.__init__> -> m.g
1383 <m.C.__new__> -> m.g
1384 <m.C> -> m.C, m.g
1385 <m.D.__init__> -> m.h
1386 <m.D.__new__> -> m.h
1387 <m.D> -> m.D, m.h
1388
1389 [case testDataclassDeps]
1390 from dataclasses import dataclass
1391
1392 Z = int
1393
1394 @dataclass
1395 class A:
1396 x: Z
1397
1398 @dataclass
1399 class B(A):
1400 y: int
1401 [builtins fixtures/dataclasses.pyi]
1402
1403 [out]
1404 <m.A.(abstract)> -> <m.B.__init__>, m
1405 <m.A.__dataclass_fields__> -> <m.B.__dataclass_fields__>
1406 <m.A.__init__> -> <m.B.__init__>, m.B.__init__
1407 <m.A.__match_args__> -> <m.B.__match_args__>
1408 <m.A.__mypy-replace> -> <m.B.__mypy-replace>, m, m.B.__mypy-replace
1409 <m.A.__new__> -> <m.B.__new__>
1410 <m.A.x> -> <m.B.x>
1411 <m.A.y> -> <m.B.y>
1412 <m.A> -> m, m.A, m.B
1413 <m.A[wildcard]> -> m
1414 <m.B.__mypy-replace> -> m
1415 <m.B.y> -> m
1416 <m.B> -> m.B
1417 <m.Z> -> m
1418 <dataclasses.dataclass> -> m
1419 <dataclasses> -> m
1420
1421 [case testPEP695TypeAliasDeps]
1422 # flags: --python-version=3.12
1423 from a import C, E
1424 type A = C
1425 type A2 = A
1426 type A3 = E
1427 [file a.py]
1428 class C: pass
1429 class D: pass
1430 type E = D
1431 [out]
1432 <m.A2> -> m
1433 <m.A3> -> m
1434 <m.A> -> m
1435 <a.C.__init__> -> m
1436 <a.C.__new__> -> m
1437 <a.C> -> m
1438 <a.E> -> m
1439 <a> -> m
1440 [builtins fixtures/tuple.pyi]
1441 [typing fixtures/typing-full.pyi]