Debian Codegraph @

test-data/unit/parse.test

raw
1 -- Test cases for parser. Each test case consists of two sections.
2 -- The first section contains [case NAME] followed by the input code, while
3 -- the second section contains [out] followed by the output from the parser.
4 --
5 -- Lines starting with "--" in this file will be ignored, except for lines
6 -- starting with "----" that are not ignored. The first two dashes of these
7 -- lines are interpreted as escapes and removed.
8
9 [case testEmptyFile]
10 [out]
11 MypyFile:1()
12
13 [case testExpressionStatement]
14 1
15 [out]
16 MypyFile:1(
17 ExpressionStmt:1(
18 IntExpr(1)))
19
20 [case testAssignment]
21 x = 1
22 [out]
23 MypyFile:1(
24 AssignmentStmt:1(
25 NameExpr(x)
26 IntExpr(1)))
27
28 [case testExpressionBasics]
29 x = f(1, None)
30 123 * (2 + x)
31 "hello".lower()
32 -1.23
33 [out]
34 MypyFile:1(
35 AssignmentStmt:1(
36 NameExpr(x)
37 CallExpr:1(
38 NameExpr(f)
39 Args(
40 IntExpr(1)
41 NameExpr(None))))
42 ExpressionStmt:2(
43 OpExpr:2(
44 *
45 IntExpr(123)
46 OpExpr:2(
47 +
48 IntExpr(2)
49 NameExpr(x))))
50 ExpressionStmt:3(
51 CallExpr:3(
52 MemberExpr:3(
53 StrExpr(hello)
54 lower)
55 Args()))
56 ExpressionStmt:4(
57 UnaryExpr:4(
58 -
59 FloatExpr(1.23))))
60
61 [case testSingleQuotedStr]
62 ''
63 'foo'
64 'foo\
65 bar'
66 [out]
67 MypyFile:1(
68 ExpressionStmt:1(
69 StrExpr())
70 ExpressionStmt:2(
71 StrExpr(foo))
72 ExpressionStmt:3(
73 StrExpr(foobar)))
74
75 [case testDoubleQuotedStr]
76 ""
77 "foo"
78 "foo\
79 bar"
80 [out]
81 MypyFile:1(
82 ExpressionStmt:1(
83 StrExpr())
84 ExpressionStmt:2(
85 StrExpr(foo))
86 ExpressionStmt:3(
87 StrExpr(foobar)))
88
89 [case testRawStr]
90 r'x\n\''
91 r"x\n\""
92 [out]
93 MypyFile:1(
94 ExpressionStmt:1(
95 StrExpr(x\n\'))
96 ExpressionStmt:2(
97 StrExpr(x\n\")))
98
99 [case testBytes]
100 b'foo'
101 b"foo\
102 bar"
103 br'x\n\''
104 [out]
105 MypyFile:1(
106 ExpressionStmt:1(
107 BytesExpr(foo))
108 ExpressionStmt:2(
109 BytesExpr(foobar))
110 ExpressionStmt:3(
111 BytesExpr(x\\n\\')))
112
113 [case testEscapesInStrings]
114 '\r\n\t\x2f\u123f'
115 b'\r\n\t\x2f\u123f'
116 [out]
117 MypyFile:1(
118 ExpressionStmt:1(
119 StrExpr(\u000d\u000a\u0009/\u123f))
120 ExpressionStmt:2(
121 BytesExpr(\r\n\t/\\\u123f)))
122 -- Note \\u in the b'...' case (\u sequence not translated)
123
124 [case testEscapedQuote]
125 '\''
126 [out]
127 MypyFile:1(
128 ExpressionStmt:1(
129 StrExpr(')))
130
131 [case testOctalEscapes]
132 '\0\1\177\1234'
133 b'\1\476'
134 [out]
135 MypyFile:1(
136 ExpressionStmt:1(
137 StrExpr(\u0000\u0001\u007fS4))
138 ExpressionStmt:2(
139 BytesExpr(\x01>)))
140
141 [case testUnicodeLiteralInPython3]
142 u'foo'
143 [out]
144 MypyFile:1(
145 ExpressionStmt:1(
146 StrExpr(foo)))
147
148 [case testArrays]
149 a = []
150 a = [1, 2]
151 a[[1]] = a[2]
152 [out]
153 MypyFile:1(
154 AssignmentStmt:1(
155 NameExpr(a)
156 ListExpr:1())
157 AssignmentStmt:2(
158 NameExpr(a)
159 ListExpr:2(
160 IntExpr(1)
161 IntExpr(2)))
162 AssignmentStmt:3(
163 IndexExpr:3(
164 NameExpr(a)
165 ListExpr:3(
166 IntExpr(1)))
167 IndexExpr:3(
168 NameExpr(a)
169 IntExpr(2))))
170
171 [case testTuples]
172 ()
173 (1,)
174 (1, foo)
175 a, b = 1, (2, 3)
176 [out]
177 MypyFile:1(
178 ExpressionStmt:1(
179 TupleExpr:1())
180 ExpressionStmt:2(
181 TupleExpr:2(
182 IntExpr(1)))
183 ExpressionStmt:3(
184 TupleExpr:3(
185 IntExpr(1)
186 NameExpr(foo)))
187 AssignmentStmt:4(
188 TupleExpr:4(
189 NameExpr(a)
190 NameExpr(b))
191 TupleExpr:4(
192 IntExpr(1)
193 TupleExpr:4(
194 IntExpr(2)
195 IntExpr(3)))))
196
197 [case testNestedListAssignment]
198 a1, [b1, c1] = a2, [b2, c2]
199 [out]
200 MypyFile:1(
201 AssignmentStmt:1(
202 TupleExpr:1(
203 NameExpr(a1)
204 TupleExpr:1(
205 NameExpr(b1)
206 NameExpr(c1)))
207 TupleExpr:1(
208 NameExpr(a2)
209 ListExpr:1(
210 NameExpr(b2)
211 NameExpr(c2)))))
212
213 [case testSimpleFunction]
214 def main():
215 1
216 [out]
217 MypyFile:1(
218 FuncDef:1(
219 main
220 Block:2(
221 ExpressionStmt:2(
222 IntExpr(1)))))
223
224 [case testPass]
225 def f():
226 pass
227 [out]
228 MypyFile:1(
229 FuncDef:1(
230 f
231 Block:2(
232 PassStmt:2())))
233
234 [case testIf]
235 if 1:
236 2
237 [out]
238 MypyFile:1(
239 IfStmt:1(
240 If(
241 IntExpr(1))
242 Then(
243 ExpressionStmt:2(
244 IntExpr(2)))))
245
246 [case testIfElse]
247 if 1:
248 2
249 else:
250 3
251 [out]
252 MypyFile:1(
253 IfStmt:1(
254 If(
255 IntExpr(1))
256 Then(
257 ExpressionStmt:2(
258 IntExpr(2)))
259 Else(
260 ExpressionStmt:4(
261 IntExpr(3)))))
262
263 [case testIfElif]
264 if 1:
265 2
266 elif 3:
267 4
268 elif 5:
269 6
270 else:
271 7
272 [out]
273 MypyFile:1(
274 IfStmt:1(
275 If(
276 IntExpr(1))
277 Then(
278 ExpressionStmt:2(
279 IntExpr(2)))
280 Else(
281 IfStmt:3(
282 If(
283 IntExpr(3))
284 Then(
285 ExpressionStmt:4(
286 IntExpr(4)))
287 Else(
288 IfStmt:5(
289 If(
290 IntExpr(5))
291 Then(
292 ExpressionStmt:6(
293 IntExpr(6)))
294 Else(
295 ExpressionStmt:8(
296 IntExpr(7)))))))))
297
298 [case testWhile]
299 while 1:
300 pass
301 [out]
302 MypyFile:1(
303 WhileStmt:1(
304 IntExpr(1)
305 Block:2(
306 PassStmt:2())))
307
308 [case testReturn]
309 def f():
310 return 1
311 [out]
312 MypyFile:1(
313 FuncDef:1(
314 f
315 Block:2(
316 ReturnStmt:2(
317 IntExpr(1)))))
318
319
320 [case testReturnWithoutValue]
321 def f():
322 return
323 [out]
324 MypyFile:1(
325 FuncDef:1(
326 f
327 Block:2(
328 ReturnStmt:2())))
329
330 [case testBreak]
331 while 1:
332 break
333 [out]
334 MypyFile:1(
335 WhileStmt:1(
336 IntExpr(1)
337 Block:2(
338 BreakStmt:2())))
339
340 [case testLargeBlock]
341 if 1:
342 x = 1
343 while 2:
344 pass
345 y = 2
346 [out]
347 MypyFile:1(
348 IfStmt:1(
349 If(
350 IntExpr(1))
351 Then(
352 AssignmentStmt:2(
353 NameExpr(x)
354 IntExpr(1))
355 WhileStmt:3(
356 IntExpr(2)
357 Block:4(
358 PassStmt:4()))
359 AssignmentStmt:5(
360 NameExpr(y)
361 IntExpr(2)))))
362
363 [case testSimpleClass]
364 class A:
365 def f(self):
366 pass
367 [out]
368 MypyFile:1(
369 ClassDef:1(
370 A
371 FuncDef:2(
372 f
373 Args(
374 Var(self))
375 Block:3(
376 PassStmt:3()))))
377
378 [case testGlobalVarWithType]
379 x = 0 # type: int
380 y = False # type: bool
381 [out]
382 MypyFile:1(
383 AssignmentStmt:1(
384 NameExpr(x)
385 IntExpr(0)
386 int?)
387 AssignmentStmt:2(
388 NameExpr(y)
389 NameExpr(False)
390 bool?))
391
392 [case testLocalVarWithType]
393 def f():
394 x = 0 # type: int
395 y = False # type: bool
396 a = None # type: Any
397 [out]
398 MypyFile:1(
399 FuncDef:1(
400 f
401 Block:2(
402 AssignmentStmt:2(
403 NameExpr(x)
404 IntExpr(0)
405 int?)
406 AssignmentStmt:3(
407 NameExpr(y)
408 NameExpr(False)
409 bool?)
410 AssignmentStmt:4(
411 NameExpr(a)
412 NameExpr(None)
413 Any?))))
414
415 [case testFunctionDefWithType]
416 def f(y: str) -> int:
417 return
418 class A:
419 def f(self, a: int, b: Any) -> x:
420 pass
421 def g(self) -> Any:
422 pass
423 [out]
424 MypyFile:1(
425 FuncDef:1(
426 f
427 Args(
428 Var(y))
429 def (y: str?) -> int?
430 Block:2(
431 ReturnStmt:2()))
432 ClassDef:3(
433 A
434 FuncDef:4(
435 f
436 Args(
437 Var(self)
438 Var(a)
439 Var(b))
440 def (self: Any, a: int?, b: Any?) -> x?
441 Block:5(
442 PassStmt:5()))
443 FuncDef:6(
444 g
445 Args(
446 Var(self))
447 def (self: Any) -> Any?
448 Block:7(
449 PassStmt:7()))))
450
451 [case testFuncWithNoneReturn]
452 def f() -> None:
453 pass
454 [out]
455 MypyFile:1(
456 FuncDef:1(
457 f
458 def () -> None?
459 Block:2(
460 PassStmt:2())))
461
462 [case testVarDefWithGenericType]
463 x = None # type: List[str]
464 y = None # type: Dict[int, Any]
465 [out]
466 MypyFile:1(
467 AssignmentStmt:1(
468 NameExpr(x)
469 NameExpr(None)
470 List?[str?])
471 AssignmentStmt:2(
472 NameExpr(y)
473 NameExpr(None)
474 Dict?[int?, Any?]))
475
476 [case testSignatureWithGenericTypes]
477 def f(y: t[Any, x]) -> a[b[c], d]:
478 pass
479 [out]
480 MypyFile:1(
481 FuncDef:1(
482 f
483 Args(
484 Var(y))
485 def (y: t?[Any?, x?]) -> a?[b?[c?], d?]
486 Block:2(
487 PassStmt:2())))
488
489 [case testParsingExpressionsWithLessAndGreaterThan]
490 # The operators < > can sometimes be confused with generic types.
491 x = a < b > c
492 f(x < b, y > c)
493 a < b > 1
494 x < b, y > 2
495 (a < b > c)
496 [out]
497 MypyFile:1(
498 AssignmentStmt:2(
499 NameExpr(x)
500 ComparisonExpr:2(
501 <
502 >
503 NameExpr(a)
504 NameExpr(b)
505 NameExpr(c)))
506 ExpressionStmt:3(
507 CallExpr:3(
508 NameExpr(f)
509 Args(
510 ComparisonExpr:3(
511 <
512 NameExpr(x)
513 NameExpr(b))
514 ComparisonExpr:3(
515 >
516 NameExpr(y)
517 NameExpr(c)))))
518 ExpressionStmt:4(
519 ComparisonExpr:4(
520 <
521 >
522 NameExpr(a)
523 NameExpr(b)
524 IntExpr(1)))
525 ExpressionStmt:5(
526 TupleExpr:5(
527 ComparisonExpr:5(
528 <
529 NameExpr(x)
530 NameExpr(b))
531 ComparisonExpr:5(
532 >
533 NameExpr(y)
534 IntExpr(2))))
535 ExpressionStmt:6(
536 ComparisonExpr:6(
537 <
538 >
539 NameExpr(a)
540 NameExpr(b)
541 NameExpr(c))))
542
543 [case testLineContinuation]
544 if (1 +
545 2):
546 pass
547 [out]
548 MypyFile:1(
549 IfStmt:1(
550 If(
551 OpExpr:1(
552 +
553 IntExpr(1)
554 IntExpr(2)))
555 Then(
556 PassStmt:3())))
557
558 [case testMultipleVarDef]
559 x, y = z # type: int, a[c]
560 [out]
561 MypyFile:1(
562 AssignmentStmt:1(
563 TupleExpr:1(
564 NameExpr(x)
565 NameExpr(y))
566 NameExpr(z)
567 tuple[int?, a?[c?]]))
568
569 [case testMultipleVarDef2]
570 (xx, z, i) = 1 # type: (a[c], Any, int)
571 [out]
572 MypyFile:1(
573 AssignmentStmt:1(
574 TupleExpr:1(
575 NameExpr(xx)
576 NameExpr(z)
577 NameExpr(i))
578 IntExpr(1)
579 tuple[a?[c?], Any?, int?]))
580
581 [case testMultipleVarDef3]
582 (xx, (z, i)) = 1 # type: (a[c], (Any, int))
583 [out]
584 MypyFile:1(
585 AssignmentStmt:1(
586 TupleExpr:1(
587 NameExpr(xx)
588 TupleExpr:1(
589 NameExpr(z)
590 NameExpr(i)))
591 IntExpr(1)
592 tuple[a?[c?], tuple[Any?, int?]]))
593
594 [case testAnnotateAssignmentViaSelf]
595 class A:
596 def __init__(self):
597 self.x = 1 # type: int
598 [out]
599 MypyFile:1(
600 ClassDef:1(
601 A
602 FuncDef:2(
603 __init__
604 Args(
605 Var(self))
606 Block:3(
607 AssignmentStmt:3(
608 MemberExpr:3(
609 NameExpr(self)
610 x)
611 IntExpr(1)
612 int?)))))
613
614 [case testCommentAfterTypeComment]
615 x = 0 # type: int # bar!
616 [out]
617 MypyFile:1(
618 AssignmentStmt:1(
619 NameExpr(x)
620 IntExpr(0)
621 int?))
622
623 [case testMultilineAssignmentAndAnnotations]
624 (x,
625 y) = (1,
626 2) # type: foo, bar
627 [out]
628 MypyFile:1(
629 AssignmentStmt:1(
630 TupleExpr:1(
631 NameExpr(x)
632 NameExpr(y))
633 TupleExpr:2(
634 IntExpr(1)
635 IntExpr(2))
636 tuple[foo?, bar?]))
637
638 [case testWhitespaceAndCommentAnnotation]
639 x = 1#type:int
640 [out]
641 MypyFile:1(
642 AssignmentStmt:1(
643 NameExpr(x)
644 IntExpr(1)
645 int?))
646
647 [case testWhitespaceAndCommentAnnotation2]
648 x = 1# type: int
649 [out]
650 MypyFile:1(
651 AssignmentStmt:1(
652 NameExpr(x)
653 IntExpr(1)
654 int?))
655
656 [case testWhitespaceAndCommentAnnotation3]
657 x = 1# type : int # not recognized!
658 [out]
659 MypyFile:1(
660 AssignmentStmt:1(
661 NameExpr(x)
662 IntExpr(1)))
663
664 [case testInvalidAnnotation]
665 x=1 ##type: int
666 y=1 #.type: int
667 z=1 # Type: int
668 [out]
669 MypyFile:1(
670 AssignmentStmt:1(
671 NameExpr(x)
672 IntExpr(1))
673 AssignmentStmt:2(
674 NameExpr(y)
675 IntExpr(1))
676 AssignmentStmt:3(
677 NameExpr(z)
678 IntExpr(1)))
679
680 [case testEmptyClass]
681 class C:
682 pass
683 [out]
684 MypyFile:1(
685 ClassDef:1(
686 C
687 PassStmt:2()))
688
689 [case testOperatorPrecedence]
690 a | b ^ c
691 a & b << c
692 [out]
693 MypyFile:1(
694 ExpressionStmt:1(
695 OpExpr:1(
696 |
697 NameExpr(a)
698 OpExpr:1(
699 ^
700 NameExpr(b)
701 NameExpr(c))))
702 ExpressionStmt:2(
703 OpExpr:2(
704 &
705 NameExpr(a)
706 OpExpr:2(
707 <<
708 NameExpr(b)
709 NameExpr(c)))))
710
711 [case testOperatorAssociativity]
712 1 - 2 + 3
713 1 << 2 << 3
714 [out]
715 MypyFile:1(
716 ExpressionStmt:1(
717 OpExpr:1(
718 +
719 OpExpr:1(
720 -
721 IntExpr(1)
722 IntExpr(2))
723 IntExpr(3)))
724 ExpressionStmt:2(
725 OpExpr:2(
726 <<
727 OpExpr:2(
728 <<
729 IntExpr(1)
730 IntExpr(2))
731 IntExpr(3))))
732
733 [case testUnaryOperators]
734 -2 * +3 * ~3 * 2
735 ~3**2
736 [out]
737 MypyFile:1(
738 ExpressionStmt:1(
739 OpExpr:1(
740 *
741 OpExpr:1(
742 *
743 OpExpr:1(
744 *
745 UnaryExpr:1(
746 -
747 IntExpr(2))
748 UnaryExpr:1(
749 +
750 IntExpr(3)))
751 UnaryExpr:1(
752 ~
753 IntExpr(3)))
754 IntExpr(2)))
755 ExpressionStmt:2(
756 UnaryExpr:2(
757 ~
758 OpExpr:2(
759 **
760 IntExpr(3)
761 IntExpr(2)))))
762
763 [case testSingleLineBodies]
764 if 1: pass
765 while 1: pass
766 def f(): pass
767 def g() -> int: return 1
768 [out]
769 MypyFile:1(
770 IfStmt:1(
771 If(
772 IntExpr(1))
773 Then(
774 PassStmt:1()))
775 WhileStmt:2(
776 IntExpr(1)
777 Block:2(
778 PassStmt:2()))
779 FuncDef:3(
780 f
781 Block:3(
782 PassStmt:3()))
783 FuncDef:4(
784 g
785 def () -> int?
786 Block:4(
787 ReturnStmt:4(
788 IntExpr(1)))))
789
790 [case testForStatement]
791 for x in y:
792 pass
793 for x, (y, w) in z:
794 1
795 for [x, (y, w)] in z:
796 1
797 [out]
798 MypyFile:1(
799 ForStmt:1(
800 NameExpr(x)
801 NameExpr(y)
802 Block:2(
803 PassStmt:2()))
804 ForStmt:3(
805 TupleExpr:3(
806 NameExpr(x)
807 TupleExpr:3(
808 NameExpr(y)
809 NameExpr(w)))
810 NameExpr(z)
811 Block:4(
812 ExpressionStmt:4(
813 IntExpr(1))))
814 ForStmt:5(
815 TupleExpr:5(
816 NameExpr(x)
817 TupleExpr:5(
818 NameExpr(y)
819 NameExpr(w)))
820 NameExpr(z)
821 Block:6(
822 ExpressionStmt:6(
823 IntExpr(1)))))
824
825 [case testGlobalDecl]
826 global x
827 def f():
828 global x, y
829 [out]
830 MypyFile:1(
831 GlobalDecl:1(
832 x)
833 FuncDef:2(
834 f
835 Block:3(
836 GlobalDecl:3(
837 x
838 y))))
839
840 [case testNonlocalDecl]
841 def f():
842 def g():
843 nonlocal x, y
844 [out]
845 MypyFile:1(
846 FuncDef:1(
847 f
848 Block:2(
849 FuncDef:2(
850 g
851 Block:3(
852 NonlocalDecl:3(
853 x
854 y))))))
855
856 [case testRaiseStatement]
857 raise foo
858 [out]
859 MypyFile:1(
860 RaiseStmt:1(
861 NameExpr(foo)))
862
863 [case testRaiseWithoutArg]
864 try:
865 pass
866 except:
867 raise
868 [out]
869 MypyFile:1(
870 TryStmt:1(
871 Block:2(
872 PassStmt:2())
873 Block:4(
874 RaiseStmt:4())))
875
876 [case testRaiseFrom]
877 raise e from x
878 [out]
879 MypyFile:1(
880 RaiseStmt:1(
881 NameExpr(e)
882 NameExpr(x)))
883
884 [case testBaseclasses]
885 class A(B):
886 pass
887 class A(B[T], C[Any, d[x]]):
888 pass
889 [out]
890 MypyFile:1(
891 ClassDef:1(
892 A
893 BaseTypeExpr(
894 NameExpr(B))
895 PassStmt:2())
896 ClassDef:3(
897 A
898 BaseTypeExpr(
899 IndexExpr:3(
900 NameExpr(B)
901 NameExpr(T))
902 IndexExpr:3(
903 NameExpr(C)
904 TupleExpr:3(
905 NameExpr(Any)
906 IndexExpr:3(
907 NameExpr(d)
908 NameExpr(x)))))
909 PassStmt:4()))
910
911 [case testIsNot]
912 x is not y
913 [out]
914 MypyFile:1(
915 ExpressionStmt:1(
916 ComparisonExpr:1(
917 is not
918 NameExpr(x)
919 NameExpr(y))))
920
921 [case testNotIn]
922 x not in y
923 not x not in y
924 x not in y | z
925 [out]
926 MypyFile:1(
927 ExpressionStmt:1(
928 ComparisonExpr:1(
929 not in
930 NameExpr(x)
931 NameExpr(y)))
932 ExpressionStmt:2(
933 UnaryExpr:2(
934 not
935 ComparisonExpr:2(
936 not in
937 NameExpr(x)
938 NameExpr(y))))
939 ExpressionStmt:3(
940 ComparisonExpr:3(
941 not in
942 NameExpr(x)
943 OpExpr:3(
944 |
945 NameExpr(y)
946 NameExpr(z)))))
947
948 [case testNotAsBinaryOp]
949 x not y
950 [out]
951 main:1: error: Invalid syntax
952 [out version==3.10.0]
953 main:1: error: Invalid syntax. Perhaps you forgot a comma?
954
955 [case testNotIs]
956 x not is y # E: Invalid syntax
957 [out]
958
959 [case testBinaryNegAsBinaryOp]
960 1 ~ 2
961 [out]
962 main:1: error: Invalid syntax
963 [out version==3.10.0]
964 main:1: error: Invalid syntax. Perhaps you forgot a comma?
965
966 [case testSliceInList]
967 x = [1, 2][1:2]
968 [out]
969 MypyFile:1(
970 AssignmentStmt:1(
971 NameExpr(x)
972 IndexExpr:1(
973 ListExpr:1(
974 IntExpr(1)
975 IntExpr(2))
976 SliceExpr:1(
977 IntExpr(1)
978 IntExpr(2)))))
979
980 [case testDictionaryExpression]
981 {}
982 {1:x}
983 {1:x, 2 or 1:2 and 3}
984 [out]
985 MypyFile:1(
986 ExpressionStmt:1(
987 DictExpr:1())
988 ExpressionStmt:2(
989 DictExpr:2(
990 IntExpr(1)
991 NameExpr(x)))
992 ExpressionStmt:3(
993 DictExpr:3(
994 IntExpr(1)
995 NameExpr(x)
996 OpExpr:3(
997 or
998 IntExpr(2)
999 IntExpr(1))
1000 OpExpr:3(
1001 and
1002 IntExpr(2)
1003 IntExpr(3)))))
1004
1005 [case testImport]
1006 import x
1007 import y.z.foo, __foo__.bar
1008 [out]
1009 MypyFile:1(
1010 Import:1(x)
1011 Import:2(y.z.foo, __foo__.bar))
1012
1013 [case testVariableTypeWithQualifiedName]
1014 x = None # type: x.y
1015 [out]
1016 MypyFile:1(
1017 AssignmentStmt:1(
1018 NameExpr(x)
1019 NameExpr(None)
1020 x.y?))
1021
1022 [case testTypeInSignatureWithQualifiedName]
1023 def f() -> x.y[a.b.c]: pass
1024 [out]
1025 MypyFile:1(
1026 FuncDef:1(
1027 f
1028 def () -> x.y?[a.b.c?]
1029 Block:1(
1030 PassStmt:1())))
1031
1032 [case testImportFrom]
1033 from m import x
1034 from m.n import x, y, z
1035 [out]
1036 MypyFile:1(
1037 ImportFrom:1(m, [x])
1038 ImportFrom:2(m.n, [x, y, z]))
1039
1040 [case testImportFromAs]
1041 from m import x as y
1042 from x import y, z as a, c as c
1043 [out]
1044 MypyFile:1(
1045 ImportFrom:1(m, [x : y])
1046 ImportFrom:2(x, [y, z : a, c : c]))
1047
1048 [case testImportStar]
1049 from x import *
1050 [out]
1051 MypyFile:1(
1052 ImportAll:1(x))
1053
1054 [case testImportsInDifferentPlaces]
1055 1
1056 import x
1057 def f():
1058 from x import y
1059 from z import *
1060 [out]
1061 MypyFile:1(
1062 ExpressionStmt:1(
1063 IntExpr(1))
1064 Import:2(x)
1065 FuncDef:3(
1066 f
1067 Block:4(
1068 ImportFrom:4(x, [y])
1069 ImportAll:5(z))))
1070
1071 [case testImportWithExtraComma]
1072 from x import (y, z,)
1073 [out]
1074 MypyFile:1(
1075 ImportFrom:1(x, [y, z]))
1076
1077 [case testDefaultArgs]
1078 def f(x=1):
1079 pass
1080 def g(x, y=1+2, z=(1, 2)):
1081 pass
1082 [out]
1083 MypyFile:1(
1084 FuncDef:1(
1085 f
1086 Args(
1087 default(
1088 Var(x)
1089 IntExpr(1)))
1090 Block:2(
1091 PassStmt:2()))
1092 FuncDef:3(
1093 g
1094 Args(
1095 Var(x)
1096 default(
1097 Var(y)
1098 OpExpr:3(
1099 +
1100 IntExpr(1)
1101 IntExpr(2)))
1102 default(
1103 Var(z)
1104 TupleExpr:3(
1105 IntExpr(1)
1106 IntExpr(2))))
1107 Block:4(
1108 PassStmt:4())))
1109
1110 [case testTryFinally]
1111 try:
1112 1
1113 finally:
1114 2
1115 [out]
1116 MypyFile:1(
1117 TryStmt:1(
1118 Block:2(
1119 ExpressionStmt:2(
1120 IntExpr(1)))
1121 Finally(
1122 ExpressionStmt:4(
1123 IntExpr(2)))))
1124
1125 [case testTry]
1126 try:
1127 1
1128 except x:
1129 2
1130 [out]
1131 MypyFile:1(
1132 TryStmt:1(
1133 Block:2(
1134 ExpressionStmt:2(
1135 IntExpr(1)))
1136 NameExpr(x)
1137 Block:4(
1138 ExpressionStmt:4(
1139 IntExpr(2)))))
1140
1141 [case testComplexTry]
1142 try:
1143 1
1144 except x as y:
1145 2
1146 except x.y:
1147 3
1148 [out]
1149 MypyFile:1(
1150 TryStmt:1(
1151 Block:2(
1152 ExpressionStmt:2(
1153 IntExpr(1)))
1154 NameExpr(x)
1155 NameExpr(y)
1156 Block:4(
1157 ExpressionStmt:4(
1158 IntExpr(2)))
1159 MemberExpr:5(
1160 NameExpr(x)
1161 y)
1162 Block:6(
1163 ExpressionStmt:6(
1164 IntExpr(3)))))
1165
1166 [case testGeneratorExpression]
1167 (x for y in z)
1168 [out]
1169 MypyFile:1(
1170 ExpressionStmt:1(
1171 GeneratorExpr:1(
1172 NameExpr(x)
1173 NameExpr(y)
1174 NameExpr(z))))
1175
1176 [case testGeneratorExpressionNested]
1177 (x for y, (p, q) in z)
1178 [out]
1179 MypyFile:1(
1180 ExpressionStmt:1(
1181 GeneratorExpr:1(
1182 NameExpr(x)
1183 TupleExpr:1(
1184 NameExpr(y)
1185 TupleExpr:1(
1186 NameExpr(p)
1187 NameExpr(q)))
1188 NameExpr(z))))
1189
1190 [case testListComprehension]
1191 x=[x for y in z]
1192 [out]
1193 MypyFile:1(
1194 AssignmentStmt:1(
1195 NameExpr(x)
1196 ListComprehension:1(
1197 GeneratorExpr:1(
1198 NameExpr(x)
1199 NameExpr(y)
1200 NameExpr(z)))))
1201
1202 [case testComplexListComprehension]
1203 x=[(x, y) for y, z in (1, 2)]
1204 [out]
1205 MypyFile:1(
1206 AssignmentStmt:1(
1207 NameExpr(x)
1208 ListComprehension:1(
1209 GeneratorExpr:1(
1210 TupleExpr:1(
1211 NameExpr(x)
1212 NameExpr(y))
1213 TupleExpr:1(
1214 NameExpr(y)
1215 NameExpr(z))
1216 TupleExpr:1(
1217 IntExpr(1)
1218 IntExpr(2))))))
1219
1220 [case testListComprehension2]
1221 ([x + 1 for x in a])
1222 [out]
1223 MypyFile:1(
1224 ExpressionStmt:1(
1225 ListComprehension:1(
1226 GeneratorExpr:1(
1227 OpExpr:1(
1228 +
1229 NameExpr(x)
1230 IntExpr(1))
1231 NameExpr(x)
1232 NameExpr(a)))))
1233
1234 [case testSlices]
1235 x[1:2]
1236 x[:1]
1237 x[1:]
1238 x[:]
1239 [out]
1240 MypyFile:1(
1241 ExpressionStmt:1(
1242 IndexExpr:1(
1243 NameExpr(x)
1244 SliceExpr:1(
1245 IntExpr(1)
1246 IntExpr(2))))
1247 ExpressionStmt:2(
1248 IndexExpr:2(
1249 NameExpr(x)
1250 SliceExpr:2(
1251 <empty>
1252 IntExpr(1))))
1253 ExpressionStmt:3(
1254 IndexExpr:3(
1255 NameExpr(x)
1256 SliceExpr:3(
1257 IntExpr(1)
1258 <empty>)))
1259 ExpressionStmt:4(
1260 IndexExpr:4(
1261 NameExpr(x)
1262 SliceExpr:4(
1263 <empty>
1264 <empty>))))
1265
1266 [case testSliceWithStride]
1267 x[1:2:3]
1268 x[1::2]
1269 x[:1:2]
1270 x[::2]
1271 x[1:2:]
1272 [out]
1273 MypyFile:1(
1274 ExpressionStmt:1(
1275 IndexExpr:1(
1276 NameExpr(x)
1277 SliceExpr:1(
1278 IntExpr(1)
1279 IntExpr(2)
1280 IntExpr(3))))
1281 ExpressionStmt:2(
1282 IndexExpr:2(
1283 NameExpr(x)
1284 SliceExpr:2(
1285 IntExpr(1)
1286 <empty>
1287 IntExpr(2))))
1288 ExpressionStmt:3(
1289 IndexExpr:3(
1290 NameExpr(x)
1291 SliceExpr:3(
1292 <empty>
1293 IntExpr(1)
1294 IntExpr(2))))
1295 ExpressionStmt:4(
1296 IndexExpr:4(
1297 NameExpr(x)
1298 SliceExpr:4(
1299 <empty>
1300 <empty>
1301 IntExpr(2))))
1302 ExpressionStmt:5(
1303 IndexExpr:5(
1304 NameExpr(x)
1305 SliceExpr:5(
1306 IntExpr(1)
1307 IntExpr(2)))))
1308
1309 [case testYield]
1310 def f():
1311 yield x + 1
1312 [out]
1313 MypyFile:1(
1314 FuncDef:1(
1315 f
1316 Block:2(
1317 ExpressionStmt:2(
1318 YieldExpr:2(
1319 OpExpr:2(
1320 +
1321 NameExpr(x)
1322 IntExpr(1)))))))
1323
1324 [case testYieldFrom]
1325 def f():
1326 yield from h()
1327 [out]
1328 MypyFile:1(
1329 FuncDef:1(
1330 f
1331 Block:2(
1332 ExpressionStmt:2(
1333 YieldFromExpr:2(
1334 CallExpr:2(
1335 NameExpr(h)
1336 Args()))))))
1337
1338 [case testYieldFromAssignment]
1339 def f():
1340 a = yield from h()
1341 [out]
1342 MypyFile:1(
1343 FuncDef:1(
1344 f
1345 Block:2(
1346 AssignmentStmt:2(
1347 NameExpr(a)
1348 YieldFromExpr:2(
1349 CallExpr:2(
1350 NameExpr(h)
1351 Args()))))))
1352
1353 [case testDel]
1354 del x
1355 del x[0], y[1]
1356 [out]
1357 MypyFile:1(
1358 DelStmt:1(
1359 NameExpr(x))
1360 DelStmt:2(
1361 TupleExpr:2(
1362 IndexExpr:2(
1363 NameExpr(x)
1364 IntExpr(0))
1365 IndexExpr:2(
1366 NameExpr(y)
1367 IntExpr(1)))))
1368
1369 [case testExtraCommas]
1370 1, 2,
1371 +[1, 2,]
1372 f(1,)
1373 {1:2,}
1374 [out]
1375 MypyFile:1(
1376 ExpressionStmt:1(
1377 TupleExpr:1(
1378 IntExpr(1)
1379 IntExpr(2)))
1380 ExpressionStmt:2(
1381 UnaryExpr:2(
1382 +
1383 ListExpr:2(
1384 IntExpr(1)
1385 IntExpr(2))))
1386 ExpressionStmt:3(
1387 CallExpr:3(
1388 NameExpr(f)
1389 Args(
1390 IntExpr(1))))
1391 ExpressionStmt:4(
1392 DictExpr:4(
1393 IntExpr(1)
1394 IntExpr(2))))
1395
1396 [case testExtraCommaInFunc]
1397 def f(x,):
1398 pass
1399 [out]
1400 MypyFile:1(
1401 FuncDef:1(
1402 f
1403 Args(
1404 Var(x))
1405 Block:2(
1406 PassStmt:2())))
1407
1408 [case testLambda]
1409 lambda: 1
1410 lambda x: y + 1
1411 lambda x, y: 1
1412 [out]
1413 MypyFile:1(
1414 ExpressionStmt:1(
1415 LambdaExpr:1(
1416 Block:1(
1417 ReturnStmt:1(
1418 IntExpr(1)))))
1419 ExpressionStmt:2(
1420 LambdaExpr:2(
1421 Args(
1422 Var(x))
1423 Block:2(
1424 ReturnStmt:2(
1425 OpExpr:2(
1426 +
1427 NameExpr(y)
1428 IntExpr(1))))))
1429 ExpressionStmt:3(
1430 LambdaExpr:3(
1431 Args(
1432 Var(x)
1433 Var(y))
1434 Block:3(
1435 ReturnStmt:3(
1436 IntExpr(1))))))
1437
1438 [case testComplexLambda]
1439 lambda x=2: x
1440 [out]
1441 MypyFile:1(
1442 ExpressionStmt:1(
1443 LambdaExpr:1(
1444 Args(
1445 default(
1446 Var(x)
1447 IntExpr(2)))
1448 Block:1(
1449 ReturnStmt:1(
1450 NameExpr(x))))))
1451
1452 [case testLambdaPrecedence]
1453 lambda x: 1, 2
1454 [out]
1455 MypyFile:1(
1456 ExpressionStmt:1(
1457 TupleExpr:1(
1458 LambdaExpr:1(
1459 Args(
1460 Var(x))
1461 Block:1(
1462 ReturnStmt:1(
1463 IntExpr(1))))
1464 IntExpr(2))))
1465
1466 [case testForIndicesInParens]
1467 for (i, j) in x:
1468 pass
1469 [out]
1470 MypyFile:1(
1471 ForStmt:1(
1472 TupleExpr:1(
1473 NameExpr(i)
1474 NameExpr(j))
1475 NameExpr(x)
1476 Block:2(
1477 PassStmt:2())))
1478
1479 [case testForAndTrailingCommaAfterIndexVar]
1480 for i, in x:
1481 pass
1482 [out]
1483 MypyFile:1(
1484 ForStmt:1(
1485 TupleExpr:1(
1486 NameExpr(i))
1487 NameExpr(x)
1488 Block:2(
1489 PassStmt:2())))
1490
1491 [case testListComprehensionAndTrailingCommaAfterIndexVar]
1492 x = [a for b, in c]
1493 [out]
1494 MypyFile:1(
1495 AssignmentStmt:1(
1496 NameExpr(x)
1497 ListComprehension:1(
1498 GeneratorExpr:1(
1499 NameExpr(a)
1500 TupleExpr:1(
1501 NameExpr(b))
1502 NameExpr(c)))))
1503
1504 [case testForAndTrailingCommaAfterIndexVars]
1505 for i, j, in x:
1506 pass
1507 [out]
1508 MypyFile:1(
1509 ForStmt:1(
1510 TupleExpr:1(
1511 NameExpr(i)
1512 NameExpr(j))
1513 NameExpr(x)
1514 Block:2(
1515 PassStmt:2())))
1516
1517 [case testGeneratorWithCondition]
1518 (x for y in z if 0)
1519 [out]
1520 MypyFile:1(
1521 ExpressionStmt:1(
1522 GeneratorExpr:1(
1523 NameExpr(x)
1524 NameExpr(y)
1525 NameExpr(z)
1526 IntExpr(0))))
1527
1528 [case testListComprehensionWithCondition]
1529 raise [x for y in z if 0]
1530 [out]
1531 MypyFile:1(
1532 RaiseStmt:1(
1533 ListComprehension:1(
1534 GeneratorExpr:1(
1535 NameExpr(x)
1536 NameExpr(y)
1537 NameExpr(z)
1538 IntExpr(0)))))
1539
1540 [case testListComprehensionWithConditions]
1541 raise [x for y in z if 0 if 1]
1542 [out]
1543 MypyFile:1(
1544 RaiseStmt:1(
1545 ListComprehension:1(
1546 GeneratorExpr:1(
1547 NameExpr(x)
1548 NameExpr(y)
1549 NameExpr(z)
1550 IntExpr(0)
1551 IntExpr(1)))))
1552
1553 [case testListComprehensionWithCrazyConditions]
1554 raise [x for y in z if (1 if 2 else 3) if 1]
1555 [out]
1556 MypyFile:1(
1557 RaiseStmt:1(
1558 ListComprehension:1(
1559 GeneratorExpr:1(
1560 NameExpr(x)
1561 NameExpr(y)
1562 NameExpr(z)
1563 ConditionalExpr:1(
1564 Condition(
1565 IntExpr(2))
1566 IntExpr(1)
1567 IntExpr(3))
1568 IntExpr(1)))))
1569
1570 [case testDictionaryComprehension]
1571 a = {x: y for x, y in xys}
1572 [out]
1573 MypyFile:1(
1574 AssignmentStmt:1(
1575 NameExpr(a)
1576 DictionaryComprehension:1(
1577 NameExpr(x)
1578 NameExpr(y)
1579 TupleExpr:1(
1580 NameExpr(x)
1581 NameExpr(y))
1582 NameExpr(xys))))
1583
1584 [case testDictionaryComprehensionComplex]
1585 a = {x: y for x, y in xys for p, q in pqs if c}
1586 [out]
1587 MypyFile:1(
1588 AssignmentStmt:1(
1589 NameExpr(a)
1590 DictionaryComprehension:1(
1591 NameExpr(x)
1592 NameExpr(y)
1593 TupleExpr:1(
1594 NameExpr(x)
1595 NameExpr(y))
1596 TupleExpr:1(
1597 NameExpr(p)
1598 NameExpr(q))
1599 NameExpr(xys)
1600 NameExpr(pqs)
1601 NameExpr(c))))
1602
1603 [case testSetComprehension]
1604 a = {i for i in l}
1605 [out]
1606 MypyFile:1(
1607 AssignmentStmt:1(
1608 NameExpr(a)
1609 SetComprehension:1(
1610 GeneratorExpr:1(
1611 NameExpr(i)
1612 NameExpr(i)
1613 NameExpr(l)))))
1614
1615 [case testSetComprehensionComplex]
1616 a = {x + p for x in xys for p in pqs if c}
1617 [out]
1618 MypyFile:1(
1619 AssignmentStmt:1(
1620 NameExpr(a)
1621 SetComprehension:1(
1622 GeneratorExpr:1(
1623 OpExpr:1(
1624 +
1625 NameExpr(x)
1626 NameExpr(p))
1627 NameExpr(x)
1628 NameExpr(p)
1629 NameExpr(xys)
1630 NameExpr(pqs)
1631 NameExpr(c)))))
1632
1633 [case testWithStatement]
1634 with open('foo') as f:
1635 pass
1636 [out]
1637 MypyFile:1(
1638 WithStmt:1(
1639 Expr(
1640 CallExpr:1(
1641 NameExpr(open)
1642 Args(
1643 StrExpr(foo))))
1644 Target(
1645 NameExpr(f))
1646 Block:2(
1647 PassStmt:2())))
1648
1649 [case testWithStatementWithoutTarget]
1650 with foo:
1651 pass
1652 [out]
1653 MypyFile:1(
1654 WithStmt:1(
1655 Expr(
1656 NameExpr(foo))
1657 Block:2(
1658 PassStmt:2())))
1659
1660 [case testHexOctBinLiterals]
1661 0xa, 0Xaf, 0o7, 0O12, 0b1, 0B101
1662 [out]
1663 MypyFile:1(
1664 ExpressionStmt:1(
1665 TupleExpr:1(
1666 IntExpr(10)
1667 IntExpr(175)
1668 IntExpr(7)
1669 IntExpr(10)
1670 IntExpr(1)
1671 IntExpr(5))))
1672
1673 [case testImportFromWithParens]
1674 from x import (y)
1675 from x import (y,
1676 z)
1677 [out]
1678 MypyFile:1(
1679 ImportFrom:1(x, [y])
1680 ImportFrom:2(x, [y, z]))
1681
1682 [case testContinueStmt]
1683 while 1:
1684 continue
1685 [out]
1686 MypyFile:1(
1687 WhileStmt:1(
1688 IntExpr(1)
1689 Block:2(
1690 ContinueStmt:2())))
1691
1692 [case testStrLiteralConcatenate]
1693 'f' 'bar'
1694 ('x'
1695 'y'
1696 'z')
1697 [out]
1698 MypyFile:1(
1699 ExpressionStmt:1(
1700 StrExpr(fbar))
1701 ExpressionStmt:2(
1702 StrExpr(xyz)))
1703
1704 [case testCatchAllExcept]
1705 try:
1706 1
1707 except:
1708 pass
1709 try:
1710 1
1711 except x:
1712 pass
1713 except:
1714 2
1715 [out]
1716 MypyFile:1(
1717 TryStmt:1(
1718 Block:2(
1719 ExpressionStmt:2(
1720 IntExpr(1)))
1721 Block:4(
1722 PassStmt:4()))
1723 TryStmt:5(
1724 Block:6(
1725 ExpressionStmt:6(
1726 IntExpr(1)))
1727 NameExpr(x)
1728 Block:8(
1729 PassStmt:8())
1730 Block:10(
1731 ExpressionStmt:10(
1732 IntExpr(2)))))
1733
1734 [case testTryElse]
1735 try:
1736 pass
1737 except x:
1738 1
1739 else:
1740 2
1741 [out]
1742 MypyFile:1(
1743 TryStmt:1(
1744 Block:2(
1745 PassStmt:2())
1746 NameExpr(x)
1747 Block:4(
1748 ExpressionStmt:4(
1749 IntExpr(1)))
1750 Else(
1751 ExpressionStmt:6(
1752 IntExpr(2)))))
1753
1754 [case testExceptWithMultipleTypes]
1755 try:
1756 pass
1757 except (x, y):
1758 pass
1759 except (a, b, c) as e:
1760 pass
1761 [out]
1762 MypyFile:1(
1763 TryStmt:1(
1764 Block:2(
1765 PassStmt:2())
1766 TupleExpr:3(
1767 NameExpr(x)
1768 NameExpr(y))
1769 Block:4(
1770 PassStmt:4())
1771 TupleExpr:5(
1772 NameExpr(a)
1773 NameExpr(b)
1774 NameExpr(c))
1775 NameExpr(e)
1776 Block:6(
1777 PassStmt:6())))
1778
1779 [case testNestedFunctions]
1780 def f():
1781 def g():
1782 pass
1783 def h() -> int:
1784 def g() -> int:
1785 pass
1786 [out]
1787 MypyFile:1(
1788 FuncDef:1(
1789 f
1790 Block:2(
1791 FuncDef:2(
1792 g
1793 Block:3(
1794 PassStmt:3()))))
1795 FuncDef:4(
1796 h
1797 def () -> int?
1798 Block:5(
1799 FuncDef:5(
1800 g
1801 def () -> int?
1802 Block:6(
1803 PassStmt:6())))))
1804
1805 [case testStatementsAndDocStringsInClassBody]
1806 class A:
1807 "doc string"
1808 x = y
1809 def f(self):
1810 pass
1811 [out]
1812 MypyFile:1(
1813 ClassDef:1(
1814 A
1815 ExpressionStmt:2(
1816 StrExpr(doc string))
1817 AssignmentStmt:3(
1818 NameExpr(x)
1819 NameExpr(y))
1820 FuncDef:4(
1821 f
1822 Args(
1823 Var(self))
1824 Block:5(
1825 PassStmt:5()))))
1826
1827 [case testSingleLineClass]
1828 class a: pass
1829 [out]
1830 MypyFile:1(
1831 ClassDef:1(
1832 a
1833 PassStmt:1()))
1834
1835 [case testDecorator]
1836 @property
1837 def f():
1838 pass
1839 [out]
1840 MypyFile:1(
1841 Decorator:1(
1842 Var(f)
1843 NameExpr(property)
1844 FuncDef:2(
1845 f
1846 Block:3(
1847 PassStmt:3()))))
1848
1849 [case testComplexDecorator]
1850 @foo(bar, 1)
1851 @zar
1852 def f() -> int:
1853 pass
1854 [out]
1855 MypyFile:1(
1856 Decorator:1(
1857 Var(f)
1858 CallExpr:1(
1859 NameExpr(foo)
1860 Args(
1861 NameExpr(bar)
1862 IntExpr(1)))
1863 NameExpr(zar)
1864 FuncDef:3(
1865 f
1866 def () -> int?
1867 Block:4(
1868 PassStmt:4()))))
1869
1870 [case testKeywordArgInCall]
1871 f(x=1)
1872 [out]
1873 MypyFile:1(
1874 ExpressionStmt:1(
1875 CallExpr:1(
1876 NameExpr(f)
1877 Args()
1878 KwArgs(
1879 x
1880 IntExpr(1)))))
1881
1882 [case testComplexKeywordArgs]
1883 f(x, y=1 or 2, z=y)
1884 [out]
1885 MypyFile:1(
1886 ExpressionStmt:1(
1887 CallExpr:1(
1888 NameExpr(f)
1889 Args(
1890 NameExpr(x))
1891 KwArgs(
1892 y
1893 OpExpr:1(
1894 or
1895 IntExpr(1)
1896 IntExpr(2)))
1897 KwArgs(
1898 z
1899 NameExpr(y)))))
1900
1901 [case testChainedAssignment]
1902 x = z = 1
1903 [out]
1904 MypyFile:1(
1905 AssignmentStmt:1(
1906 Lvalues(
1907 NameExpr(x)
1908 NameExpr(z))
1909 IntExpr(1)))
1910
1911 [case testVarArgs]
1912 def f(x, *a): pass
1913 f(1, *2)
1914 [out]
1915 MypyFile:1(
1916 FuncDef:1(
1917 f
1918 Args(
1919 Var(x))
1920 VarArg(
1921 Var(a))
1922 Block:1(
1923 PassStmt:1()))
1924 ExpressionStmt:2(
1925 CallExpr:2(
1926 NameExpr(f)
1927 Args(
1928 IntExpr(1)
1929 IntExpr(2))
1930 VarArg)))
1931
1932 [case testVarArgWithType]
1933 def f(x: str, *a: int): pass
1934 [out]
1935 MypyFile:1(
1936 FuncDef:1(
1937 f
1938 Args(
1939 Var(x))
1940 def (x: str?, *a: int?) -> Any
1941 VarArg(
1942 Var(a))
1943 Block:1(
1944 PassStmt:1())))
1945
1946 [case testDictVarArgs]
1947 def f(x, **a): pass
1948 [out]
1949 MypyFile:1(
1950 FuncDef:1(
1951 f
1952 Args(
1953 Var(x))
1954 DictVarArg(
1955 Var(a))
1956 Block:1(
1957 PassStmt:1())))
1958
1959 [case testBothVarArgs]
1960 def f(x, *a, **b): pass
1961 def g(*a, **b): pass
1962 [out]
1963 MypyFile:1(
1964 FuncDef:1(
1965 f
1966 Args(
1967 Var(x))
1968 VarArg(
1969 Var(a))
1970 DictVarArg(
1971 Var(b))
1972 Block:1(
1973 PassStmt:1()))
1974 FuncDef:2(
1975 g
1976 VarArg(
1977 Var(a))
1978 DictVarArg(
1979 Var(b))
1980 Block:2(
1981 PassStmt:2())))
1982
1983 [case testDictVarArgsWithType]
1984 def f(x: X, **a: A) -> None: pass
1985 [out]
1986 MypyFile:1(
1987 FuncDef:1(
1988 f
1989 Args(
1990 Var(x))
1991 def (x: X?, **a: A?) -> None?
1992 DictVarArg(
1993 Var(a))
1994 Block:1(
1995 PassStmt:1())))
1996
1997 [case testCallDictVarArgs]
1998 f(**x)
1999 f(x, **y)
2000 f(*x, **y)
2001 f(x, *y, **z)
2002 [out]
2003 MypyFile:1(
2004 ExpressionStmt:1(
2005 CallExpr:1(
2006 NameExpr(f)
2007 Args()
2008 DictVarArg(
2009 NameExpr(x))))
2010 ExpressionStmt:2(
2011 CallExpr:2(
2012 NameExpr(f)
2013 Args(
2014 NameExpr(x))
2015 DictVarArg(
2016 NameExpr(y))))
2017 ExpressionStmt:3(
2018 CallExpr:3(
2019 NameExpr(f)
2020 Args(
2021 NameExpr(x))
2022 VarArg
2023 DictVarArg(
2024 NameExpr(y))))
2025 ExpressionStmt:4(
2026 CallExpr:4(
2027 NameExpr(f)
2028 Args(
2029 NameExpr(x)
2030 NameExpr(y))
2031 VarArg
2032 DictVarArg(
2033 NameExpr(z)))))
2034
2035 [case testAssert]
2036 assert x == y
2037 [out]
2038 MypyFile:1(
2039 AssertStmt:1(
2040 ComparisonExpr:1(
2041 ==
2042 NameExpr(x)
2043 NameExpr(y))))
2044
2045 [case testYieldWithoutExpressions]
2046 def f():
2047 yield
2048 [out]
2049 MypyFile:1(
2050 FuncDef:1(
2051 f
2052 Block:2(
2053 ExpressionStmt:2(
2054 YieldExpr:2()))))
2055
2056 [case testConditionalExpression]
2057 x if y else z
2058 [out]
2059 MypyFile:1(
2060 ExpressionStmt:1(
2061 ConditionalExpr:1(
2062 Condition(
2063 NameExpr(y))
2064 NameExpr(x)
2065 NameExpr(z))))
2066
2067 [case testConditionalExpressionInListComprehension]
2068 a = [x if y else z for a in b]
2069 [out]
2070 MypyFile:1(
2071 AssignmentStmt:1(
2072 NameExpr(a)
2073 ListComprehension:1(
2074 GeneratorExpr:1(
2075 ConditionalExpr:1(
2076 Condition(
2077 NameExpr(y))
2078 NameExpr(x)
2079 NameExpr(z))
2080 NameExpr(a)
2081 NameExpr(b)))))
2082
2083 [case testConditionalExpressionInTuple]
2084 1 if 2 else 3, 4
2085 [out]
2086 MypyFile:1(
2087 ExpressionStmt:1(
2088 TupleExpr:1(
2089 ConditionalExpr:1(
2090 Condition(
2091 IntExpr(2))
2092 IntExpr(1)
2093 IntExpr(3))
2094 IntExpr(4))))
2095
2096 [case testSetLiteral]
2097 {x or y}
2098 {1, 2}
2099 [out]
2100 MypyFile:1(
2101 ExpressionStmt:1(
2102 SetExpr:1(
2103 OpExpr:1(
2104 or
2105 NameExpr(x)
2106 NameExpr(y))))
2107 ExpressionStmt:2(
2108 SetExpr:2(
2109 IntExpr(1)
2110 IntExpr(2))))
2111
2112 [case testSetLiteralWithExtraComma]
2113 {x,}
2114 [out]
2115 MypyFile:1(
2116 ExpressionStmt:1(
2117 SetExpr:1(
2118 NameExpr(x))))
2119
2120 [case testImportAs]
2121 import x as y
2122 import x, z as y, a.b as c, d as d
2123 [out]
2124 MypyFile:1(
2125 Import:1(x : y)
2126 Import:2(x, z : y, a.b : c, d : d))
2127
2128 [case testForAndElse]
2129 for x in y:
2130 pass
2131 else:
2132 x
2133 [out]
2134 MypyFile:1(
2135 ForStmt:1(
2136 NameExpr(x)
2137 NameExpr(y)
2138 Block:2(
2139 PassStmt:2())
2140 Else(
2141 ExpressionStmt:4(
2142 NameExpr(x)))))
2143
2144 [case testWhileAndElse]
2145 while x:
2146 pass
2147 else:
2148 y
2149 [out]
2150 MypyFile:1(
2151 WhileStmt:1(
2152 NameExpr(x)
2153 Block:2(
2154 PassStmt:2())
2155 Else(
2156 ExpressionStmt:4(
2157 NameExpr(y)))))
2158
2159 [case testWithAndMultipleOperands]
2160 with x as y, a as b:
2161 pass
2162 with x(), y():
2163 pass
2164 [out]
2165 MypyFile:1(
2166 WithStmt:1(
2167 Expr(
2168 NameExpr(x))
2169 Target(
2170 NameExpr(y))
2171 Expr(
2172 NameExpr(a))
2173 Target(
2174 NameExpr(b))
2175 Block:2(
2176 PassStmt:2()))
2177 WithStmt:3(
2178 Expr(
2179 CallExpr:3(
2180 NameExpr(x)
2181 Args()))
2182 Expr(
2183 CallExpr:3(
2184 NameExpr(y)
2185 Args()))
2186 Block:4(
2187 PassStmt:4())))
2188
2189 [case testOperatorAssignment]
2190 x += 1
2191 x -= 1
2192 x *= 1
2193 x /= 1
2194 x //= 1
2195 x %= 1
2196 x **= 1
2197 x |= 1
2198 x &= 1
2199 x ^= 1
2200 x >>= 1
2201 x <<= 1
2202 [out]
2203 MypyFile:1(
2204 OperatorAssignmentStmt:1(
2205 +
2206 NameExpr(x)
2207 IntExpr(1))
2208 OperatorAssignmentStmt:2(
2209 -
2210 NameExpr(x)
2211 IntExpr(1))
2212 OperatorAssignmentStmt:3(
2213 *
2214 NameExpr(x)
2215 IntExpr(1))
2216 OperatorAssignmentStmt:4(
2217 /
2218 NameExpr(x)
2219 IntExpr(1))
2220 OperatorAssignmentStmt:5(
2221 //
2222 NameExpr(x)
2223 IntExpr(1))
2224 OperatorAssignmentStmt:6(
2225 %
2226 NameExpr(x)
2227 IntExpr(1))
2228 OperatorAssignmentStmt:7(
2229 **
2230 NameExpr(x)
2231 IntExpr(1))
2232 OperatorAssignmentStmt:8(
2233 |
2234 NameExpr(x)
2235 IntExpr(1))
2236 OperatorAssignmentStmt:9(
2237 &
2238 NameExpr(x)
2239 IntExpr(1))
2240 OperatorAssignmentStmt:10(
2241 ^
2242 NameExpr(x)
2243 IntExpr(1))
2244 OperatorAssignmentStmt:11(
2245 >>
2246 NameExpr(x)
2247 IntExpr(1))
2248 OperatorAssignmentStmt:12(
2249 <<
2250 NameExpr(x)
2251 IntExpr(1)))
2252
2253 [case testNestedClasses]
2254 class A:
2255 class B:
2256 pass
2257 class C:
2258 pass
2259 [out]
2260 MypyFile:1(
2261 ClassDef:1(
2262 A
2263 ClassDef:2(
2264 B
2265 PassStmt:3())
2266 ClassDef:4(
2267 C
2268 PassStmt:5())))
2269
2270 [case testTryWithExceptAndFinally]
2271 try:
2272 pass
2273 except x:
2274 x
2275 finally:
2276 y
2277 [out]
2278 MypyFile:1(
2279 TryStmt:1(
2280 Block:2(
2281 PassStmt:2())
2282 NameExpr(x)
2283 Block:4(
2284 ExpressionStmt:4(
2285 NameExpr(x)))
2286 Finally(
2287 ExpressionStmt:6(
2288 NameExpr(y)))))
2289
2290 [case testBareAsteriskInFuncDef]
2291 def f(x, *, y=1): pass
2292 [out]
2293 MypyFile:1(
2294 FuncDef:1(
2295 f
2296 MaxPos(1)
2297 Args(
2298 Var(x)
2299 default(
2300 Var(y)
2301 IntExpr(1)))
2302 Block:1(
2303 PassStmt:1())))
2304
2305 [case testBareAsteriskInFuncDefWithSignature]
2306 def f(x: A, *, y: B = 1) -> None: pass
2307 [out]
2308 MypyFile:1(
2309 FuncDef:1(
2310 f
2311 MaxPos(1)
2312 Args(
2313 Var(x)
2314 default(
2315 Var(y)
2316 IntExpr(1)))
2317 def (x: A?, *, y: B? =) -> None?
2318 Block:1(
2319 PassStmt:1())))
2320
2321 [case testBareAsteriskNamedDefault]
2322 def f(*, y: B = 1) -> None: pass
2323 [out]
2324 MypyFile:1(
2325 FuncDef:1(
2326 f
2327 MaxPos(0)
2328 Args(
2329 default(
2330 Var(y)
2331 IntExpr(1)))
2332 def (*, y: B? =) -> None?
2333 Block:1(
2334 PassStmt:1())))
2335
2336 [case testBareAsteriskNamedNoDefault]
2337 def f(*, y: B) -> None: pass
2338 [out]
2339 MypyFile:1(
2340 FuncDef:1(
2341 f
2342 MaxPos(0)
2343 Args(
2344 Var(y))
2345 def (*, y: B?) -> None?
2346 Block:1(
2347 PassStmt:1())))
2348
2349 [case testSuperExpr]
2350 super().x
2351 [out]
2352 MypyFile:1(
2353 ExpressionStmt:1(
2354 SuperExpr:1(
2355 x
2356 CallExpr:1(
2357 NameExpr(super)
2358 Args()))))
2359
2360 [case testKeywordAndDictArgs]
2361 f(x = y, **kwargs)
2362 [out]
2363 MypyFile:1(
2364 ExpressionStmt:1(
2365 CallExpr:1(
2366 NameExpr(f)
2367 Args()
2368 KwArgs(
2369 x
2370 NameExpr(y))
2371 DictVarArg(
2372 NameExpr(kwargs)))))
2373
2374 [case testSimpleFunctionType]
2375 f = None # type: Callable[[], None]
2376 [out]
2377 MypyFile:1(
2378 AssignmentStmt:1(
2379 NameExpr(f)
2380 NameExpr(None)
2381 Callable?[<TypeList >, None?]))
2382
2383 [case testFunctionTypeWithArgument]
2384 f = None # type: Callable[[str], int]
2385 [out]
2386 MypyFile:1(
2387 AssignmentStmt:1(
2388 NameExpr(f)
2389 NameExpr(None)
2390 Callable?[<TypeList str?>, int?]))
2391
2392 [case testFunctionTypeWithTwoArguments]
2393 f = None # type: Callable[[a[b], x.y], List[int]]
2394 [out]
2395 MypyFile:1(
2396 AssignmentStmt:1(
2397 NameExpr(f)
2398 NameExpr(None)
2399 Callable?[<TypeList a?[b?], x.y?>, List?[int?]]))
2400
2401 [case testFunctionTypeWithExtraComma]
2402 def f(x: Callable[[str,], int]): pass
2403 [out]
2404 MypyFile:1(
2405 FuncDef:1(
2406 f
2407 Args(
2408 Var(x))
2409 def (x: Callable?[<TypeList str?>, int?]) -> Any
2410 Block:1(
2411 PassStmt:1())))
2412
2413 [case testSimpleStringLiteralType]
2414 def f() -> 'A': pass
2415 [out]
2416 MypyFile:1(
2417 FuncDef:1(
2418 f
2419 def () -> A?
2420 Block:1(
2421 PassStmt:1())))
2422
2423 [case testGenericStringLiteralType]
2424 def f() -> 'A[B, C]': pass
2425 [out]
2426 MypyFile:1(
2427 FuncDef:1(
2428 f
2429 def () -> A?[B?, C?]
2430 Block:1(
2431 PassStmt:1())))
2432
2433 [case testPartialStringLiteralType]
2434 def f() -> A['B', C]: pass
2435 [out]
2436 MypyFile:1(
2437 FuncDef:1(
2438 f
2439 def () -> A?[B?, C?]
2440 Block:1(
2441 PassStmt:1())))
2442
2443 [case testWhitespaceInStringLiteralType]
2444 def f() -> ' A [ X ] ': pass
2445 [out]
2446 MypyFile:1(
2447 FuncDef:1(
2448 f
2449 def () -> A?[X?]
2450 Block:1(
2451 PassStmt:1())))
2452
2453 [case testEscapeInStringLiteralType]
2454 def f() -> '\x41': pass
2455 [out]
2456 MypyFile:1(
2457 FuncDef:1(
2458 f
2459 def () -> A?
2460 Block:1(
2461 PassStmt:1())))
2462
2463 [case testBytesLiteralType]
2464 x: Literal[b'foo\xfe\x00']
2465 [out]
2466 MypyFile:1(
2467 AssignmentStmt:1(
2468 NameExpr(x)
2469 TempNode:1(
2470 Any)
2471 Literal?[b'foo\xfe\x00']))
2472
2473 [case testMetaclass]
2474 class Foo(metaclass=Bar): pass
2475 [out]
2476 MypyFile:1(
2477 ClassDef:1(
2478 Foo
2479 Keywords(metaclass=NameExpr(Bar))
2480 Metaclass(NameExpr(Bar))
2481 PassStmt:1()))
2482
2483 [case testQualifiedMetaclass]
2484 class Foo(metaclass=foo.Bar): pass
2485 [out]
2486 MypyFile:1(
2487 ClassDef:1(
2488 Foo
2489 Keywords(metaclass=MemberExpr:1(
2490 NameExpr(foo)
2491 Bar))
2492 Metaclass(MemberExpr:1(
2493 NameExpr(foo)
2494 Bar))
2495 PassStmt:1()))
2496
2497 [case testBaseAndMetaclass]
2498 class Foo(foo.bar[x], metaclass=Bar): pass
2499 [out]
2500 MypyFile:1(
2501 ClassDef:1(
2502 Foo
2503 Keywords(metaclass=NameExpr(Bar))
2504 Metaclass(NameExpr(Bar))
2505 BaseTypeExpr(
2506 IndexExpr:1(
2507 MemberExpr:1(
2508 NameExpr(foo)
2509 bar)
2510 NameExpr(x)))
2511 PassStmt:1()))
2512
2513 [case testClassKeywordArgs]
2514 class Foo(_root=None): pass
2515 [out]
2516 MypyFile:1(
2517 ClassDef:1(
2518 Foo
2519 Keywords(_root=NameExpr(None))
2520 PassStmt:1()))
2521
2522 [case testClassKeywordArgsBeforeMeta]
2523 class Foo(_root=None, metaclass=Bar): pass
2524 [out]
2525 MypyFile:1(
2526 ClassDef:1(
2527 Foo
2528 Keywords(_root=NameExpr(None), metaclass=NameExpr(Bar))
2529 Metaclass(NameExpr(Bar))
2530 PassStmt:1()))
2531
2532 [case testClassKeywordArgsAfterMeta]
2533 class Foo(metaclass=Bar, _root=None): pass
2534 [out]
2535 MypyFile:1(
2536 ClassDef:1(
2537 Foo
2538 Keywords(metaclass=NameExpr(Bar), _root=NameExpr(None))
2539 Metaclass(NameExpr(Bar))
2540 PassStmt:1()))
2541
2542 [case testNamesThatAreNoLongerKeywords]
2543 any = interface
2544 [out]
2545 MypyFile:1(
2546 AssignmentStmt:1(
2547 NameExpr(any)
2548 NameExpr(interface)))
2549
2550 [case testFunctionOverload]
2551 @overload
2552 def f() -> x: pass
2553 @overload
2554 def f() -> y: pass
2555 [out]
2556 MypyFile:1(
2557 OverloadedFuncDef:1(
2558 Decorator:1(
2559 Var(f)
2560 NameExpr(overload)
2561 FuncDef:2(
2562 f
2563 def () -> x?
2564 Block:2(
2565 PassStmt:2())))
2566 Decorator:3(
2567 Var(f)
2568 NameExpr(overload)
2569 FuncDef:4(
2570 f
2571 def () -> y?
2572 Block:4(
2573 PassStmt:4())))))
2574
2575 [case testFunctionOverloadAndOtherStatements]
2576 x
2577 @overload
2578 def f() -> x: pass
2579 @overload
2580 def f() -> y: pass
2581 x
2582 [out]
2583 MypyFile:1(
2584 ExpressionStmt:1(
2585 NameExpr(x))
2586 OverloadedFuncDef:2(
2587 Decorator:2(
2588 Var(f)
2589 NameExpr(overload)
2590 FuncDef:3(
2591 f
2592 def () -> x?
2593 Block:3(
2594 PassStmt:3())))
2595 Decorator:4(
2596 Var(f)
2597 NameExpr(overload)
2598 FuncDef:5(
2599 f
2600 def () -> y?
2601 Block:5(
2602 PassStmt:5()))))
2603 ExpressionStmt:6(
2604 NameExpr(x)))
2605
2606 [case testFunctionOverloadWithThreeVariants]
2607 @overload
2608 def f() -> x: pass
2609 @overload
2610 def f() -> y: pass
2611 @overload
2612 def f(y): pass
2613 [out]
2614 MypyFile:1(
2615 OverloadedFuncDef:1(
2616 Decorator:1(
2617 Var(f)
2618 NameExpr(overload)
2619 FuncDef:2(
2620 f
2621 def () -> x?
2622 Block:2(
2623 PassStmt:2())))
2624 Decorator:3(
2625 Var(f)
2626 NameExpr(overload)
2627 FuncDef:4(
2628 f
2629 def () -> y?
2630 Block:4(
2631 PassStmt:4())))
2632 Decorator:5(
2633 Var(f)
2634 NameExpr(overload)
2635 FuncDef:6(
2636 f
2637 Args(
2638 Var(y))
2639 Block:6(
2640 PassStmt:6())))))
2641
2642 [case testDecoratorsThatAreNotOverloads]
2643 @foo
2644 def f() -> x: pass
2645 @foo
2646 def g() -> y: pass
2647 [out]
2648 MypyFile:1(
2649 Decorator:1(
2650 Var(f)
2651 NameExpr(foo)
2652 FuncDef:2(
2653 f
2654 def () -> x?
2655 Block:2(
2656 PassStmt:2())))
2657 Decorator:3(
2658 Var(g)
2659 NameExpr(foo)
2660 FuncDef:4(
2661 g
2662 def () -> y?
2663 Block:4(
2664 PassStmt:4()))))
2665
2666 [case testFunctionOverloadWithinFunction]
2667 def f():
2668 @overload
2669 def g(): pass
2670 @overload
2671 def g() -> x: pass
2672 [out]
2673 MypyFile:1(
2674 FuncDef:1(
2675 f
2676 Block:2(
2677 OverloadedFuncDef:2(
2678 Decorator:2(
2679 Var(g)
2680 NameExpr(overload)
2681 FuncDef:3(
2682 g
2683 Block:3(
2684 PassStmt:3())))
2685 Decorator:4(
2686 Var(g)
2687 NameExpr(overload)
2688 FuncDef:5(
2689 g
2690 def () -> x?
2691 Block:5(
2692 PassStmt:5())))))))
2693
2694 [case testConditionalOverload1]
2695 @overload
2696 def foo(x: int): ...
2697 if sys.version_info[0] > 2:
2698 @overload
2699 def foo(x: str): ...
2700 def foo(x): pass
2701 [out]
2702 MypyFile:1(
2703 IfStmt:3(
2704 If(
2705 ComparisonExpr:3(
2706 >
2707 IndexExpr:3(
2708 MemberExpr:3(
2709 NameExpr(sys)
2710 version_info)
2711 IntExpr(0))
2712 IntExpr(2)))
2713 Then()
2714 Else())
2715 OverloadedFuncDef:1(
2716 Decorator:1(
2717 Var(foo)
2718 NameExpr(overload)
2719 FuncDef:2(
2720 foo
2721 Args(
2722 Var(x))
2723 def (x: int?) -> Any
2724 Block:2(
2725 ExpressionStmt:2(
2726 Ellipsis))))
2727 Decorator:4(
2728 Var(foo)
2729 NameExpr(overload)
2730 FuncDef:5(
2731 foo
2732 Args(
2733 Var(x))
2734 def (x: str?) -> Any
2735 Block:5(
2736 ExpressionStmt:5(
2737 Ellipsis))))
2738 FuncDef:6(
2739 foo
2740 Args(
2741 Var(x))
2742 Block:6(
2743 PassStmt:6()))))
2744
2745 [case testConditionalOverload2]
2746 @overload
2747 def foo(x: int): ...
2748 if sys.platform == "foo":
2749 @overload
2750 def foo(x: str): ...
2751 def foo(x): pass
2752 [out]
2753 MypyFile:1(
2754 IfStmt:3(
2755 If(
2756 ComparisonExpr:3(
2757 ==
2758 MemberExpr:3(
2759 NameExpr(sys)
2760 platform)
2761 StrExpr(foo)))
2762 Then())
2763 OverloadedFuncDef:1(
2764 Decorator:1(
2765 Var(foo)
2766 NameExpr(overload)
2767 FuncDef:2(
2768 foo
2769 Args(
2770 Var(x))
2771 def (x: int?) -> Any
2772 Block:2(
2773 ExpressionStmt:2(
2774 Ellipsis))))
2775 FuncDef:6(
2776 foo
2777 Args(
2778 Var(x))
2779 Block:6(
2780 PassStmt:6()))))
2781
2782 [case testConditionalOverload3]
2783 if sys.version_info[0] > 2:
2784 @overload
2785 def foo(x: str): ...
2786 @overload
2787 def foo(x: int): ...
2788 def foo(x): pass
2789 [out]
2790 MypyFile:1(
2791 IfStmt:1(
2792 If(
2793 ComparisonExpr:1(
2794 >
2795 IndexExpr:1(
2796 MemberExpr:1(
2797 NameExpr(sys)
2798 version_info)
2799 IntExpr(0))
2800 IntExpr(2)))
2801 Then()
2802 Else())
2803 OverloadedFuncDef:2(
2804 Decorator:2(
2805 Var(foo)
2806 NameExpr(overload)
2807 FuncDef:3(
2808 foo
2809 Args(
2810 Var(x))
2811 def (x: str?) -> Any
2812 Block:3(
2813 ExpressionStmt:3(
2814 Ellipsis))))
2815 Decorator:4(
2816 Var(foo)
2817 NameExpr(overload)
2818 FuncDef:5(
2819 foo
2820 Args(
2821 Var(x))
2822 def (x: int?) -> Any
2823 Block:5(
2824 ExpressionStmt:5(
2825 Ellipsis))))
2826 FuncDef:6(
2827 foo
2828 Args(
2829 Var(x))
2830 Block:6(
2831 PassStmt:6()))))
2832
2833 [case testCommentFunctionAnnotation]
2834 def f(): # type: () -> A
2835 pass
2836 def g(x): # type: (A) -> B
2837 pass
2838 [out]
2839 MypyFile:1(
2840 FuncDef:1(
2841 f
2842 def () -> A?
2843 Block:2(
2844 PassStmt:2()))
2845 FuncDef:3(
2846 g
2847 Args(
2848 Var(x))
2849 def (x: A?) -> B?
2850 Block:4(
2851 PassStmt:4())))
2852
2853 [case testCommentMethodAnnotation]
2854 class A:
2855 def f(self): # type: () -> A
2856 pass
2857 def g(xself, x): # type: (A) -> B
2858 pass
2859 [out]
2860 MypyFile:1(
2861 ClassDef:1(
2862 A
2863 FuncDef:2(
2864 f
2865 Args(
2866 Var(self))
2867 def (self: Any) -> A?
2868 Block:3(
2869 PassStmt:3()))
2870 FuncDef:4(
2871 g
2872 Args(
2873 Var(xself)
2874 Var(x))
2875 def (xself: Any, x: A?) -> B?
2876 Block:5(
2877 PassStmt:5()))))
2878
2879 [case testCommentMethodAnnotationAndNestedFunction]
2880 class A:
2881 def f(self): # type: () -> A
2882 def g(x): # type: (A) -> B
2883 pass
2884 [out]
2885 MypyFile:1(
2886 ClassDef:1(
2887 A
2888 FuncDef:2(
2889 f
2890 Args(
2891 Var(self))
2892 def (self: Any) -> A?
2893 Block:3(
2894 FuncDef:3(
2895 g
2896 Args(
2897 Var(x))
2898 def (x: A?) -> B?
2899 Block:4(
2900 PassStmt:4()))))))
2901
2902 [case testCommentFunctionAnnotationOnSeparateLine]
2903 def f(x):
2904 # type: (X) -> Y
2905 pass
2906 [out]
2907 MypyFile:1(
2908 FuncDef:1(
2909 f
2910 Args(
2911 Var(x))
2912 def (x: X?) -> Y?
2913 Block:3(
2914 PassStmt:3())))
2915
2916 [case testCommentFunctionAnnotationOnSeparateLine2]
2917 def f(x):
2918
2919 # type: (X) -> Y # bar
2920
2921 pass
2922 [out]
2923 MypyFile:1(
2924 FuncDef:1(
2925 f
2926 Args(
2927 Var(x))
2928 def (x: X?) -> Y?
2929 Block:5(
2930 PassStmt:5())))
2931
2932 [case testCommentFunctionAnnotationAndVarArg]
2933 def f(x, *y): # type: (X, *Y) -> Z
2934 pass
2935 [out]
2936 MypyFile:1(
2937 FuncDef:1(
2938 f
2939 Args(
2940 Var(x))
2941 def (x: X?, *y: Y?) -> Z?
2942 VarArg(
2943 Var(y))
2944 Block:2(
2945 PassStmt:2())))
2946
2947 [case testCommentFunctionAnnotationAndAllVarArgs]
2948 def f(x, *y, **z): # type: (X, *Y, **Z) -> A
2949 pass
2950 [out]
2951 MypyFile:1(
2952 FuncDef:1(
2953 f
2954 Args(
2955 Var(x))
2956 def (x: X?, *y: Y?, **z: Z?) -> A?
2957 VarArg(
2958 Var(y))
2959 DictVarArg(
2960 Var(z))
2961 Block:2(
2962 PassStmt:2())))
2963
2964 [case testClassDecorator]
2965 @foo
2966 class X: pass
2967 @foo(bar)
2968 @x.y
2969 class Z: pass
2970 [out]
2971 MypyFile:1(
2972 ClassDef:2(
2973 X
2974 Decorators(
2975 NameExpr(foo))
2976 PassStmt:2())
2977 ClassDef:5(
2978 Z
2979 Decorators(
2980 CallExpr:3(
2981 NameExpr(foo)
2982 Args(
2983 NameExpr(bar)))
2984 MemberExpr:4(
2985 NameExpr(x)
2986 y))
2987 PassStmt:5()))
2988
2989 [case testTrailingSemicolon]
2990 def x():
2991 pass;
2992
2993 def y():
2994 pass
2995 [out]
2996 MypyFile:1(
2997 FuncDef:1(
2998 x
2999 Block:2(
3000 PassStmt:2()))
3001 FuncDef:4(
3002 y
3003 Block:5(
3004 PassStmt:5())))
3005
3006 [case testEmptySuperClass]
3007 class A():
3008 pass
3009 [out]
3010 MypyFile:1(
3011 ClassDef:1(
3012 A
3013 PassStmt:2()))
3014
3015 [case testStarExpression]
3016 *a
3017 *a, b
3018 a, *b
3019 a, (*x, y)
3020 a, (x, *y)
3021 [out]
3022 MypyFile:1(
3023 ExpressionStmt:1(
3024 StarExpr:1(
3025 NameExpr(a)))
3026 ExpressionStmt:2(
3027 TupleExpr:2(
3028 StarExpr:2(
3029 NameExpr(a))
3030 NameExpr(b)))
3031 ExpressionStmt:3(
3032 TupleExpr:3(
3033 NameExpr(a)
3034 StarExpr:3(
3035 NameExpr(b))))
3036 ExpressionStmt:4(
3037 TupleExpr:4(
3038 NameExpr(a)
3039 TupleExpr:4(
3040 StarExpr:4(
3041 NameExpr(x))
3042 NameExpr(y))))
3043 ExpressionStmt:5(
3044 TupleExpr:5(
3045 NameExpr(a)
3046 TupleExpr:5(
3047 NameExpr(x)
3048 StarExpr:5(
3049 NameExpr(y))))))
3050
3051 [case testStarExpressionParenthesis]
3052 *(a)
3053 *(a,b)
3054 [out]
3055 MypyFile:1(
3056 ExpressionStmt:1(
3057 StarExpr:1(
3058 NameExpr(a)))
3059 ExpressionStmt:2(
3060 StarExpr:2(
3061 TupleExpr:2(
3062 NameExpr(a)
3063 NameExpr(b)))))
3064
3065 [case testStarExpressionInFor]
3066 for *a in b:
3067 pass
3068
3069 for a, *b in c:
3070 pass
3071
3072 for *a, b in c:
3073 pass
3074 [out]
3075 MypyFile:1(
3076 ForStmt:1(
3077 StarExpr:1(
3078 NameExpr(a))
3079 NameExpr(b)
3080 Block:2(
3081 PassStmt:2()))
3082 ForStmt:4(
3083 TupleExpr:4(
3084 NameExpr(a)
3085 StarExpr:4(
3086 NameExpr(b)))
3087 NameExpr(c)
3088 Block:5(
3089 PassStmt:5()))
3090 ForStmt:7(
3091 TupleExpr:7(
3092 StarExpr:7(
3093 NameExpr(a))
3094 NameExpr(b))
3095 NameExpr(c)
3096 Block:8(
3097 PassStmt:8())))
3098
3099 [case testStarExprInGeneratorExpr]
3100 (x for y, *p in z)
3101 (x for *p, y in z)
3102 (x for y, *p, q in z)
3103 [out]
3104 MypyFile:1(
3105 ExpressionStmt:1(
3106 GeneratorExpr:1(
3107 NameExpr(x)
3108 TupleExpr:1(
3109 NameExpr(y)
3110 StarExpr:1(
3111 NameExpr(p)))
3112 NameExpr(z)))
3113 ExpressionStmt:2(
3114 GeneratorExpr:2(
3115 NameExpr(x)
3116 TupleExpr:2(
3117 StarExpr:2(
3118 NameExpr(p))
3119 NameExpr(y))
3120 NameExpr(z)))
3121 ExpressionStmt:3(
3122 GeneratorExpr:3(
3123 NameExpr(x)
3124 TupleExpr:3(
3125 NameExpr(y)
3126 StarExpr:3(
3127 NameExpr(p))
3128 NameExpr(q))
3129 NameExpr(z))))
3130
3131 [case testParseNamedtupleBaseclass]
3132 class A(namedtuple('x', ['y'])): pass
3133 [out]
3134 MypyFile:1(
3135 ClassDef:1(
3136 A
3137 BaseTypeExpr(
3138 CallExpr:1(
3139 NameExpr(namedtuple)
3140 Args(
3141 StrExpr(x)
3142 ListExpr:1(
3143 StrExpr(y)))))
3144 PassStmt:1()))
3145
3146 [case testEllipsis]
3147 ...
3148 a[1,...,2]
3149 ....__class__
3150 [out]
3151 MypyFile:1(
3152 ExpressionStmt:1(
3153 Ellipsis)
3154 ExpressionStmt:2(
3155 IndexExpr:2(
3156 NameExpr(a)
3157 TupleExpr:2(
3158 IntExpr(1)
3159 Ellipsis
3160 IntExpr(2))))
3161 ExpressionStmt:3(
3162 MemberExpr:3(
3163 Ellipsis
3164 __class__)))
3165
3166 [case testFunctionWithManyKindsOfArgs]
3167 def f(x, *args, y=None, **kw): pass
3168 [out]
3169 MypyFile:1(
3170 FuncDef:1(
3171 f
3172 MaxPos(1)
3173 Args(
3174 Var(x)
3175 default(
3176 Var(y)
3177 NameExpr(None)))
3178 VarArg(
3179 Var(args))
3180 DictVarArg(
3181 Var(kw))
3182 Block:1(
3183 PassStmt:1())))
3184
3185 [case testIfWithSemicolons]
3186 if 1: a; b
3187 [out]
3188 MypyFile:1(
3189 IfStmt:1(
3190 If(
3191 IntExpr(1))
3192 Then(
3193 ExpressionStmt:1(
3194 NameExpr(a))
3195 ExpressionStmt:1(
3196 NameExpr(b)))))
3197
3198 [case testIfWithSemicolonsNested]
3199 while 2:
3200 if 1: a; b
3201 [out]
3202 MypyFile:1(
3203 WhileStmt:1(
3204 IntExpr(2)
3205 Block:2(
3206 IfStmt:2(
3207 If(
3208 IntExpr(1))
3209 Then(
3210 ExpressionStmt:2(
3211 NameExpr(a))
3212 ExpressionStmt:2(
3213 NameExpr(b)))))))
3214
3215 [case testIfElseWithSemicolons]
3216 if 1: global x; y = 1
3217 else: x = 1; return 3
3218 4
3219 [out]
3220 MypyFile:1(
3221 IfStmt:1(
3222 If(
3223 IntExpr(1))
3224 Then(
3225 GlobalDecl:1(
3226 x)
3227 AssignmentStmt:1(
3228 NameExpr(y)
3229 IntExpr(1)))
3230 Else(
3231 AssignmentStmt:2(
3232 NameExpr(x)
3233 IntExpr(1))
3234 ReturnStmt:2(
3235 IntExpr(3))))
3236 ExpressionStmt:3(
3237 IntExpr(4)))
3238
3239 [case testIfElseWithSemicolonsNested]
3240 while 2:
3241 if 1: global x; y = 1
3242 else: x = 1; return 3
3243 4
3244 [out]
3245 MypyFile:1(
3246 WhileStmt:1(
3247 IntExpr(2)
3248 Block:2(
3249 IfStmt:2(
3250 If(
3251 IntExpr(1))
3252 Then(
3253 GlobalDecl:2(
3254 x)
3255 AssignmentStmt:2(
3256 NameExpr(y)
3257 IntExpr(1)))
3258 Else(
3259 AssignmentStmt:3(
3260 NameExpr(x)
3261 IntExpr(1))
3262 ReturnStmt:3(
3263 IntExpr(3))))))
3264 ExpressionStmt:4(
3265 IntExpr(4)))
3266
3267 [case testKeywordArgumentAfterStarArgumentInCall]
3268 f(x=1, *y)
3269 [out]
3270 MypyFile:1(
3271 ExpressionStmt:1(
3272 CallExpr:1(
3273 NameExpr(f)
3274 Args(
3275 NameExpr(y))
3276 VarArg
3277 KwArgs(
3278 x
3279 IntExpr(1)))))
3280
3281 [case testConditionalExpressionInSetComprehension]
3282 { 1 if x else 2 for x in y }
3283 [out]
3284 MypyFile:1(
3285 ExpressionStmt:1(
3286 SetComprehension:1(
3287 GeneratorExpr:1(
3288 ConditionalExpr:1(
3289 Condition(
3290 NameExpr(x))
3291 IntExpr(1)
3292 IntExpr(2))
3293 NameExpr(x)
3294 NameExpr(y)))))
3295
3296 [case testConditionalExpressionInListComprehension2]
3297 a = [ 1 if x else 2 for x in y ]
3298 [out]
3299 MypyFile:1(
3300 AssignmentStmt:1(
3301 NameExpr(a)
3302 ListComprehension:1(
3303 GeneratorExpr:1(
3304 ConditionalExpr:1(
3305 Condition(
3306 NameExpr(x))
3307 IntExpr(1)
3308 IntExpr(2))
3309 NameExpr(x)
3310 NameExpr(y)))))
3311
3312 [case testComplexWithLvalue]
3313 with x as y.z: pass
3314 [out]
3315 MypyFile:1(
3316 WithStmt:1(
3317 Expr(
3318 NameExpr(x))
3319 Target(
3320 MemberExpr:1(
3321 NameExpr(y)
3322 z))
3323 Block:1(
3324 PassStmt:1())))
3325
3326 [case testRelativeImportWithEllipsis]
3327 from ... import x
3328 [out]
3329 MypyFile:1(
3330 ImportFrom:1(..., [x]))
3331
3332 [case testRelativeImportWithEllipsis2]
3333 from .... import x
3334 [out]
3335 MypyFile:1(
3336 ImportFrom:1(...., [x]))
3337
3338 [case testParseExtendedSlicing]
3339 a[:, :]
3340 [out]
3341 MypyFile:1(
3342 ExpressionStmt:1(
3343 IndexExpr:1(
3344 NameExpr(a)
3345 TupleExpr:1(
3346 SliceExpr:1(
3347 <empty>
3348 <empty>)
3349 SliceExpr:1(
3350 <empty>
3351 <empty>)))))
3352
3353 [case testParseExtendedSlicing2]
3354 a[1:2:, :,]
3355 [out]
3356 MypyFile:1(
3357 ExpressionStmt:1(
3358 IndexExpr:1(
3359 NameExpr(a)
3360 TupleExpr:1(
3361 SliceExpr:1(
3362 IntExpr(1)
3363 IntExpr(2))
3364 SliceExpr:1(
3365 <empty>
3366 <empty>)))))
3367
3368 [case testParseExtendedSlicing3]
3369 a[1:2:3, ..., 1]
3370 [out]
3371 MypyFile:1(
3372 ExpressionStmt:1(
3373 IndexExpr:1(
3374 NameExpr(a)
3375 TupleExpr:1(
3376 SliceExpr:1(
3377 IntExpr(1)
3378 IntExpr(2)
3379 IntExpr(3))
3380 Ellipsis
3381 IntExpr(1)))))
3382
3383 [case testParseExtendedSlicing4]
3384 m[*index, :]
3385 [out]
3386 main:1: error: Invalid syntax
3387 [out version>=3.11]
3388 MypyFile:1(
3389 ExpressionStmt:1(
3390 IndexExpr:1(
3391 NameExpr(m)
3392 TupleExpr:1(
3393 StarExpr:1(
3394 NameExpr(index))
3395 SliceExpr:1(
3396 <empty>
3397 <empty>)))))
3398
3399 [case testParseIfExprInDictExpr]
3400 test = { 'spam': 'eggs' if True else 'bacon' }
3401 [out]
3402 MypyFile:1(
3403 AssignmentStmt:1(
3404 NameExpr(test)
3405 DictExpr:1(
3406 StrExpr(spam)
3407 ConditionalExpr:1(
3408 Condition(
3409 NameExpr(True))
3410 StrExpr(eggs)
3411 StrExpr(bacon)))))
3412
3413 [case testIgnoreLine]
3414 import x # type: ignore
3415 [out]
3416 MypyFile:1(
3417 Import:1(x)
3418 IgnoredLines(1))
3419
3420 [case testIgnore2Lines]
3421 x
3422 y # type: ignore
3423 z # type: ignore
3424 [out]
3425 MypyFile:1(
3426 ExpressionStmt:1(
3427 NameExpr(x))
3428 ExpressionStmt:2(
3429 NameExpr(y))
3430 ExpressionStmt:3(
3431 NameExpr(z))
3432 IgnoredLines(2, 3))
3433
3434 [case testCommentedOutIgnoreAnnotation]
3435 y ## type: ignore
3436 [out]
3437 MypyFile:1(
3438 ExpressionStmt:1(
3439 NameExpr(y)))
3440
3441 [case testSpaceInIgnoreAnnotations]
3442 y # type: ignore # foo
3443 y #type:ignore
3444 [out]
3445 MypyFile:1(
3446 ExpressionStmt:1(
3447 NameExpr(y))
3448 ExpressionStmt:2(
3449 NameExpr(y))
3450 IgnoredLines(1, 2))
3451
3452 [case testIgnoreAnnotationAndMultilineStatement]
3453 x = {
3454 1: 2 # type: ignore
3455 }
3456 y = { # type: ignore
3457 1: 2
3458 } # type: ignore
3459 [out]
3460 MypyFile:1(
3461 AssignmentStmt:1(
3462 NameExpr(x)
3463 DictExpr:1(
3464 IntExpr(1)
3465 IntExpr(2)))
3466 AssignmentStmt:4(
3467 NameExpr(y)
3468 DictExpr:4(
3469 IntExpr(1)
3470 IntExpr(2)))
3471 IgnoredLines(2, 4, 6))
3472
3473 [case testIgnoreAnnotationAndMultilineStatement2]
3474 from m import ( # type: ignore
3475 x, y
3476 )
3477 [out]
3478 MypyFile:1(
3479 ImportFrom:1(m, [x, y])
3480 IgnoredLines(1))
3481
3482 [case testYieldExpression]
3483 def f():
3484 x = yield f()
3485 [out]
3486 MypyFile:1(
3487 FuncDef:1(
3488 f
3489 Block:2(
3490 AssignmentStmt:2(
3491 NameExpr(x)
3492 YieldExpr:2(
3493 CallExpr:2(
3494 NameExpr(f)
3495 Args()))))))
3496
3497 [case testForWithSingleItemTuple]
3498 for x in 1,: pass
3499 [out]
3500 MypyFile:1(
3501 ForStmt:1(
3502 NameExpr(x)
3503 TupleExpr:1(
3504 IntExpr(1))
3505 Block:1(
3506 PassStmt:1())))
3507
3508 [case testIsoLatinUnixEncoding]
3509 # coding: iso-latin-1-unix
3510 [out]
3511 MypyFile:1()
3512
3513 [case testLatinUnixEncoding]
3514 # coding: latin-1-unix
3515 [out]
3516 MypyFile:1()
3517
3518 [case testLatinUnixEncoding2]
3519 # coding: iso-latin-1
3520 [out]
3521 MypyFile:1()
3522
3523 [case testYieldExpressionInParens]
3524 def f():
3525 (yield)
3526 [out]
3527 MypyFile:1(
3528 FuncDef:1(
3529 f
3530 Block:2(
3531 ExpressionStmt:2(
3532 YieldExpr:2()))))
3533
3534 [case testFStringSimple]
3535 x = 'mypy'
3536 f'Hello {x}'
3537 [out]
3538 MypyFile:1(
3539 AssignmentStmt:1(
3540 NameExpr(x)
3541 StrExpr(mypy))
3542 ExpressionStmt:2(
3543 CallExpr:2(
3544 MemberExpr:2(
3545 StrExpr()
3546 join)
3547 Args(
3548 ListExpr:2(
3549 StrExpr(Hello )
3550 CallExpr:2(
3551 MemberExpr:2(
3552 StrExpr({:{}})
3553 format)
3554 Args(
3555 NameExpr(x)
3556 StrExpr())))))))
3557
3558 [case testFStringWithConversion]
3559 x = 'mypy'
3560 F'Hello {x!r}'
3561 [out]
3562 MypyFile:1(
3563 AssignmentStmt:1(
3564 NameExpr(x)
3565 StrExpr(mypy))
3566 ExpressionStmt:2(
3567 CallExpr:2(
3568 MemberExpr:2(
3569 StrExpr()
3570 join)
3571 Args(
3572 ListExpr:2(
3573 StrExpr(Hello )
3574 CallExpr:2(
3575 MemberExpr:2(
3576 StrExpr({!r:{}})
3577 format)
3578 Args(
3579 NameExpr(x)
3580 StrExpr())))))))
3581
3582 [case testFStringWithOnlyFormatSpecifier]
3583 x = 'mypy'
3584 f'Hello {x:<30}'
3585 [out]
3586 MypyFile:1(
3587 AssignmentStmt:1(
3588 NameExpr(x)
3589 StrExpr(mypy))
3590 ExpressionStmt:2(
3591 CallExpr:2(
3592 MemberExpr:2(
3593 StrExpr()
3594 join)
3595 Args(
3596 ListExpr:2(
3597 StrExpr(Hello )
3598 CallExpr:2(
3599 MemberExpr:2(
3600 StrExpr({:{}})
3601 format)
3602 Args(
3603 NameExpr(x)
3604 StrExpr(<30))))))))
3605
3606 [case testFStringWithFormatSpecifierAndConversion]
3607 x = 'mypy'
3608 f'Hello {x!s:<30}'
3609 [out]
3610 MypyFile:1(
3611 AssignmentStmt:1(
3612 NameExpr(x)
3613 StrExpr(mypy))
3614 ExpressionStmt:2(
3615 CallExpr:2(
3616 MemberExpr:2(
3617 StrExpr()
3618 join)
3619 Args(
3620 ListExpr:2(
3621 StrExpr(Hello )
3622 CallExpr:2(
3623 MemberExpr:2(
3624 StrExpr({!s:{}})
3625 format)
3626 Args(
3627 NameExpr(x)
3628 StrExpr(<30))))))))
3629
3630 [case testFStringWithFormatSpecifierExpression]
3631 x = 'mypy'
3632 y = 30
3633 f'Hello {x!s:<{y+y}}'
3634 [out]
3635 MypyFile:1(
3636 AssignmentStmt:1(
3637 NameExpr(x)
3638 StrExpr(mypy))
3639 AssignmentStmt:2(
3640 NameExpr(y)
3641 IntExpr(30))
3642 ExpressionStmt:3(
3643 CallExpr:3(
3644 MemberExpr:3(
3645 StrExpr()
3646 join)
3647 Args(
3648 ListExpr:3(
3649 StrExpr(Hello )
3650 CallExpr:3(
3651 MemberExpr:3(
3652 StrExpr({!s:{}})
3653 format)
3654 Args(
3655 NameExpr(x)
3656 CallExpr:3(
3657 MemberExpr:3(
3658 StrExpr()
3659 join)
3660 Args(
3661 ListExpr:3(
3662 StrExpr(<)
3663 CallExpr:3(
3664 MemberExpr:3(
3665 StrExpr({:{}})
3666 format)
3667 Args(
3668 OpExpr:3(
3669 +
3670 NameExpr(y)
3671 NameExpr(y))
3672 StrExpr()))))))))))))
3673
3674 [case testStripFunctionBodiesIfIgnoringErrors]
3675 # mypy: ignore-errors=True
3676 def f(self):
3677 self.x = 1 # Cannot define an attribute
3678 return 1
3679 [out]
3680 MypyFile:1(
3681 FuncDef:2(
3682 f
3683 Args(
3684 Var(self))
3685 Block:3()))
3686
3687 [case testStripMethodBodiesIfIgnoringErrors]
3688 # mypy: ignore-errors=True
3689 class C:
3690 def f(self):
3691 x = self.x
3692 for x in y:
3693 pass
3694 with a as y:
3695 pass
3696 while self.foo():
3697 self.bah()
3698 a[self.x] = 1
3699 [out]
3700 MypyFile:1(
3701 ClassDef:2(
3702 C
3703 FuncDef:3(
3704 f
3705 Args(
3706 Var(self))
3707 Block:4())))
3708
3709 [case testDoNotStripModuleTopLevelOrClassBody]
3710 # mypy: ignore-errors=True
3711 f()
3712 class C:
3713 x = 5
3714 [out]
3715 MypyFile:1(
3716 ExpressionStmt:2(
3717 CallExpr:2(
3718 NameExpr(f)
3719 Args()))
3720 ClassDef:3(
3721 C
3722 AssignmentStmt:4(
3723 NameExpr(x)
3724 IntExpr(5))))
3725
3726 [case testDoNotStripMethodThatAssignsToAttribute]
3727 # mypy: ignore-errors=True
3728 class C:
3729 def m1(self):
3730 self.x = 0
3731 def m2(self):
3732 a, self.y = 0
3733 [out]
3734 MypyFile:1(
3735 ClassDef:2(
3736 C
3737 FuncDef:3(
3738 m1
3739 Args(
3740 Var(self))
3741 Block:4(
3742 AssignmentStmt:4(
3743 MemberExpr:4(
3744 NameExpr(self)
3745 x)
3746 IntExpr(0))))
3747 FuncDef:5(
3748 m2
3749 Args(
3750 Var(self))
3751 Block:6(
3752 AssignmentStmt:6(
3753 TupleExpr:6(
3754 NameExpr(a)
3755 MemberExpr:6(
3756 NameExpr(self)
3757 y))
3758 IntExpr(0))))))
3759
3760 [case testDoNotStripMethodThatAssignsToAttributeWithinStatement]
3761 # mypy: ignore-errors=True
3762 class C:
3763 def m1(self):
3764 for x in y:
3765 self.x = 0
3766 def m2(self):
3767 with x:
3768 self.y = 0
3769 def m3(self):
3770 if x:
3771 self.y = 0
3772 else:
3773 x = 4
3774 [out]
3775 MypyFile:1(
3776 ClassDef:2(
3777 C
3778 FuncDef:3(
3779 m1
3780 Args(
3781 Var(self))
3782 Block:4(
3783 ForStmt:4(
3784 NameExpr(x)
3785 NameExpr(y)
3786 Block:5(
3787 AssignmentStmt:5(
3788 MemberExpr:5(
3789 NameExpr(self)
3790 x)
3791 IntExpr(0))))))
3792 FuncDef:6(
3793 m2
3794 Args(
3795 Var(self))
3796 Block:7(
3797 WithStmt:7(
3798 Expr(
3799 NameExpr(x))
3800 Block:8(
3801 AssignmentStmt:8(
3802 MemberExpr:8(
3803 NameExpr(self)
3804 y)
3805 IntExpr(0))))))
3806 FuncDef:9(
3807 m3
3808 Args(
3809 Var(self))
3810 Block:10(
3811 IfStmt:10(
3812 If(
3813 NameExpr(x))
3814 Then(
3815 AssignmentStmt:11(
3816 MemberExpr:11(
3817 NameExpr(self)
3818 y)
3819 IntExpr(0)))
3820 Else(
3821 AssignmentStmt:13(
3822 NameExpr(x)
3823 IntExpr(4))))))))
3824
3825 [case testDoNotStripMethodThatDefinesAttributeWithoutAssignment]
3826 # mypy: ignore-errors=True
3827 class C:
3828 def m1(self):
3829 with y as self.x:
3830 pass
3831 def m2(self):
3832 for self.y in x:
3833 pass
3834 [out]
3835 MypyFile:1(
3836 ClassDef:2(
3837 C
3838 FuncDef:3(
3839 m1
3840 Args(
3841 Var(self))
3842 Block:4(
3843 WithStmt:4(
3844 Expr(
3845 NameExpr(y))
3846 Target(
3847 MemberExpr:4(
3848 NameExpr(self)
3849 x))
3850 Block:5(
3851 PassStmt:5()))))
3852 FuncDef:6(
3853 m2
3854 Args(
3855 Var(self))
3856 Block:7(
3857 ForStmt:7(
3858 MemberExpr:7(
3859 NameExpr(self)
3860 y)
3861 NameExpr(x)
3862 Block:8(
3863 PassStmt:8()))))))
3864
3865 [case testStripDecoratedFunctionOrMethod]
3866 # mypy: ignore-errors=True
3867 @deco
3868 def f():
3869 x = 0
3870
3871 class C:
3872 @deco
3873 def m1(self):
3874 x = 0
3875
3876 @deco
3877 def m2(self):
3878 self.x = 0
3879 [out]
3880 MypyFile:1(
3881 Decorator:2(
3882 Var(f)
3883 NameExpr(deco)
3884 FuncDef:3(
3885 f
3886 Block:4()))
3887 ClassDef:6(
3888 C
3889 Decorator:7(
3890 Var(m1)
3891 NameExpr(deco)
3892 FuncDef:8(
3893 m1
3894 Args(
3895 Var(self))
3896 Block:9()))
3897 Decorator:11(
3898 Var(m2)
3899 NameExpr(deco)
3900 FuncDef:12(
3901 m2
3902 Args(
3903 Var(self))
3904 Block:13(
3905 AssignmentStmt:13(
3906 MemberExpr:13(
3907 NameExpr(self)
3908 x)
3909 IntExpr(0)))))))
3910
3911 [case testStripOverloadedMethod]
3912 # mypy: ignore-errors=True
3913 class C:
3914 @overload
3915 def m1(self, x: int) -> None: ...
3916 @overload
3917 def m1(self, x: str) -> None: ...
3918 def m1(self, x):
3919 x = 0
3920
3921 @overload
3922 def m2(self, x: int) -> None: ...
3923 @overload
3924 def m2(self, x: str) -> None: ...
3925 def m2(self, x):
3926 self.x = 0
3927 [out]
3928 MypyFile:1(
3929 ClassDef:2(
3930 C
3931 OverloadedFuncDef:3(
3932 Decorator:3(
3933 Var(m1)
3934 NameExpr(overload)
3935 FuncDef:4(
3936 m1
3937 Args(
3938 Var(self)
3939 Var(x))
3940 def (self: Any, x: int?) -> None?
3941 Block:4(
3942 ExpressionStmt:4(
3943 Ellipsis))))
3944 Decorator:5(
3945 Var(m1)
3946 NameExpr(overload)
3947 FuncDef:6(
3948 m1
3949 Args(
3950 Var(self)
3951 Var(x))
3952 def (self: Any, x: str?) -> None?
3953 Block:6(
3954 ExpressionStmt:6(
3955 Ellipsis))))
3956 FuncDef:7(
3957 m1
3958 Args(
3959 Var(self)
3960 Var(x))
3961 Block:8()))
3962 OverloadedFuncDef:10(
3963 Decorator:10(
3964 Var(m2)
3965 NameExpr(overload)
3966 FuncDef:11(
3967 m2
3968 Args(
3969 Var(self)
3970 Var(x))
3971 def (self: Any, x: int?) -> None?
3972 Block:11(
3973 ExpressionStmt:11(
3974 Ellipsis))))
3975 Decorator:12(
3976 Var(m2)
3977 NameExpr(overload)
3978 FuncDef:13(
3979 m2
3980 Args(
3981 Var(self)
3982 Var(x))
3983 def (self: Any, x: str?) -> None?
3984 Block:13(
3985 ExpressionStmt:13(
3986 Ellipsis))))
3987 FuncDef:14(
3988 m2
3989 Args(
3990 Var(self)
3991 Var(x))
3992 Block:15(
3993 AssignmentStmt:15(
3994 MemberExpr:15(
3995 NameExpr(self)
3996 x)
3997 IntExpr(0)))))))
3998
3999 [case testStripMethodInNestedClass]
4000 # mypy: ignore-errors=True
4001 class C:
4002 class D:
4003 def m1(self):
4004 self.x = 1
4005 def m2(self):
4006 return self.x
4007 [out]
4008 MypyFile:1(
4009 ClassDef:2(
4010 C
4011 ClassDef:3(
4012 D
4013 FuncDef:4(
4014 m1
4015 Args(
4016 Var(self))
4017 Block:5(
4018 AssignmentStmt:5(
4019 MemberExpr:5(
4020 NameExpr(self)
4021 x)
4022 IntExpr(1))))
4023 FuncDef:6(
4024 m2
4025 Args(
4026 Var(self))
4027 Block:7()))))
4028
4029 [case testDebugFString]
4030 f"test {x=}"
4031 [out]
4032 MypyFile:1(
4033 ExpressionStmt:1(
4034 CallExpr:1(
4035 MemberExpr:1(
4036 StrExpr()
4037 join)
4038 Args(
4039 ListExpr:1(
4040 StrExpr(test x=)
4041 CallExpr:1(
4042 MemberExpr:1(
4043 StrExpr({!r:{}})
4044 format)
4045 Args(
4046 NameExpr(x)
4047 StrExpr())))))))