Debian Codegraph @

test-data/unit/semanal-modules.test

raw
1 -- NOTE: If a module has a name starting or ending with _, it is skipped in
2 -- output.
3
4 [case testImport]
5 import x
6 x.y
7 [file x.py]
8 y = 1
9 [out]
10 MypyFile:1(
11 Import:1(x)
12 ExpressionStmt:2(
13 MemberExpr:2(
14 NameExpr(x)
15 y [x.y])))
16 MypyFile:1(
17 tmp/x.py
18 AssignmentStmt:1(
19 NameExpr(y [x.y])
20 IntExpr(1)
21 builtins.int))
22
23 [case testImportedNameInType]
24 import m
25 x = None # type: m.c
26 [file m.py]
27 class c: pass
28 [out]
29 MypyFile:1(
30 Import:1(m)
31 AssignmentStmt:2(
32 NameExpr(x [__main__.x])
33 NameExpr(None [builtins.None])
34 m.c))
35 MypyFile:1(
36 tmp/m.py
37 ClassDef:1(
38 c
39 PassStmt:1()))
40
41 [case testImportFrom]
42 from m import y
43 x = y
44 [file m.py]
45 y = 1
46 [out]
47 MypyFile:1(
48 ImportFrom:1(m, [y])
49 AssignmentStmt:2(
50 NameExpr(x* [__main__.x])
51 NameExpr(y [m.y])))
52 MypyFile:1(
53 tmp/m.py
54 AssignmentStmt:1(
55 NameExpr(y [m.y])
56 IntExpr(1)
57 builtins.int))
58
59 [case testImportFromType]
60 from m import c
61 x = None # type: c
62 [file m.py]
63 class c: pass
64 [out]
65 MypyFile:1(
66 ImportFrom:1(m, [c])
67 AssignmentStmt:2(
68 NameExpr(x [__main__.x])
69 NameExpr(None [builtins.None])
70 m.c))
71 MypyFile:1(
72 tmp/m.py
73 ClassDef:1(
74 c
75 PassStmt:1()))
76
77 [case testImportMultiple]
78 import _m, _n
79 _m.x, _n.y
80 [fixture _m.py]
81 x = 1
82 [fixture _n.py]
83 y = 2
84 [out]
85 MypyFile:1(
86 Import:1(_m, _n)
87 ExpressionStmt:2(
88 TupleExpr:2(
89 MemberExpr:2(
90 NameExpr(_m)
91 x [_m.x])
92 MemberExpr:2(
93 NameExpr(_n)
94 y [_n.y]))))
95
96 [case testImportAs]
97 import _m as n
98 n.x
99 [fixture _m.py]
100 x = 1
101 [out]
102 MypyFile:1(
103 Import:1(_m : n)
104 ExpressionStmt:2(
105 MemberExpr:2(
106 NameExpr(n [_m])
107 x [_m.x])))
108
109 [case testImportFromMultiple]
110 from _m import x, y
111 x, y
112 [fixture _m.py]
113 x = y = 1
114 [out]
115 MypyFile:1(
116 ImportFrom:1(_m, [x, y])
117 ExpressionStmt:2(
118 TupleExpr:2(
119 NameExpr(x [_m.x])
120 NameExpr(y [_m.y]))))
121
122 [case testImportFromAs]
123 from _m import y as z
124 z
125 [fixture _m.py]
126 y = 1
127 [out]
128 MypyFile:1(
129 ImportFrom:1(_m, [y : z])
130 ExpressionStmt:2(
131 NameExpr(z [_m.y])))
132
133 [case testAccessImportedName]
134 from m import x
135 y = x
136 [file m.py]
137 from _n import x
138 [fixture _n.py]
139 x = 1
140 [out]
141 MypyFile:1(
142 ImportFrom:1(m, [x])
143 AssignmentStmt:2(
144 NameExpr(y* [__main__.y])
145 NameExpr(x [_n.x])))
146 MypyFile:1(
147 tmp/m.py
148 ImportFrom:1(_n, [x]))
149
150 [case testAccessImportedName2]
151 import _m
152 y = _m.x
153 [fixture _m.py]
154 from _n import x
155 [fixture _n.py]
156 x = 1
157 [out]
158 MypyFile:1(
159 Import:1(_m)
160 AssignmentStmt:2(
161 NameExpr(y* [__main__.y])
162 MemberExpr:2(
163 NameExpr(_m)
164 x [_n.x])))
165
166 [case testAccessingImportedNameInType]
167 from _m import c
168 x = None # type: c
169 [fixture _m.py]
170 from _n import c
171 [fixture _n.py]
172 class c: pass
173 [out]
174 MypyFile:1(
175 ImportFrom:1(_m, [c])
176 AssignmentStmt:2(
177 NameExpr(x [__main__.x])
178 NameExpr(None [builtins.None])
179 _n.c))
180
181 [case testAccessingImportedNameInType2]
182 import _m
183 x = None # type: _m.c
184 [fixture _m.py]
185 from _n import c
186 [fixture _n.py]
187 class c: pass
188 [out]
189 MypyFile:1(
190 Import:1(_m)
191 AssignmentStmt:2(
192 NameExpr(x [__main__.x])
193 NameExpr(None [builtins.None])
194 _n.c))
195
196 [case testAccessingImportedModule]
197 from _m import _n
198 _n.x
199 [fixture _m.py]
200 import _n
201 [fixture _n.py]
202 x = 1
203 [out]
204 MypyFile:1(
205 ImportFrom:1(_m, [_n])
206 ExpressionStmt:2(
207 MemberExpr:2(
208 NameExpr(_n)
209 x [_n.x])))
210
211 [case testAccessingImportedModule2]
212 import _m
213 _m._n.x
214 [fixture _m.py]
215 import _n
216 [fixture _n.py]
217 x = 1
218 [out]
219 MypyFile:1(
220 Import:1(_m)
221 ExpressionStmt:2(
222 MemberExpr:2(
223 MemberExpr:2(
224 NameExpr(_m)
225 _n)
226 x [_n.x])))
227
228 [case testAccessTypeViaDoubleIndirection]
229 from _m import c
230 a = None # type: c
231 [fixture _m.py]
232 from _n import c
233 [fixture _n.py]
234 class c: pass
235 [out]
236 MypyFile:1(
237 ImportFrom:1(_m, [c])
238 AssignmentStmt:2(
239 NameExpr(a [__main__.a])
240 NameExpr(None [builtins.None])
241 _n.c))
242
243 [case testAccessTypeViaDoubleIndirection2]
244 import _m
245 a = None # type: _m.c
246 [fixture _m.py]
247 from _n import c
248 [fixture _n.py]
249 class c: pass
250 [out]
251 MypyFile:1(
252 Import:1(_m)
253 AssignmentStmt:2(
254 NameExpr(a [__main__.a])
255 NameExpr(None [builtins.None])
256 _n.c))
257
258 [case testImportAsterisk]
259 from _m import *
260 x, y
261 [fixture _m.py]
262 x = y = 1
263 [out]
264 MypyFile:1(
265 ImportAll:1(_m)
266 ExpressionStmt:2(
267 TupleExpr:2(
268 NameExpr(x [_m.x])
269 NameExpr(y [_m.y]))))
270
271 [case testImportAsteriskAndImportedNames]
272 from _m import *
273 n_.x, y
274 [fixture _m.py]
275 import n_
276 from n_ import y
277 [fixture n_.py]
278 x = y = 1
279 [out]
280 MypyFile:1(
281 ImportAll:1(_m)
282 ExpressionStmt:2(
283 TupleExpr:2(
284 MemberExpr:2(
285 NameExpr(n_)
286 x [n_.x])
287 NameExpr(y [n_.y]))))
288
289 [case testImportAsteriskAndImportedNamesInTypes]
290 from _m import *
291 x = None # type: n_.c
292 y = None # type: d
293 [fixture _m.py]
294 import n_
295 from n_ import d
296 [fixture n_.py]
297 class c: pass
298 class d: pass
299 [out]
300 MypyFile:1(
301 ImportAll:1(_m)
302 AssignmentStmt:2(
303 NameExpr(x [__main__.x])
304 NameExpr(None [builtins.None])
305 n_.c)
306 AssignmentStmt:3(
307 NameExpr(y [__main__.y])
308 NameExpr(None [builtins.None])
309 n_.d))
310
311 [case testModuleInSubdir]
312 import _m
313 _m.x
314 [fixture _m/__init__.py]
315 x = 1
316 [out]
317 MypyFile:1(
318 Import:1(_m)
319 ExpressionStmt:2(
320 MemberExpr:2(
321 NameExpr(_m)
322 x [_m.x])))
323
324 [case testNestedModules]
325 import m.n
326 m.n.x, m.y
327 [fixture m/__init__.py]
328 y = 1
329 [file m/n.py]
330 x = 1
331 [out]
332 MypyFile:1(
333 Import:1(m.n)
334 ExpressionStmt:2(
335 TupleExpr:2(
336 MemberExpr:2(
337 MemberExpr:2(
338 NameExpr(m)
339 n [m.n])
340 x [m.n.x])
341 MemberExpr:2(
342 NameExpr(m)
343 y [m.y]))))
344 MypyFile:1(
345 tmp/m/n.py
346 AssignmentStmt:1(
347 NameExpr(x [m.n.x])
348 IntExpr(1)
349 builtins.int))
350
351 [case testImportFromSubmodule]
352 from m._n import x
353 x
354 [fixture m/__init__.py]
355 [fixture m/_n.py]
356 x = 1
357 [out]
358 MypyFile:1(
359 ImportFrom:1(m._n, [x])
360 ExpressionStmt:2(
361 NameExpr(x [m._n.x])))
362
363 [case testImportAllFromSubmodule]
364 from m._n import *
365 x, y
366 [fixture m/__init__.py]
367 [fixture m/_n.py]
368 x = y = 1
369 [out]
370 MypyFile:1(
371 ImportAll:1(m._n)
372 ExpressionStmt:2(
373 TupleExpr:2(
374 NameExpr(x [m._n.x])
375 NameExpr(y [m._n.y]))))
376
377 [case testSubmodulesAndTypes]
378 import m._n
379 x = None # type: m._n.c
380 [fixture m/__init__.py]
381 [fixture m/_n.py]
382 class c: pass
383 [out]
384 MypyFile:1(
385 Import:1(m._n)
386 AssignmentStmt:2(
387 NameExpr(x [__main__.x])
388 NameExpr(None [builtins.None])
389 m._n.c))
390
391 [case testSubmodulesAndTypes2]
392 from m._n import c
393 x = None # type: c
394 [fixture m/__init__.py]
395 [fixture m/_n.py]
396 class c: pass
397 [out]
398 MypyFile:1(
399 ImportFrom:1(m._n, [c])
400 AssignmentStmt:2(
401 NameExpr(x [__main__.x])
402 NameExpr(None [builtins.None])
403 m._n.c))
404
405 [case testFromPackageImportModule]
406 from m import _n
407 _n.x
408 [fixture m/__init__.py]
409 [fixture m/_n.py]
410 x = 1
411 [out]
412 MypyFile:1(
413 ImportFrom:1(m, [_n])
414 ExpressionStmt:2(
415 MemberExpr:2(
416 NameExpr(_n [m._n])
417 x [m._n.x])))
418
419 [case testDeeplyNestedModule]
420 import m.n.k
421 m.n.k.x
422 m.n.b
423 m.a
424 [fixture m/__init__.py]
425 a = 1
426 [fixture m/n/__init__.py]
427 b = 1
428 [file m/n/k.py]
429 x = 1
430 [out]
431 MypyFile:1(
432 Import:1(m.n.k)
433 ExpressionStmt:2(
434 MemberExpr:2(
435 MemberExpr:2(
436 MemberExpr:2(
437 NameExpr(m)
438 n [m.n])
439 k [m.n.k])
440 x [m.n.k.x]))
441 ExpressionStmt:3(
442 MemberExpr:3(
443 MemberExpr:3(
444 NameExpr(m)
445 n [m.n])
446 b [m.n.b]))
447 ExpressionStmt:4(
448 MemberExpr:4(
449 NameExpr(m)
450 a [m.a])))
451 MypyFile:1(
452 tmp/m/n/k.py
453 AssignmentStmt:1(
454 NameExpr(x [m.n.k.x])
455 IntExpr(1)
456 builtins.int))
457
458 [case testImportInSubmodule]
459 import m._n
460 y = m._n.x
461 [fixture m/__init__.py]
462 [fixture m/_n.py]
463 from m._k import x
464 [fixture m/_k.py]
465 x = 1
466 [out]
467 MypyFile:1(
468 Import:1(m._n)
469 AssignmentStmt:2(
470 NameExpr(y* [__main__.y])
471 MemberExpr:2(
472 MemberExpr:2(
473 NameExpr(m)
474 _n [m._n])
475 x [m._k.x])))
476
477 [case testBuiltinsUsingModule]
478 o = None # type: __builtins__.object
479 [out]
480 MypyFile:1(
481 AssignmentStmt:1(
482 NameExpr(o [__main__.o])
483 NameExpr(None [builtins.None])
484 builtins.object))
485
486 [case testImplicitAccessToBuiltins]
487 object
488 [out]
489 MypyFile:1(
490 ExpressionStmt:1(
491 NameExpr(object [builtins.object])))
492
493 [case testAssignmentToModuleAttribute]
494 import _m
495 _m.x = (
496 _m.x)
497 [fixture _m.py]
498 x = None
499 [out]
500 MypyFile:1(
501 Import:1(_m)
502 AssignmentStmt:2(
503 MemberExpr:2(
504 NameExpr(_m)
505 x [_m.x])
506 MemberExpr:3(
507 NameExpr(_m)
508 x [_m.x])))
509
510 [case testAssignmentThatRefersToModule]
511 import _m
512 _m.x[None] = None
513 [fixture _m.py]
514 x = None
515 [out]
516 MypyFile:1(
517 Import:1(_m)
518 AssignmentStmt:2(
519 IndexExpr:2(
520 MemberExpr:2(
521 NameExpr(_m)
522 x [_m.x])
523 NameExpr(None [builtins.None]))
524 NameExpr(None [builtins.None])))
525
526 [case testImportInBlock]
527 if 1:
528 import _x
529 _x.y
530 [fixture _x.py]
531 y = 1
532 [out]
533 MypyFile:1(
534 IfStmt:1(
535 If(
536 IntExpr(1))
537 Then(
538 Import:2(_x)
539 ExpressionStmt:3(
540 MemberExpr:3(
541 NameExpr(_x)
542 y [_x.y])))))
543
544 [case testImportInFunction]
545 def f() -> None:
546 import _x
547 _x.y
548 [fixture _x.py]
549 y = 1
550 [out]
551 MypyFile:1(
552 FuncDef:1(
553 f
554 def ()
555 Block:2(
556 Import:2(_x)
557 ExpressionStmt:3(
558 MemberExpr:3(
559 NameExpr(_x)
560 y [_x.y])))))
561
562 [case testImportInClassBody]
563 class A:
564 from _x import y
565 z = y
566 [fixture _x.py]
567 y = 1
568 [out]
569 MypyFile:1(
570 ClassDef:1(
571 A
572 ImportFrom:2(_x, [y])
573 AssignmentStmt:3(
574 NameExpr(z* [m])
575 NameExpr(y [__main__.A.y]))))
576
577 [case testImportInClassBody2]
578 class A:
579 import _x
580 z = _x.y
581 [fixture _x.py]
582 y = 1
583 [out]
584 MypyFile:1(
585 ClassDef:1(
586 A
587 Import:2(_x)
588 AssignmentStmt:3(
589 NameExpr(z* [m])
590 MemberExpr:3(
591 NameExpr(_x)
592 y [_x.y]))))
593
594 [case testImportModuleTwice]
595 def f() -> None:
596 import x
597 import x
598 x.y
599 [file x.py]
600 y = 1
601 [out]
602 MypyFile:1(
603 FuncDef:1(
604 f
605 def ()
606 Block:2(
607 Import:2(x)
608 Import:3(x)
609 ExpressionStmt:4(
610 MemberExpr:4(
611 NameExpr(x)
612 y [x.y])))))
613 MypyFile:1(
614 tmp/x.py
615 AssignmentStmt:1(
616 NameExpr(y [x.y])
617 IntExpr(1)
618 builtins.int))
619
620 [case testRelativeImport0]
621 import m.x
622 m.x.z.y
623 [fixture m/__init__.py]
624 [file m/x.py]
625 from . import z
626 [file m/z.py]
627 y = 1
628 [out]
629 MypyFile:1(
630 Import:1(m.x)
631 ExpressionStmt:2(
632 MemberExpr:2(
633 MemberExpr:2(
634 MemberExpr:2(
635 NameExpr(m)
636 x [m.x])
637 z [m.z])
638 y [m.z.y])))
639 MypyFile:1(
640 tmp/m/x.py
641 ImportFrom:1(., [z]))
642 MypyFile:1(
643 tmp/m/z.py
644 AssignmentStmt:1(
645 NameExpr(y [m.z.y])
646 IntExpr(1)
647 builtins.int))
648
649 [case testRelativeImport1]
650 import m.t.b as b
651 b.x.y
652 b.z.y
653 [fixture m/__init__.py]
654 [file m/x.py]
655 y = 1
656 [file m/z.py]
657 y = 3
658 [fixture m/t/__init__.py]
659 [file m/t/b.py]
660 from .. import x, z
661 [out]
662 MypyFile:1(
663 Import:1(m.t.b : b)
664 ExpressionStmt:2(
665 MemberExpr:2(
666 MemberExpr:2(
667 NameExpr(b [m.t.b])
668 x [m.x])
669 y [m.x.y]))
670 ExpressionStmt:3(
671 MemberExpr:3(
672 MemberExpr:3(
673 NameExpr(b [m.t.b])
674 z [m.z])
675 y [m.z.y])))
676 MypyFile:1(
677 tmp/m/t/b.py
678 ImportFrom:1(.., [x, z]))
679 MypyFile:1(
680 tmp/m/x.py
681 AssignmentStmt:1(
682 NameExpr(y [m.x.y])
683 IntExpr(1)
684 builtins.int))
685 MypyFile:1(
686 tmp/m/z.py
687 AssignmentStmt:1(
688 NameExpr(y [m.z.y])
689 IntExpr(3)
690 builtins.int))
691
692 [case testRelativeImport2]
693 import m.t.b as b
694 b.xy
695 b.zy
696 [fixture m/__init__.py]
697 [file m/x.py]
698 y = 1
699 [file m/z.py]
700 y = 3
701 [fixture m/t/__init__.py]
702 [file m/t/b.py]
703 from ..x import y as xy
704 from ..z import y as zy
705 [out]
706 MypyFile:1(
707 Import:1(m.t.b : b)
708 ExpressionStmt:2(
709 MemberExpr:2(
710 NameExpr(b [m.t.b])
711 xy [m.x.y]))
712 ExpressionStmt:3(
713 MemberExpr:3(
714 NameExpr(b [m.t.b])
715 zy [m.z.y])))
716 MypyFile:1(
717 tmp/m/t/b.py
718 ImportFrom:1(..x, [y : xy])
719 ImportFrom:2(..z, [y : zy]))
720 MypyFile:1(
721 tmp/m/x.py
722 AssignmentStmt:1(
723 NameExpr(y [m.x.y])
724 IntExpr(1)
725 builtins.int))
726 MypyFile:1(
727 tmp/m/z.py
728 AssignmentStmt:1(
729 NameExpr(y [m.z.y])
730 IntExpr(3)
731 builtins.int))
732
733 [case testRelativeImport3]
734 import m.t
735 m.zy
736 m.xy
737 m.t.y
738 [fixture m/__init__.py]
739 from .x import *
740 from .z import *
741 [file m/x.py]
742 from .z import zy as xy
743 [file m/z.py]
744 zy = 3
745 [fixture m/t/__init__.py]
746 from .b import *
747 [file m/t/b.py]
748 from .. import xy as y
749 [out]
750 MypyFile:1(
751 Import:1(m.t)
752 ExpressionStmt:2(
753 MemberExpr:2(
754 NameExpr(m)
755 zy [m.z.zy]))
756 ExpressionStmt:3(
757 MemberExpr:3(
758 NameExpr(m)
759 xy [m.z.zy]))
760 ExpressionStmt:4(
761 MemberExpr:4(
762 MemberExpr:4(
763 NameExpr(m)
764 t [m.t])
765 y [m.z.zy])))
766 MypyFile:1(
767 tmp/m/t/b.py
768 ImportFrom:1(.., [xy : y]))
769 MypyFile:1(
770 tmp/m/x.py
771 ImportFrom:1(.z, [zy : xy]))
772 MypyFile:1(
773 tmp/m/z.py
774 AssignmentStmt:1(
775 NameExpr(zy [m.z.zy])
776 IntExpr(3)
777 builtins.int))
778
779 [case testRelativeImportFromSameModule]
780 import m.x
781 [fixture m/__init__.py]
782 [file m/x.py]
783 from .x import nonexistent
784 [out]
785 tmp/m/x.py:1: error: Module "m.x" has no attribute "nonexistent"
786
787 [case testImportFromSameModule]
788 import m.x
789 [fixture m/__init__.py]
790 [file m/x.py]
791 from m.x import nonexistent
792 [out]
793 tmp/m/x.py:1: error: Module "m.x" has no attribute "nonexistent"
794
795 [case testImportMisspellingSingleCandidate]
796 import f
797 [fixture m/__init__.py]
798 [file m/x.py]
799 def some_function():
800 pass
801 [file f.py]
802 from m.x import somefunction
803 [out]
804 tmp/f.py:1: error: Module "m.x" has no attribute "somefunction"; maybe "some_function"?
805
806 [case testImportMisspellingMultipleCandidates]
807 import f
808 [fixture m/__init__.py]
809 [file m/x.py]
810 def some_function():
811 pass
812 def somef_unction():
813 pass
814 [file f.py]
815 from m.x import somefunction
816 [out]
817 tmp/f.py:1: error: Module "m.x" has no attribute "somefunction"; maybe "some_function" or "somef_unction"?
818
819 [case testImportMisspellingMultipleCandidatesTruncated]
820 import f
821 [fixture m/__init__.py]
822 [file m/x.py]
823 def some_function():
824 pass
825 def somef_unction():
826 pass
827 def somefu_nction():
828 pass
829 def somefun_ction():
830 pass
831 [file f.py]
832 from m.x import somefunction
833 [out]
834 tmp/f.py:1: error: Module "m.x" has no attribute "somefunction"; maybe "some_function", "somef_unction", or "somefu_nction"?
835
836 [case testFromImportAsInStub]
837 from m import *
838 x
839 y # E: Name "y" is not defined
840 [file m.pyi]
841 from m2 import x as x
842 from m2 import y
843 [file m2.py]
844 x = 1
845 y = 2
846 [out]
847
848 [case testFromImportAsInNonStub]
849 from m_ import *
850 x
851 y
852 [fixture m_.py]
853 from m2_ import x as x
854 from m2_ import y
855 [fixture m2_.py]
856 x = 1
857 y = 2
858 [out]
859 MypyFile:1(
860 ImportAll:1(m_)
861 ExpressionStmt:2(
862 NameExpr(x [m2_.x]))
863 ExpressionStmt:3(
864 NameExpr(y [m2_.y])))
865
866 [case testImportAsInStub]
867 from m import *
868 m2
869 m3 # E: Name "m3" is not defined
870 [file m.pyi]
871 import m2 as m2
872 import m3
873 [file m2.py]
874 [file m3.py]
875 [out]
876
877 [case testImportAsInNonStub]
878 from m_ import *
879 m2_
880 m3_
881 [fixture m_.py]
882 import m2_ as m2_
883 import m3_
884 [fixture m2_.py]
885 [fixture m3_.py]
886 [out]
887 MypyFile:1(
888 ImportAll:1(m_)
889 ExpressionStmt:2(
890 NameExpr(m2_))
891 ExpressionStmt:3(
892 NameExpr(m3_)))
893
894 [case testErrorsInMultipleModules]
895 import m
896 x
897 [file m.py]
898 y
899 [out]
900 tmp/m.py:1: error: Name "y" is not defined
901 main:2: error: Name "x" is not defined
902
903 [case testImportTwice]
904 import typing
905 from x import a, a # ok (we could give a warning, but this is valid)
906 def f() -> None:
907 from x import a
908 from x import a # ok
909 import x
910 import x # ok, since we may import multiple submodules of a package
911 [file x.py]
912 a = 1
913 [out]
914 MypyFile:1(
915 Import:1(typing)
916 ImportFrom:2(x, [a, a])
917 FuncDef:3(
918 f
919 def ()
920 Block:4(
921 ImportFrom:4(x, [a])
922 ImportFrom:5(x, [a])))
923 Import:6(x)
924 Import:7(x))
925 MypyFile:1(
926 tmp/x.py
927 AssignmentStmt:1(
928 NameExpr(a [x.a])
929 IntExpr(1)
930 builtins.int))