-- Test cases for parser. Each test case consists of two sections. -- The first section contains [case NAME] followed by the input code, while -- the second section contains [out] followed by the output from the parser. -- -- Lines starting with "--" in this file will be ignored, except for lines -- starting with "----" that are not ignored. The first two dashes of these -- lines are interpreted as escapes and removed. [case testEmptyFile] [out] MypyFile:1() [case testExpressionStatement] 1 [out] MypyFile:1( ExpressionStmt:1( IntExpr(1))) [case testAssignment] x = 1 [out] MypyFile:1( AssignmentStmt:1( NameExpr(x) IntExpr(1))) [case testExpressionBasics] x = f(1, None) 123 * (2 + x) "hello".lower() -1.23 [out] MypyFile:1( AssignmentStmt:1( NameExpr(x) CallExpr:1( NameExpr(f) Args( IntExpr(1) NameExpr(None)))) ExpressionStmt:2( OpExpr:2( * IntExpr(123) OpExpr:2( + IntExpr(2) NameExpr(x)))) ExpressionStmt:3( CallExpr:3( MemberExpr:3( StrExpr(hello) lower) Args())) ExpressionStmt:4( UnaryExpr:4( - FloatExpr(1.23)))) [case testSingleQuotedStr] '' 'foo' 'foo\ bar' [out] MypyFile:1( ExpressionStmt:1( StrExpr()) ExpressionStmt:2( StrExpr(foo)) ExpressionStmt:3( StrExpr(foobar))) [case testDoubleQuotedStr] "" "foo" "foo\ bar" [out] MypyFile:1( ExpressionStmt:1( StrExpr()) ExpressionStmt:2( StrExpr(foo)) ExpressionStmt:3( StrExpr(foobar))) [case testRawStr] r'x\n\'' r"x\n\"" [out] MypyFile:1( ExpressionStmt:1( StrExpr(x\n\')) ExpressionStmt:2( StrExpr(x\n\"))) [case testBytes] b'foo' b"foo\ bar" br'x\n\'' [out] MypyFile:1( ExpressionStmt:1( BytesExpr(foo)) ExpressionStmt:2( BytesExpr(foobar)) ExpressionStmt:3( BytesExpr(x\\n\\'))) [case testEscapesInStrings] '\r\n\t\x2f\u123f' b'\r\n\t\x2f\u123f' [out] MypyFile:1( ExpressionStmt:1( StrExpr(\u000d\u000a\u0009/\u123f)) ExpressionStmt:2( BytesExpr(\r\n\t/\\\u123f))) -- Note \\u in the b'...' case (\u sequence not translated) [case testEscapedQuote] '\'' [out] MypyFile:1( ExpressionStmt:1( StrExpr('))) [case testOctalEscapes] '\0\1\177\1234' b'\1\476' [out] MypyFile:1( ExpressionStmt:1( StrExpr(\u0000\u0001\u007fS4)) ExpressionStmt:2( BytesExpr(\x01>))) [case testUnicodeLiteralInPython3] u'foo' [out] MypyFile:1( ExpressionStmt:1( StrExpr(foo))) [case testArrays] a = [] a = [1, 2] a[[1]] = a[2] [out] MypyFile:1( AssignmentStmt:1( NameExpr(a) ListExpr:1()) AssignmentStmt:2( NameExpr(a) ListExpr:2( IntExpr(1) IntExpr(2))) AssignmentStmt:3( IndexExpr:3( NameExpr(a) ListExpr:3( IntExpr(1))) IndexExpr:3( NameExpr(a) IntExpr(2)))) [case testTuples] () (1,) (1, foo) a, b = 1, (2, 3) [out] MypyFile:1( ExpressionStmt:1( TupleExpr:1()) ExpressionStmt:2( TupleExpr:2( IntExpr(1))) ExpressionStmt:3( TupleExpr:3( IntExpr(1) NameExpr(foo))) AssignmentStmt:4( TupleExpr:4( NameExpr(a) NameExpr(b)) TupleExpr:4( IntExpr(1) TupleExpr:4( IntExpr(2) IntExpr(3))))) [case testNestedListAssignment] a1, [b1, c1] = a2, [b2, c2] [out] MypyFile:1( AssignmentStmt:1( TupleExpr:1( NameExpr(a1) TupleExpr:1( NameExpr(b1) NameExpr(c1))) TupleExpr:1( NameExpr(a2) ListExpr:1( NameExpr(b2) NameExpr(c2))))) [case testSimpleFunction] def main(): 1 [out] MypyFile:1( FuncDef:1( main Block:2( ExpressionStmt:2( IntExpr(1))))) [case testPass] def f(): pass [out] MypyFile:1( FuncDef:1( f Block:2( PassStmt:2()))) [case testIf] if 1: 2 [out] MypyFile:1( IfStmt:1( If( IntExpr(1)) Then( ExpressionStmt:2( IntExpr(2))))) [case testIfElse] if 1: 2 else: 3 [out] MypyFile:1( IfStmt:1( If( IntExpr(1)) Then( ExpressionStmt:2( IntExpr(2))) Else( ExpressionStmt:4( IntExpr(3))))) [case testIfElif] if 1: 2 elif 3: 4 elif 5: 6 else: 7 [out] MypyFile:1( IfStmt:1( If( IntExpr(1)) Then( ExpressionStmt:2( IntExpr(2))) Else( IfStmt:3( If( IntExpr(3)) Then( ExpressionStmt:4( IntExpr(4))) Else( IfStmt:5( If( IntExpr(5)) Then( ExpressionStmt:6( IntExpr(6))) Else( ExpressionStmt:8( IntExpr(7))))))))) [case testWhile] while 1: pass [out] MypyFile:1( WhileStmt:1( IntExpr(1) Block:2( PassStmt:2()))) [case testReturn] def f(): return 1 [out] MypyFile:1( FuncDef:1( f Block:2( ReturnStmt:2( IntExpr(1))))) [case testReturnWithoutValue] def f(): return [out] MypyFile:1( FuncDef:1( f Block:2( ReturnStmt:2()))) [case testBreak] while 1: break [out] MypyFile:1( WhileStmt:1( IntExpr(1) Block:2( BreakStmt:2()))) [case testLargeBlock] if 1: x = 1 while 2: pass y = 2 [out] MypyFile:1( IfStmt:1( If( IntExpr(1)) Then( AssignmentStmt:2( NameExpr(x) IntExpr(1)) WhileStmt:3( IntExpr(2) Block:4( PassStmt:4())) AssignmentStmt:5( NameExpr(y) IntExpr(2))))) [case testSimpleClass] class A: def f(self): pass [out] MypyFile:1( ClassDef:1( A FuncDef:2( f Args( Var(self)) Block:3( PassStmt:3())))) [case testGlobalVarWithType] x = 0 # type: int y = False # type: bool [out] MypyFile:1( AssignmentStmt:1( NameExpr(x) IntExpr(0) int?) AssignmentStmt:2( NameExpr(y) NameExpr(False) bool?)) [case testLocalVarWithType] def f(): x = 0 # type: int y = False # type: bool a = None # type: Any [out] MypyFile:1( FuncDef:1( f Block:2( AssignmentStmt:2( NameExpr(x) IntExpr(0) int?) AssignmentStmt:3( NameExpr(y) NameExpr(False) bool?) AssignmentStmt:4( NameExpr(a) NameExpr(None) Any?)))) [case testFunctionDefWithType] def f(y: str) -> int: return class A: def f(self, a: int, b: Any) -> x: pass def g(self) -> Any: pass [out] MypyFile:1( FuncDef:1( f Args( Var(y)) def (y: str?) -> int? Block:2( ReturnStmt:2())) ClassDef:3( A FuncDef:4( f Args( Var(self) Var(a) Var(b)) def (self: Any, a: int?, b: Any?) -> x? Block:5( PassStmt:5())) FuncDef:6( g Args( Var(self)) def (self: Any) -> Any? Block:7( PassStmt:7())))) [case testFuncWithNoneReturn] def f() -> None: pass [out] MypyFile:1( FuncDef:1( f def () -> None? Block:2( PassStmt:2()))) [case testVarDefWithGenericType] x = None # type: List[str] y = None # type: Dict[int, Any] [out] MypyFile:1( AssignmentStmt:1( NameExpr(x) NameExpr(None) List?[str?]) AssignmentStmt:2( NameExpr(y) NameExpr(None) Dict?[int?, Any?])) [case testSignatureWithGenericTypes] def f(y: t[Any, x]) -> a[b[c], d]: pass [out] MypyFile:1( FuncDef:1( f Args( Var(y)) def (y: t?[Any?, x?]) -> a?[b?[c?], d?] Block:2( PassStmt:2()))) [case testParsingExpressionsWithLessAndGreaterThan] # The operators < > can sometimes be confused with generic types. x = a < b > c f(x < b, y > c) a < b > 1 x < b, y > 2 (a < b > c) [out] MypyFile:1( AssignmentStmt:2( NameExpr(x) ComparisonExpr:2( < > NameExpr(a) NameExpr(b) NameExpr(c))) ExpressionStmt:3( CallExpr:3( NameExpr(f) Args( ComparisonExpr:3( < NameExpr(x) NameExpr(b)) ComparisonExpr:3( > NameExpr(y) NameExpr(c))))) ExpressionStmt:4( ComparisonExpr:4( < > NameExpr(a) NameExpr(b) IntExpr(1))) ExpressionStmt:5( TupleExpr:5( ComparisonExpr:5( < NameExpr(x) NameExpr(b)) ComparisonExpr:5( > NameExpr(y) IntExpr(2)))) ExpressionStmt:6( ComparisonExpr:6( < > NameExpr(a) NameExpr(b) NameExpr(c)))) [case testLineContinuation] if (1 + 2): pass [out] MypyFile:1( IfStmt:1( If( OpExpr:1( + IntExpr(1) IntExpr(2))) Then( PassStmt:3()))) [case testMultipleVarDef] x, y = z # type: int, a[c] [out] MypyFile:1( AssignmentStmt:1( TupleExpr:1( NameExpr(x) NameExpr(y)) NameExpr(z) tuple[int?, a?[c?]])) [case testMultipleVarDef2] (xx, z, i) = 1 # type: (a[c], Any, int) [out] MypyFile:1( AssignmentStmt:1( TupleExpr:1( NameExpr(xx) NameExpr(z) NameExpr(i)) IntExpr(1) tuple[a?[c?], Any?, int?])) [case testMultipleVarDef3] (xx, (z, i)) = 1 # type: (a[c], (Any, int)) [out] MypyFile:1( AssignmentStmt:1( TupleExpr:1( NameExpr(xx) TupleExpr:1( NameExpr(z) NameExpr(i))) IntExpr(1) tuple[a?[c?], tuple[Any?, int?]])) [case testAnnotateAssignmentViaSelf] class A: def __init__(self): self.x = 1 # type: int [out] MypyFile:1( ClassDef:1( A FuncDef:2( __init__ Args( Var(self)) Block:3( AssignmentStmt:3( MemberExpr:3( NameExpr(self) x) IntExpr(1) int?))))) [case testCommentAfterTypeComment] x = 0 # type: int # bar! [out] MypyFile:1( AssignmentStmt:1( NameExpr(x) IntExpr(0) int?)) [case testMultilineAssignmentAndAnnotations] (x, y) = (1, 2) # type: foo, bar [out] MypyFile:1( AssignmentStmt:1( TupleExpr:1( NameExpr(x) NameExpr(y)) TupleExpr:2( IntExpr(1) IntExpr(2)) tuple[foo?, bar?])) [case testWhitespaceAndCommentAnnotation] x = 1#type:int [out] MypyFile:1( AssignmentStmt:1( NameExpr(x) IntExpr(1) int?)) [case testWhitespaceAndCommentAnnotation2] x = 1# type: int [out] MypyFile:1( AssignmentStmt:1( NameExpr(x) IntExpr(1) int?)) [case testWhitespaceAndCommentAnnotation3] x = 1# type : int # not recognized! [out] MypyFile:1( AssignmentStmt:1( NameExpr(x) IntExpr(1))) [case testInvalidAnnotation] x=1 ##type: int y=1 #.type: int z=1 # Type: int [out] MypyFile:1( AssignmentStmt:1( NameExpr(x) IntExpr(1)) AssignmentStmt:2( NameExpr(y) IntExpr(1)) AssignmentStmt:3( NameExpr(z) IntExpr(1))) [case testEmptyClass] class C: pass [out] MypyFile:1( ClassDef:1( C PassStmt:2())) [case testOperatorPrecedence] a | b ^ c a & b << c [out] MypyFile:1( ExpressionStmt:1( OpExpr:1( | NameExpr(a) OpExpr:1( ^ NameExpr(b) NameExpr(c)))) ExpressionStmt:2( OpExpr:2( & NameExpr(a) OpExpr:2( << NameExpr(b) NameExpr(c))))) [case testOperatorAssociativity] 1 - 2 + 3 1 << 2 << 3 [out] MypyFile:1( ExpressionStmt:1( OpExpr:1( + OpExpr:1( - IntExpr(1) IntExpr(2)) IntExpr(3))) ExpressionStmt:2( OpExpr:2( << OpExpr:2( << IntExpr(1) IntExpr(2)) IntExpr(3)))) [case testUnaryOperators] -2 * +3 * ~3 * 2 ~3**2 [out] MypyFile:1( ExpressionStmt:1( OpExpr:1( * OpExpr:1( * OpExpr:1( * UnaryExpr:1( - IntExpr(2)) UnaryExpr:1( + IntExpr(3))) UnaryExpr:1( ~ IntExpr(3))) IntExpr(2))) ExpressionStmt:2( UnaryExpr:2( ~ OpExpr:2( ** IntExpr(3) IntExpr(2))))) [case testSingleLineBodies] if 1: pass while 1: pass def f(): pass def g() -> int: return 1 [out] MypyFile:1( IfStmt:1( If( IntExpr(1)) Then( PassStmt:1())) WhileStmt:2( IntExpr(1) Block:2( PassStmt:2())) FuncDef:3( f Block:3( PassStmt:3())) FuncDef:4( g def () -> int? Block:4( ReturnStmt:4( IntExpr(1))))) [case testForStatement] for x in y: pass for x, (y, w) in z: 1 for [x, (y, w)] in z: 1 [out] MypyFile:1( ForStmt:1( NameExpr(x) NameExpr(y) Block:2( PassStmt:2())) ForStmt:3( TupleExpr:3( NameExpr(x) TupleExpr:3( NameExpr(y) NameExpr(w))) NameExpr(z) Block:4( ExpressionStmt:4( IntExpr(1)))) ForStmt:5( TupleExpr:5( NameExpr(x) TupleExpr:5( NameExpr(y) NameExpr(w))) NameExpr(z) Block:6( ExpressionStmt:6( IntExpr(1))))) [case testGlobalDecl] global x def f(): global x, y [out] MypyFile:1( GlobalDecl:1( x) FuncDef:2( f Block:3( GlobalDecl:3( x y)))) [case testNonlocalDecl] def f(): def g(): nonlocal x, y [out] MypyFile:1( FuncDef:1( f Block:2( FuncDef:2( g Block:3( NonlocalDecl:3( x y)))))) [case testRaiseStatement] raise foo [out] MypyFile:1( RaiseStmt:1( NameExpr(foo))) [case testRaiseWithoutArg] try: pass except: raise [out] MypyFile:1( TryStmt:1( Block:2( PassStmt:2()) Block:4( RaiseStmt:4()))) [case testRaiseFrom] raise e from x [out] MypyFile:1( RaiseStmt:1( NameExpr(e) NameExpr(x))) [case testBaseclasses] class A(B): pass class A(B[T], C[Any, d[x]]): pass [out] MypyFile:1( ClassDef:1( A BaseTypeExpr( NameExpr(B)) PassStmt:2()) ClassDef:3( A BaseTypeExpr( IndexExpr:3( NameExpr(B) NameExpr(T)) IndexExpr:3( NameExpr(C) TupleExpr:3( NameExpr(Any) IndexExpr:3( NameExpr(d) NameExpr(x))))) PassStmt:4())) [case testIsNot] x is not y [out] MypyFile:1( ExpressionStmt:1( ComparisonExpr:1( is not NameExpr(x) NameExpr(y)))) [case testNotIn] x not in y not x not in y x not in y | z [out] MypyFile:1( ExpressionStmt:1( ComparisonExpr:1( not in NameExpr(x) NameExpr(y))) ExpressionStmt:2( UnaryExpr:2( not ComparisonExpr:2( not in NameExpr(x) NameExpr(y)))) ExpressionStmt:3( ComparisonExpr:3( not in NameExpr(x) OpExpr:3( | NameExpr(y) NameExpr(z))))) [case testNotAsBinaryOp] x not y [out] main:1: error: Invalid syntax [out version==3.10.0] main:1: error: Invalid syntax. Perhaps you forgot a comma? [case testNotIs] x not is y # E: Invalid syntax [out] [case testBinaryNegAsBinaryOp] 1 ~ 2 [out] main:1: error: Invalid syntax [out version==3.10.0] main:1: error: Invalid syntax. Perhaps you forgot a comma? [case testSliceInList] x = [1, 2][1:2] [out] MypyFile:1( AssignmentStmt:1( NameExpr(x) IndexExpr:1( ListExpr:1( IntExpr(1) IntExpr(2)) SliceExpr:1( IntExpr(1) IntExpr(2))))) [case testDictionaryExpression] {} {1:x} {1:x, 2 or 1:2 and 3} [out] MypyFile:1( ExpressionStmt:1( DictExpr:1()) ExpressionStmt:2( DictExpr:2( IntExpr(1) NameExpr(x))) ExpressionStmt:3( DictExpr:3( IntExpr(1) NameExpr(x) OpExpr:3( or IntExpr(2) IntExpr(1)) OpExpr:3( and IntExpr(2) IntExpr(3))))) [case testImport] import x import y.z.foo, __foo__.bar [out] MypyFile:1( Import:1(x) Import:2(y.z.foo, __foo__.bar)) [case testVariableTypeWithQualifiedName] x = None # type: x.y [out] MypyFile:1( AssignmentStmt:1( NameExpr(x) NameExpr(None) x.y?)) [case testTypeInSignatureWithQualifiedName] def f() -> x.y[a.b.c]: pass [out] MypyFile:1( FuncDef:1( f def () -> x.y?[a.b.c?] Block:1( PassStmt:1()))) [case testImportFrom] from m import x from m.n import x, y, z [out] MypyFile:1( ImportFrom:1(m, [x]) ImportFrom:2(m.n, [x, y, z])) [case testImportFromAs] from m import x as y from x import y, z as a, c as c [out] MypyFile:1( ImportFrom:1(m, [x : y]) ImportFrom:2(x, [y, z : a, c : c])) [case testImportStar] from x import * [out] MypyFile:1( ImportAll:1(x)) [case testImportsInDifferentPlaces] 1 import x def f(): from x import y from z import * [out] MypyFile:1( ExpressionStmt:1( IntExpr(1)) Import:2(x) FuncDef:3( f Block:4( ImportFrom:4(x, [y]) ImportAll:5(z)))) [case testImportWithExtraComma] from x import (y, z,) [out] MypyFile:1( ImportFrom:1(x, [y, z])) [case testDefaultArgs] def f(x=1): pass def g(x, y=1+2, z=(1, 2)): pass [out] MypyFile:1( FuncDef:1( f Args( default( Var(x) IntExpr(1))) Block:2( PassStmt:2())) FuncDef:3( g Args( Var(x) default( Var(y) OpExpr:3( + IntExpr(1) IntExpr(2))) default( Var(z) TupleExpr:3( IntExpr(1) IntExpr(2)))) Block:4( PassStmt:4()))) [case testTryFinally] try: 1 finally: 2 [out] MypyFile:1( TryStmt:1( Block:2( ExpressionStmt:2( IntExpr(1))) Finally( ExpressionStmt:4( IntExpr(2))))) [case testTry] try: 1 except x: 2 [out] MypyFile:1( TryStmt:1( Block:2( ExpressionStmt:2( IntExpr(1))) NameExpr(x) Block:4( ExpressionStmt:4( IntExpr(2))))) [case testComplexTry] try: 1 except x as y: 2 except x.y: 3 [out] MypyFile:1( TryStmt:1( Block:2( ExpressionStmt:2( IntExpr(1))) NameExpr(x) NameExpr(y) Block:4( ExpressionStmt:4( IntExpr(2))) MemberExpr:5( NameExpr(x) y) Block:6( ExpressionStmt:6( IntExpr(3))))) [case testGeneratorExpression] (x for y in z) [out] MypyFile:1( ExpressionStmt:1( GeneratorExpr:1( NameExpr(x) NameExpr(y) NameExpr(z)))) [case testGeneratorExpressionNested] (x for y, (p, q) in z) [out] MypyFile:1( ExpressionStmt:1( GeneratorExpr:1( NameExpr(x) TupleExpr:1( NameExpr(y) TupleExpr:1( NameExpr(p) NameExpr(q))) NameExpr(z)))) [case testListComprehension] x=[x for y in z] [out] MypyFile:1( AssignmentStmt:1( NameExpr(x) ListComprehension:1( GeneratorExpr:1( NameExpr(x) NameExpr(y) NameExpr(z))))) [case testComplexListComprehension] x=[(x, y) for y, z in (1, 2)] [out] MypyFile:1( AssignmentStmt:1( NameExpr(x) ListComprehension:1( GeneratorExpr:1( TupleExpr:1( NameExpr(x) NameExpr(y)) TupleExpr:1( NameExpr(y) NameExpr(z)) TupleExpr:1( IntExpr(1) IntExpr(2)))))) [case testListComprehension2] ([x + 1 for x in a]) [out] MypyFile:1( ExpressionStmt:1( ListComprehension:1( GeneratorExpr:1( OpExpr:1( + NameExpr(x) IntExpr(1)) NameExpr(x) NameExpr(a))))) [case testSlices] x[1:2] x[:1] x[1:] x[:] [out] MypyFile:1( ExpressionStmt:1( IndexExpr:1( NameExpr(x) SliceExpr:1( IntExpr(1) IntExpr(2)))) ExpressionStmt:2( IndexExpr:2( NameExpr(x) SliceExpr:2( IntExpr(1)))) ExpressionStmt:3( IndexExpr:3( NameExpr(x) SliceExpr:3( IntExpr(1) ))) ExpressionStmt:4( IndexExpr:4( NameExpr(x) SliceExpr:4( )))) [case testSliceWithStride] x[1:2:3] x[1::2] x[:1:2] x[::2] x[1:2:] [out] MypyFile:1( ExpressionStmt:1( IndexExpr:1( NameExpr(x) SliceExpr:1( IntExpr(1) IntExpr(2) IntExpr(3)))) ExpressionStmt:2( IndexExpr:2( NameExpr(x) SliceExpr:2( IntExpr(1) IntExpr(2)))) ExpressionStmt:3( IndexExpr:3( NameExpr(x) SliceExpr:3( IntExpr(1) IntExpr(2)))) ExpressionStmt:4( IndexExpr:4( NameExpr(x) SliceExpr:4( IntExpr(2)))) ExpressionStmt:5( IndexExpr:5( NameExpr(x) SliceExpr:5( IntExpr(1) IntExpr(2))))) [case testYield] def f(): yield x + 1 [out] MypyFile:1( FuncDef:1( f Block:2( ExpressionStmt:2( YieldExpr:2( OpExpr:2( + NameExpr(x) IntExpr(1))))))) [case testYieldFrom] def f(): yield from h() [out] MypyFile:1( FuncDef:1( f Block:2( ExpressionStmt:2( YieldFromExpr:2( CallExpr:2( NameExpr(h) Args())))))) [case testYieldFromAssignment] def f(): a = yield from h() [out] MypyFile:1( FuncDef:1( f Block:2( AssignmentStmt:2( NameExpr(a) YieldFromExpr:2( CallExpr:2( NameExpr(h) Args())))))) [case testDel] del x del x[0], y[1] [out] MypyFile:1( DelStmt:1( NameExpr(x)) DelStmt:2( TupleExpr:2( IndexExpr:2( NameExpr(x) IntExpr(0)) IndexExpr:2( NameExpr(y) IntExpr(1))))) [case testExtraCommas] 1, 2, +[1, 2,] f(1,) {1:2,} [out] MypyFile:1( ExpressionStmt:1( TupleExpr:1( IntExpr(1) IntExpr(2))) ExpressionStmt:2( UnaryExpr:2( + ListExpr:2( IntExpr(1) IntExpr(2)))) ExpressionStmt:3( CallExpr:3( NameExpr(f) Args( IntExpr(1)))) ExpressionStmt:4( DictExpr:4( IntExpr(1) IntExpr(2)))) [case testExtraCommaInFunc] def f(x,): pass [out] MypyFile:1( FuncDef:1( f Args( Var(x)) Block:2( PassStmt:2()))) [case testLambda] lambda: 1 lambda x: y + 1 lambda x, y: 1 [out] MypyFile:1( ExpressionStmt:1( LambdaExpr:1( Block:1( ReturnStmt:1( IntExpr(1))))) ExpressionStmt:2( LambdaExpr:2( Args( Var(x)) Block:2( ReturnStmt:2( OpExpr:2( + NameExpr(y) IntExpr(1)))))) ExpressionStmt:3( LambdaExpr:3( Args( Var(x) Var(y)) Block:3( ReturnStmt:3( IntExpr(1)))))) [case testComplexLambda] lambda x=2: x [out] MypyFile:1( ExpressionStmt:1( LambdaExpr:1( Args( default( Var(x) IntExpr(2))) Block:1( ReturnStmt:1( NameExpr(x)))))) [case testLambdaPrecedence] lambda x: 1, 2 [out] MypyFile:1( ExpressionStmt:1( TupleExpr:1( LambdaExpr:1( Args( Var(x)) Block:1( ReturnStmt:1( IntExpr(1)))) IntExpr(2)))) [case testForIndicesInParens] for (i, j) in x: pass [out] MypyFile:1( ForStmt:1( TupleExpr:1( NameExpr(i) NameExpr(j)) NameExpr(x) Block:2( PassStmt:2()))) [case testForAndTrailingCommaAfterIndexVar] for i, in x: pass [out] MypyFile:1( ForStmt:1( TupleExpr:1( NameExpr(i)) NameExpr(x) Block:2( PassStmt:2()))) [case testListComprehensionAndTrailingCommaAfterIndexVar] x = [a for b, in c] [out] MypyFile:1( AssignmentStmt:1( NameExpr(x) ListComprehension:1( GeneratorExpr:1( NameExpr(a) TupleExpr:1( NameExpr(b)) NameExpr(c))))) [case testForAndTrailingCommaAfterIndexVars] for i, j, in x: pass [out] MypyFile:1( ForStmt:1( TupleExpr:1( NameExpr(i) NameExpr(j)) NameExpr(x) Block:2( PassStmt:2()))) [case testGeneratorWithCondition] (x for y in z if 0) [out] MypyFile:1( ExpressionStmt:1( GeneratorExpr:1( NameExpr(x) NameExpr(y) NameExpr(z) IntExpr(0)))) [case testListComprehensionWithCondition] raise [x for y in z if 0] [out] MypyFile:1( RaiseStmt:1( ListComprehension:1( GeneratorExpr:1( NameExpr(x) NameExpr(y) NameExpr(z) IntExpr(0))))) [case testListComprehensionWithConditions] raise [x for y in z if 0 if 1] [out] MypyFile:1( RaiseStmt:1( ListComprehension:1( GeneratorExpr:1( NameExpr(x) NameExpr(y) NameExpr(z) IntExpr(0) IntExpr(1))))) [case testListComprehensionWithCrazyConditions] raise [x for y in z if (1 if 2 else 3) if 1] [out] MypyFile:1( RaiseStmt:1( ListComprehension:1( GeneratorExpr:1( NameExpr(x) NameExpr(y) NameExpr(z) ConditionalExpr:1( Condition( IntExpr(2)) IntExpr(1) IntExpr(3)) IntExpr(1))))) [case testDictionaryComprehension] a = {x: y for x, y in xys} [out] MypyFile:1( AssignmentStmt:1( NameExpr(a) DictionaryComprehension:1( NameExpr(x) NameExpr(y) TupleExpr:1( NameExpr(x) NameExpr(y)) NameExpr(xys)))) [case testDictionaryComprehensionComplex] a = {x: y for x, y in xys for p, q in pqs if c} [out] MypyFile:1( AssignmentStmt:1( NameExpr(a) DictionaryComprehension:1( NameExpr(x) NameExpr(y) TupleExpr:1( NameExpr(x) NameExpr(y)) TupleExpr:1( NameExpr(p) NameExpr(q)) NameExpr(xys) NameExpr(pqs) NameExpr(c)))) [case testSetComprehension] a = {i for i in l} [out] MypyFile:1( AssignmentStmt:1( NameExpr(a) SetComprehension:1( GeneratorExpr:1( NameExpr(i) NameExpr(i) NameExpr(l))))) [case testSetComprehensionComplex] a = {x + p for x in xys for p in pqs if c} [out] MypyFile:1( AssignmentStmt:1( NameExpr(a) SetComprehension:1( GeneratorExpr:1( OpExpr:1( + NameExpr(x) NameExpr(p)) NameExpr(x) NameExpr(p) NameExpr(xys) NameExpr(pqs) NameExpr(c))))) [case testWithStatement] with open('foo') as f: pass [out] MypyFile:1( WithStmt:1( Expr( CallExpr:1( NameExpr(open) Args( StrExpr(foo)))) Target( NameExpr(f)) Block:2( PassStmt:2()))) [case testWithStatementWithoutTarget] with foo: pass [out] MypyFile:1( WithStmt:1( Expr( NameExpr(foo)) Block:2( PassStmt:2()))) [case testHexOctBinLiterals] 0xa, 0Xaf, 0o7, 0O12, 0b1, 0B101 [out] MypyFile:1( ExpressionStmt:1( TupleExpr:1( IntExpr(10) IntExpr(175) IntExpr(7) IntExpr(10) IntExpr(1) IntExpr(5)))) [case testImportFromWithParens] from x import (y) from x import (y, z) [out] MypyFile:1( ImportFrom:1(x, [y]) ImportFrom:2(x, [y, z])) [case testContinueStmt] while 1: continue [out] MypyFile:1( WhileStmt:1( IntExpr(1) Block:2( ContinueStmt:2()))) [case testStrLiteralConcatenate] 'f' 'bar' ('x' 'y' 'z') [out] MypyFile:1( ExpressionStmt:1( StrExpr(fbar)) ExpressionStmt:2( StrExpr(xyz))) [case testCatchAllExcept] try: 1 except: pass try: 1 except x: pass except: 2 [out] MypyFile:1( TryStmt:1( Block:2( ExpressionStmt:2( IntExpr(1))) Block:4( PassStmt:4())) TryStmt:5( Block:6( ExpressionStmt:6( IntExpr(1))) NameExpr(x) Block:8( PassStmt:8()) Block:10( ExpressionStmt:10( IntExpr(2))))) [case testTryElse] try: pass except x: 1 else: 2 [out] MypyFile:1( TryStmt:1( Block:2( PassStmt:2()) NameExpr(x) Block:4( ExpressionStmt:4( IntExpr(1))) Else( ExpressionStmt:6( IntExpr(2))))) [case testExceptWithMultipleTypes] try: pass except (x, y): pass except (a, b, c) as e: pass [out] MypyFile:1( TryStmt:1( Block:2( PassStmt:2()) TupleExpr:3( NameExpr(x) NameExpr(y)) Block:4( PassStmt:4()) TupleExpr:5( NameExpr(a) NameExpr(b) NameExpr(c)) NameExpr(e) Block:6( PassStmt:6()))) [case testNestedFunctions] def f(): def g(): pass def h() -> int: def g() -> int: pass [out] MypyFile:1( FuncDef:1( f Block:2( FuncDef:2( g Block:3( PassStmt:3())))) FuncDef:4( h def () -> int? Block:5( FuncDef:5( g def () -> int? Block:6( PassStmt:6()))))) [case testStatementsAndDocStringsInClassBody] class A: "doc string" x = y def f(self): pass [out] MypyFile:1( ClassDef:1( A ExpressionStmt:2( StrExpr(doc string)) AssignmentStmt:3( NameExpr(x) NameExpr(y)) FuncDef:4( f Args( Var(self)) Block:5( PassStmt:5())))) [case testSingleLineClass] class a: pass [out] MypyFile:1( ClassDef:1( a PassStmt:1())) [case testDecorator] @property def f(): pass [out] MypyFile:1( Decorator:1( Var(f) NameExpr(property) FuncDef:2( f Block:3( PassStmt:3())))) [case testComplexDecorator] @foo(bar, 1) @zar def f() -> int: pass [out] MypyFile:1( Decorator:1( Var(f) CallExpr:1( NameExpr(foo) Args( NameExpr(bar) IntExpr(1))) NameExpr(zar) FuncDef:3( f def () -> int? Block:4( PassStmt:4())))) [case testKeywordArgInCall] f(x=1) [out] MypyFile:1( ExpressionStmt:1( CallExpr:1( NameExpr(f) Args() KwArgs( x IntExpr(1))))) [case testComplexKeywordArgs] f(x, y=1 or 2, z=y) [out] MypyFile:1( ExpressionStmt:1( CallExpr:1( NameExpr(f) Args( NameExpr(x)) KwArgs( y OpExpr:1( or IntExpr(1) IntExpr(2))) KwArgs( z NameExpr(y))))) [case testChainedAssignment] x = z = 1 [out] MypyFile:1( AssignmentStmt:1( Lvalues( NameExpr(x) NameExpr(z)) IntExpr(1))) [case testVarArgs] def f(x, *a): pass f(1, *2) [out] MypyFile:1( FuncDef:1( f Args( Var(x)) VarArg( Var(a)) Block:1( PassStmt:1())) ExpressionStmt:2( CallExpr:2( NameExpr(f) Args( IntExpr(1) IntExpr(2)) VarArg))) [case testVarArgWithType] def f(x: str, *a: int): pass [out] MypyFile:1( FuncDef:1( f Args( Var(x)) def (x: str?, *a: int?) -> Any VarArg( Var(a)) Block:1( PassStmt:1()))) [case testDictVarArgs] def f(x, **a): pass [out] MypyFile:1( FuncDef:1( f Args( Var(x)) DictVarArg( Var(a)) Block:1( PassStmt:1()))) [case testBothVarArgs] def f(x, *a, **b): pass def g(*a, **b): pass [out] MypyFile:1( FuncDef:1( f Args( Var(x)) VarArg( Var(a)) DictVarArg( Var(b)) Block:1( PassStmt:1())) FuncDef:2( g VarArg( Var(a)) DictVarArg( Var(b)) Block:2( PassStmt:2()))) [case testDictVarArgsWithType] def f(x: X, **a: A) -> None: pass [out] MypyFile:1( FuncDef:1( f Args( Var(x)) def (x: X?, **a: A?) -> None? DictVarArg( Var(a)) Block:1( PassStmt:1()))) [case testCallDictVarArgs] f(**x) f(x, **y) f(*x, **y) f(x, *y, **z) [out] MypyFile:1( ExpressionStmt:1( CallExpr:1( NameExpr(f) Args() DictVarArg( NameExpr(x)))) ExpressionStmt:2( CallExpr:2( NameExpr(f) Args( NameExpr(x)) DictVarArg( NameExpr(y)))) ExpressionStmt:3( CallExpr:3( NameExpr(f) Args( NameExpr(x)) VarArg DictVarArg( NameExpr(y)))) ExpressionStmt:4( CallExpr:4( NameExpr(f) Args( NameExpr(x) NameExpr(y)) VarArg DictVarArg( NameExpr(z))))) [case testAssert] assert x == y [out] MypyFile:1( AssertStmt:1( ComparisonExpr:1( == NameExpr(x) NameExpr(y)))) [case testYieldWithoutExpressions] def f(): yield [out] MypyFile:1( FuncDef:1( f Block:2( ExpressionStmt:2( YieldExpr:2())))) [case testConditionalExpression] x if y else z [out] MypyFile:1( ExpressionStmt:1( ConditionalExpr:1( Condition( NameExpr(y)) NameExpr(x) NameExpr(z)))) [case testConditionalExpressionInListComprehension] a = [x if y else z for a in b] [out] MypyFile:1( AssignmentStmt:1( NameExpr(a) ListComprehension:1( GeneratorExpr:1( ConditionalExpr:1( Condition( NameExpr(y)) NameExpr(x) NameExpr(z)) NameExpr(a) NameExpr(b))))) [case testConditionalExpressionInTuple] 1 if 2 else 3, 4 [out] MypyFile:1( ExpressionStmt:1( TupleExpr:1( ConditionalExpr:1( Condition( IntExpr(2)) IntExpr(1) IntExpr(3)) IntExpr(4)))) [case testSetLiteral] {x or y} {1, 2} [out] MypyFile:1( ExpressionStmt:1( SetExpr:1( OpExpr:1( or NameExpr(x) NameExpr(y)))) ExpressionStmt:2( SetExpr:2( IntExpr(1) IntExpr(2)))) [case testSetLiteralWithExtraComma] {x,} [out] MypyFile:1( ExpressionStmt:1( SetExpr:1( NameExpr(x)))) [case testImportAs] import x as y import x, z as y, a.b as c, d as d [out] MypyFile:1( Import:1(x : y) Import:2(x, z : y, a.b : c, d : d)) [case testForAndElse] for x in y: pass else: x [out] MypyFile:1( ForStmt:1( NameExpr(x) NameExpr(y) Block:2( PassStmt:2()) Else( ExpressionStmt:4( NameExpr(x))))) [case testWhileAndElse] while x: pass else: y [out] MypyFile:1( WhileStmt:1( NameExpr(x) Block:2( PassStmt:2()) Else( ExpressionStmt:4( NameExpr(y))))) [case testWithAndMultipleOperands] with x as y, a as b: pass with x(), y(): pass [out] MypyFile:1( WithStmt:1( Expr( NameExpr(x)) Target( NameExpr(y)) Expr( NameExpr(a)) Target( NameExpr(b)) Block:2( PassStmt:2())) WithStmt:3( Expr( CallExpr:3( NameExpr(x) Args())) Expr( CallExpr:3( NameExpr(y) Args())) Block:4( PassStmt:4()))) [case testOperatorAssignment] x += 1 x -= 1 x *= 1 x /= 1 x //= 1 x %= 1 x **= 1 x |= 1 x &= 1 x ^= 1 x >>= 1 x <<= 1 [out] MypyFile:1( OperatorAssignmentStmt:1( + NameExpr(x) IntExpr(1)) OperatorAssignmentStmt:2( - NameExpr(x) IntExpr(1)) OperatorAssignmentStmt:3( * NameExpr(x) IntExpr(1)) OperatorAssignmentStmt:4( / NameExpr(x) IntExpr(1)) OperatorAssignmentStmt:5( // NameExpr(x) IntExpr(1)) OperatorAssignmentStmt:6( % NameExpr(x) IntExpr(1)) OperatorAssignmentStmt:7( ** NameExpr(x) IntExpr(1)) OperatorAssignmentStmt:8( | NameExpr(x) IntExpr(1)) OperatorAssignmentStmt:9( & NameExpr(x) IntExpr(1)) OperatorAssignmentStmt:10( ^ NameExpr(x) IntExpr(1)) OperatorAssignmentStmt:11( >> NameExpr(x) IntExpr(1)) OperatorAssignmentStmt:12( << NameExpr(x) IntExpr(1))) [case testNestedClasses] class A: class B: pass class C: pass [out] MypyFile:1( ClassDef:1( A ClassDef:2( B PassStmt:3()) ClassDef:4( C PassStmt:5()))) [case testTryWithExceptAndFinally] try: pass except x: x finally: y [out] MypyFile:1( TryStmt:1( Block:2( PassStmt:2()) NameExpr(x) Block:4( ExpressionStmt:4( NameExpr(x))) Finally( ExpressionStmt:6( NameExpr(y))))) [case testBareAsteriskInFuncDef] def f(x, *, y=1): pass [out] MypyFile:1( FuncDef:1( f MaxPos(1) Args( Var(x) default( Var(y) IntExpr(1))) Block:1( PassStmt:1()))) [case testBareAsteriskInFuncDefWithSignature] def f(x: A, *, y: B = 1) -> None: pass [out] MypyFile:1( FuncDef:1( f MaxPos(1) Args( Var(x) default( Var(y) IntExpr(1))) def (x: A?, *, y: B? =) -> None? Block:1( PassStmt:1()))) [case testBareAsteriskNamedDefault] def f(*, y: B = 1) -> None: pass [out] MypyFile:1( FuncDef:1( f MaxPos(0) Args( default( Var(y) IntExpr(1))) def (*, y: B? =) -> None? Block:1( PassStmt:1()))) [case testBareAsteriskNamedNoDefault] def f(*, y: B) -> None: pass [out] MypyFile:1( FuncDef:1( f MaxPos(0) Args( Var(y)) def (*, y: B?) -> None? Block:1( PassStmt:1()))) [case testSuperExpr] super().x [out] MypyFile:1( ExpressionStmt:1( SuperExpr:1( x CallExpr:1( NameExpr(super) Args())))) [case testKeywordAndDictArgs] f(x = y, **kwargs) [out] MypyFile:1( ExpressionStmt:1( CallExpr:1( NameExpr(f) Args() KwArgs( x NameExpr(y)) DictVarArg( NameExpr(kwargs))))) [case testSimpleFunctionType] f = None # type: Callable[[], None] [out] MypyFile:1( AssignmentStmt:1( NameExpr(f) NameExpr(None) Callable?[, None?])) [case testFunctionTypeWithArgument] f = None # type: Callable[[str], int] [out] MypyFile:1( AssignmentStmt:1( NameExpr(f) NameExpr(None) Callable?[, int?])) [case testFunctionTypeWithTwoArguments] f = None # type: Callable[[a[b], x.y], List[int]] [out] MypyFile:1( AssignmentStmt:1( NameExpr(f) NameExpr(None) Callable?[, List?[int?]])) [case testFunctionTypeWithExtraComma] def f(x: Callable[[str,], int]): pass [out] MypyFile:1( FuncDef:1( f Args( Var(x)) def (x: Callable?[, int?]) -> Any Block:1( PassStmt:1()))) [case testSimpleStringLiteralType] def f() -> 'A': pass [out] MypyFile:1( FuncDef:1( f def () -> A? Block:1( PassStmt:1()))) [case testGenericStringLiteralType] def f() -> 'A[B, C]': pass [out] MypyFile:1( FuncDef:1( f def () -> A?[B?, C?] Block:1( PassStmt:1()))) [case testPartialStringLiteralType] def f() -> A['B', C]: pass [out] MypyFile:1( FuncDef:1( f def () -> A?[B?, C?] Block:1( PassStmt:1()))) [case testWhitespaceInStringLiteralType] def f() -> ' A [ X ] ': pass [out] MypyFile:1( FuncDef:1( f def () -> A?[X?] Block:1( PassStmt:1()))) [case testEscapeInStringLiteralType] def f() -> '\x41': pass [out] MypyFile:1( FuncDef:1( f def () -> A? Block:1( PassStmt:1()))) [case testBytesLiteralType] x: Literal[b'foo\xfe\x00'] [out] MypyFile:1( AssignmentStmt:1( NameExpr(x) TempNode:1( Any) Literal?[b'foo\xfe\x00'])) [case testMetaclass] class Foo(metaclass=Bar): pass [out] MypyFile:1( ClassDef:1( Foo Keywords(metaclass=NameExpr(Bar)) Metaclass(NameExpr(Bar)) PassStmt:1())) [case testQualifiedMetaclass] class Foo(metaclass=foo.Bar): pass [out] MypyFile:1( ClassDef:1( Foo Keywords(metaclass=MemberExpr:1( NameExpr(foo) Bar)) Metaclass(MemberExpr:1( NameExpr(foo) Bar)) PassStmt:1())) [case testBaseAndMetaclass] class Foo(foo.bar[x], metaclass=Bar): pass [out] MypyFile:1( ClassDef:1( Foo Keywords(metaclass=NameExpr(Bar)) Metaclass(NameExpr(Bar)) BaseTypeExpr( IndexExpr:1( MemberExpr:1( NameExpr(foo) bar) NameExpr(x))) PassStmt:1())) [case testClassKeywordArgs] class Foo(_root=None): pass [out] MypyFile:1( ClassDef:1( Foo Keywords(_root=NameExpr(None)) PassStmt:1())) [case testClassKeywordArgsBeforeMeta] class Foo(_root=None, metaclass=Bar): pass [out] MypyFile:1( ClassDef:1( Foo Keywords(_root=NameExpr(None), metaclass=NameExpr(Bar)) Metaclass(NameExpr(Bar)) PassStmt:1())) [case testClassKeywordArgsAfterMeta] class Foo(metaclass=Bar, _root=None): pass [out] MypyFile:1( ClassDef:1( Foo Keywords(metaclass=NameExpr(Bar), _root=NameExpr(None)) Metaclass(NameExpr(Bar)) PassStmt:1())) [case testNamesThatAreNoLongerKeywords] any = interface [out] MypyFile:1( AssignmentStmt:1( NameExpr(any) NameExpr(interface))) [case testFunctionOverload] @overload def f() -> x: pass @overload def f() -> y: pass [out] MypyFile:1( OverloadedFuncDef:1( Decorator:1( Var(f) NameExpr(overload) FuncDef:2( f def () -> x? Block:2( PassStmt:2()))) Decorator:3( Var(f) NameExpr(overload) FuncDef:4( f def () -> y? Block:4( PassStmt:4()))))) [case testFunctionOverloadAndOtherStatements] x @overload def f() -> x: pass @overload def f() -> y: pass x [out] MypyFile:1( ExpressionStmt:1( NameExpr(x)) OverloadedFuncDef:2( Decorator:2( Var(f) NameExpr(overload) FuncDef:3( f def () -> x? Block:3( PassStmt:3()))) Decorator:4( Var(f) NameExpr(overload) FuncDef:5( f def () -> y? Block:5( PassStmt:5())))) ExpressionStmt:6( NameExpr(x))) [case testFunctionOverloadWithThreeVariants] @overload def f() -> x: pass @overload def f() -> y: pass @overload def f(y): pass [out] MypyFile:1( OverloadedFuncDef:1( Decorator:1( Var(f) NameExpr(overload) FuncDef:2( f def () -> x? Block:2( PassStmt:2()))) Decorator:3( Var(f) NameExpr(overload) FuncDef:4( f def () -> y? Block:4( PassStmt:4()))) Decorator:5( Var(f) NameExpr(overload) FuncDef:6( f Args( Var(y)) Block:6( PassStmt:6()))))) [case testDecoratorsThatAreNotOverloads] @foo def f() -> x: pass @foo def g() -> y: pass [out] MypyFile:1( Decorator:1( Var(f) NameExpr(foo) FuncDef:2( f def () -> x? Block:2( PassStmt:2()))) Decorator:3( Var(g) NameExpr(foo) FuncDef:4( g def () -> y? Block:4( PassStmt:4())))) [case testFunctionOverloadWithinFunction] def f(): @overload def g(): pass @overload def g() -> x: pass [out] MypyFile:1( FuncDef:1( f Block:2( OverloadedFuncDef:2( Decorator:2( Var(g) NameExpr(overload) FuncDef:3( g Block:3( PassStmt:3()))) Decorator:4( Var(g) NameExpr(overload) FuncDef:5( g def () -> x? Block:5( PassStmt:5()))))))) [case testConditionalOverload1] @overload def foo(x: int): ... if sys.version_info[0] > 2: @overload def foo(x: str): ... def foo(x): pass [out] MypyFile:1( IfStmt:3( If( ComparisonExpr:3( > IndexExpr:3( MemberExpr:3( NameExpr(sys) version_info) IntExpr(0)) IntExpr(2))) Then() Else()) OverloadedFuncDef:1( Decorator:1( Var(foo) NameExpr(overload) FuncDef:2( foo Args( Var(x)) def (x: int?) -> Any Block:2( ExpressionStmt:2( Ellipsis)))) Decorator:4( Var(foo) NameExpr(overload) FuncDef:5( foo Args( Var(x)) def (x: str?) -> Any Block:5( ExpressionStmt:5( Ellipsis)))) FuncDef:6( foo Args( Var(x)) Block:6( PassStmt:6())))) [case testConditionalOverload2] @overload def foo(x: int): ... if sys.platform == "foo": @overload def foo(x: str): ... def foo(x): pass [out] MypyFile:1( IfStmt:3( If( ComparisonExpr:3( == MemberExpr:3( NameExpr(sys) platform) StrExpr(foo))) Then()) OverloadedFuncDef:1( Decorator:1( Var(foo) NameExpr(overload) FuncDef:2( foo Args( Var(x)) def (x: int?) -> Any Block:2( ExpressionStmt:2( Ellipsis)))) FuncDef:6( foo Args( Var(x)) Block:6( PassStmt:6())))) [case testConditionalOverload3] if sys.version_info[0] > 2: @overload def foo(x: str): ... @overload def foo(x: int): ... def foo(x): pass [out] MypyFile:1( IfStmt:1( If( ComparisonExpr:1( > IndexExpr:1( MemberExpr:1( NameExpr(sys) version_info) IntExpr(0)) IntExpr(2))) Then() Else()) OverloadedFuncDef:2( Decorator:2( Var(foo) NameExpr(overload) FuncDef:3( foo Args( Var(x)) def (x: str?) -> Any Block:3( ExpressionStmt:3( Ellipsis)))) Decorator:4( Var(foo) NameExpr(overload) FuncDef:5( foo Args( Var(x)) def (x: int?) -> Any Block:5( ExpressionStmt:5( Ellipsis)))) FuncDef:6( foo Args( Var(x)) Block:6( PassStmt:6())))) [case testCommentFunctionAnnotation] def f(): # type: () -> A pass def g(x): # type: (A) -> B pass [out] MypyFile:1( FuncDef:1( f def () -> A? Block:2( PassStmt:2())) FuncDef:3( g Args( Var(x)) def (x: A?) -> B? Block:4( PassStmt:4()))) [case testCommentMethodAnnotation] class A: def f(self): # type: () -> A pass def g(xself, x): # type: (A) -> B pass [out] MypyFile:1( ClassDef:1( A FuncDef:2( f Args( Var(self)) def (self: Any) -> A? Block:3( PassStmt:3())) FuncDef:4( g Args( Var(xself) Var(x)) def (xself: Any, x: A?) -> B? Block:5( PassStmt:5())))) [case testCommentMethodAnnotationAndNestedFunction] class A: def f(self): # type: () -> A def g(x): # type: (A) -> B pass [out] MypyFile:1( ClassDef:1( A FuncDef:2( f Args( Var(self)) def (self: Any) -> A? Block:3( FuncDef:3( g Args( Var(x)) def (x: A?) -> B? Block:4( PassStmt:4())))))) [case testCommentFunctionAnnotationOnSeparateLine] def f(x): # type: (X) -> Y pass [out] MypyFile:1( FuncDef:1( f Args( Var(x)) def (x: X?) -> Y? Block:3( PassStmt:3()))) [case testCommentFunctionAnnotationOnSeparateLine2] def f(x): # type: (X) -> Y # bar pass [out] MypyFile:1( FuncDef:1( f Args( Var(x)) def (x: X?) -> Y? Block:5( PassStmt:5()))) [case testCommentFunctionAnnotationAndVarArg] def f(x, *y): # type: (X, *Y) -> Z pass [out] MypyFile:1( FuncDef:1( f Args( Var(x)) def (x: X?, *y: Y?) -> Z? VarArg( Var(y)) Block:2( PassStmt:2()))) [case testCommentFunctionAnnotationAndAllVarArgs] def f(x, *y, **z): # type: (X, *Y, **Z) -> A pass [out] MypyFile:1( FuncDef:1( f Args( Var(x)) def (x: X?, *y: Y?, **z: Z?) -> A? VarArg( Var(y)) DictVarArg( Var(z)) Block:2( PassStmt:2()))) [case testClassDecorator] @foo class X: pass @foo(bar) @x.y class Z: pass [out] MypyFile:1( ClassDef:2( X Decorators( NameExpr(foo)) PassStmt:2()) ClassDef:5( Z Decorators( CallExpr:3( NameExpr(foo) Args( NameExpr(bar))) MemberExpr:4( NameExpr(x) y)) PassStmt:5())) [case testTrailingSemicolon] def x(): pass; def y(): pass [out] MypyFile:1( FuncDef:1( x Block:2( PassStmt:2())) FuncDef:4( y Block:5( PassStmt:5()))) [case testEmptySuperClass] class A(): pass [out] MypyFile:1( ClassDef:1( A PassStmt:2())) [case testStarExpression] *a *a, b a, *b a, (*x, y) a, (x, *y) [out] MypyFile:1( ExpressionStmt:1( StarExpr:1( NameExpr(a))) ExpressionStmt:2( TupleExpr:2( StarExpr:2( NameExpr(a)) NameExpr(b))) ExpressionStmt:3( TupleExpr:3( NameExpr(a) StarExpr:3( NameExpr(b)))) ExpressionStmt:4( TupleExpr:4( NameExpr(a) TupleExpr:4( StarExpr:4( NameExpr(x)) NameExpr(y)))) ExpressionStmt:5( TupleExpr:5( NameExpr(a) TupleExpr:5( NameExpr(x) StarExpr:5( NameExpr(y)))))) [case testStarExpressionParenthesis] *(a) *(a,b) [out] MypyFile:1( ExpressionStmt:1( StarExpr:1( NameExpr(a))) ExpressionStmt:2( StarExpr:2( TupleExpr:2( NameExpr(a) NameExpr(b))))) [case testStarExpressionInFor] for *a in b: pass for a, *b in c: pass for *a, b in c: pass [out] MypyFile:1( ForStmt:1( StarExpr:1( NameExpr(a)) NameExpr(b) Block:2( PassStmt:2())) ForStmt:4( TupleExpr:4( NameExpr(a) StarExpr:4( NameExpr(b))) NameExpr(c) Block:5( PassStmt:5())) ForStmt:7( TupleExpr:7( StarExpr:7( NameExpr(a)) NameExpr(b)) NameExpr(c) Block:8( PassStmt:8()))) [case testStarExprInGeneratorExpr] (x for y, *p in z) (x for *p, y in z) (x for y, *p, q in z) [out] MypyFile:1( ExpressionStmt:1( GeneratorExpr:1( NameExpr(x) TupleExpr:1( NameExpr(y) StarExpr:1( NameExpr(p))) NameExpr(z))) ExpressionStmt:2( GeneratorExpr:2( NameExpr(x) TupleExpr:2( StarExpr:2( NameExpr(p)) NameExpr(y)) NameExpr(z))) ExpressionStmt:3( GeneratorExpr:3( NameExpr(x) TupleExpr:3( NameExpr(y) StarExpr:3( NameExpr(p)) NameExpr(q)) NameExpr(z)))) [case testParseNamedtupleBaseclass] class A(namedtuple('x', ['y'])): pass [out] MypyFile:1( ClassDef:1( A BaseTypeExpr( CallExpr:1( NameExpr(namedtuple) Args( StrExpr(x) ListExpr:1( StrExpr(y))))) PassStmt:1())) [case testEllipsis] ... a[1,...,2] ....__class__ [out] MypyFile:1( ExpressionStmt:1( Ellipsis) ExpressionStmt:2( IndexExpr:2( NameExpr(a) TupleExpr:2( IntExpr(1) Ellipsis IntExpr(2)))) ExpressionStmt:3( MemberExpr:3( Ellipsis __class__))) [case testFunctionWithManyKindsOfArgs] def f(x, *args, y=None, **kw): pass [out] MypyFile:1( FuncDef:1( f MaxPos(1) Args( Var(x) default( Var(y) NameExpr(None))) VarArg( Var(args)) DictVarArg( Var(kw)) Block:1( PassStmt:1()))) [case testIfWithSemicolons] if 1: a; b [out] MypyFile:1( IfStmt:1( If( IntExpr(1)) Then( ExpressionStmt:1( NameExpr(a)) ExpressionStmt:1( NameExpr(b))))) [case testIfWithSemicolonsNested] while 2: if 1: a; b [out] MypyFile:1( WhileStmt:1( IntExpr(2) Block:2( IfStmt:2( If( IntExpr(1)) Then( ExpressionStmt:2( NameExpr(a)) ExpressionStmt:2( NameExpr(b))))))) [case testIfElseWithSemicolons] if 1: global x; y = 1 else: x = 1; return 3 4 [out] MypyFile:1( IfStmt:1( If( IntExpr(1)) Then( GlobalDecl:1( x) AssignmentStmt:1( NameExpr(y) IntExpr(1))) Else( AssignmentStmt:2( NameExpr(x) IntExpr(1)) ReturnStmt:2( IntExpr(3)))) ExpressionStmt:3( IntExpr(4))) [case testIfElseWithSemicolonsNested] while 2: if 1: global x; y = 1 else: x = 1; return 3 4 [out] MypyFile:1( WhileStmt:1( IntExpr(2) Block:2( IfStmt:2( If( IntExpr(1)) Then( GlobalDecl:2( x) AssignmentStmt:2( NameExpr(y) IntExpr(1))) Else( AssignmentStmt:3( NameExpr(x) IntExpr(1)) ReturnStmt:3( IntExpr(3)))))) ExpressionStmt:4( IntExpr(4))) [case testKeywordArgumentAfterStarArgumentInCall] f(x=1, *y) [out] MypyFile:1( ExpressionStmt:1( CallExpr:1( NameExpr(f) Args( NameExpr(y)) VarArg KwArgs( x IntExpr(1))))) [case testConditionalExpressionInSetComprehension] { 1 if x else 2 for x in y } [out] MypyFile:1( ExpressionStmt:1( SetComprehension:1( GeneratorExpr:1( ConditionalExpr:1( Condition( NameExpr(x)) IntExpr(1) IntExpr(2)) NameExpr(x) NameExpr(y))))) [case testConditionalExpressionInListComprehension2] a = [ 1 if x else 2 for x in y ] [out] MypyFile:1( AssignmentStmt:1( NameExpr(a) ListComprehension:1( GeneratorExpr:1( ConditionalExpr:1( Condition( NameExpr(x)) IntExpr(1) IntExpr(2)) NameExpr(x) NameExpr(y))))) [case testComplexWithLvalue] with x as y.z: pass [out] MypyFile:1( WithStmt:1( Expr( NameExpr(x)) Target( MemberExpr:1( NameExpr(y) z)) Block:1( PassStmt:1()))) [case testRelativeImportWithEllipsis] from ... import x [out] MypyFile:1( ImportFrom:1(..., [x])) [case testRelativeImportWithEllipsis2] from .... import x [out] MypyFile:1( ImportFrom:1(...., [x])) [case testParseExtendedSlicing] a[:, :] [out] MypyFile:1( ExpressionStmt:1( IndexExpr:1( NameExpr(a) TupleExpr:1( SliceExpr:1( ) SliceExpr:1( ))))) [case testParseExtendedSlicing2] a[1:2:, :,] [out] MypyFile:1( ExpressionStmt:1( IndexExpr:1( NameExpr(a) TupleExpr:1( SliceExpr:1( IntExpr(1) IntExpr(2)) SliceExpr:1( ))))) [case testParseExtendedSlicing3] a[1:2:3, ..., 1] [out] MypyFile:1( ExpressionStmt:1( IndexExpr:1( NameExpr(a) TupleExpr:1( SliceExpr:1( IntExpr(1) IntExpr(2) IntExpr(3)) Ellipsis IntExpr(1))))) [case testParseExtendedSlicing4] m[*index, :] [out] main:1: error: Invalid syntax [out version>=3.11] MypyFile:1( ExpressionStmt:1( IndexExpr:1( NameExpr(m) TupleExpr:1( StarExpr:1( NameExpr(index)) SliceExpr:1( ))))) [case testParseIfExprInDictExpr] test = { 'spam': 'eggs' if True else 'bacon' } [out] MypyFile:1( AssignmentStmt:1( NameExpr(test) DictExpr:1( StrExpr(spam) ConditionalExpr:1( Condition( NameExpr(True)) StrExpr(eggs) StrExpr(bacon))))) [case testIgnoreLine] import x # type: ignore [out] MypyFile:1( Import:1(x) IgnoredLines(1)) [case testIgnore2Lines] x y # type: ignore z # type: ignore [out] MypyFile:1( ExpressionStmt:1( NameExpr(x)) ExpressionStmt:2( NameExpr(y)) ExpressionStmt:3( NameExpr(z)) IgnoredLines(2, 3)) [case testCommentedOutIgnoreAnnotation] y ## type: ignore [out] MypyFile:1( ExpressionStmt:1( NameExpr(y))) [case testSpaceInIgnoreAnnotations] y # type: ignore # foo y #type:ignore [out] MypyFile:1( ExpressionStmt:1( NameExpr(y)) ExpressionStmt:2( NameExpr(y)) IgnoredLines(1, 2)) [case testIgnoreAnnotationAndMultilineStatement] x = { 1: 2 # type: ignore } y = { # type: ignore 1: 2 } # type: ignore [out] MypyFile:1( AssignmentStmt:1( NameExpr(x) DictExpr:1( IntExpr(1) IntExpr(2))) AssignmentStmt:4( NameExpr(y) DictExpr:4( IntExpr(1) IntExpr(2))) IgnoredLines(2, 4, 6)) [case testIgnoreAnnotationAndMultilineStatement2] from m import ( # type: ignore x, y ) [out] MypyFile:1( ImportFrom:1(m, [x, y]) IgnoredLines(1)) [case testYieldExpression] def f(): x = yield f() [out] MypyFile:1( FuncDef:1( f Block:2( AssignmentStmt:2( NameExpr(x) YieldExpr:2( CallExpr:2( NameExpr(f) Args())))))) [case testForWithSingleItemTuple] for x in 1,: pass [out] MypyFile:1( ForStmt:1( NameExpr(x) TupleExpr:1( IntExpr(1)) Block:1( PassStmt:1()))) [case testIsoLatinUnixEncoding] # coding: iso-latin-1-unix [out] MypyFile:1() [case testLatinUnixEncoding] # coding: latin-1-unix [out] MypyFile:1() [case testLatinUnixEncoding2] # coding: iso-latin-1 [out] MypyFile:1() [case testYieldExpressionInParens] def f(): (yield) [out] MypyFile:1( FuncDef:1( f Block:2( ExpressionStmt:2( YieldExpr:2())))) [case testFStringSimple] x = 'mypy' f'Hello {x}' [out] MypyFile:1( AssignmentStmt:1( NameExpr(x) StrExpr(mypy)) ExpressionStmt:2( CallExpr:2( MemberExpr:2( StrExpr() join) Args( ListExpr:2( StrExpr(Hello ) CallExpr:2( MemberExpr:2( StrExpr({:{}}) format) Args( NameExpr(x) StrExpr()))))))) [case testFStringWithConversion] x = 'mypy' F'Hello {x!r}' [out] MypyFile:1( AssignmentStmt:1( NameExpr(x) StrExpr(mypy)) ExpressionStmt:2( CallExpr:2( MemberExpr:2( StrExpr() join) Args( ListExpr:2( StrExpr(Hello ) CallExpr:2( MemberExpr:2( StrExpr({!r:{}}) format) Args( NameExpr(x) StrExpr()))))))) [case testFStringWithOnlyFormatSpecifier] x = 'mypy' f'Hello {x:<30}' [out] MypyFile:1( AssignmentStmt:1( NameExpr(x) StrExpr(mypy)) ExpressionStmt:2( CallExpr:2( MemberExpr:2( StrExpr() join) Args( ListExpr:2( StrExpr(Hello ) CallExpr:2( MemberExpr:2( StrExpr({:{}}) format) Args( NameExpr(x) StrExpr(<30)))))))) [case testFStringWithFormatSpecifierAndConversion] x = 'mypy' f'Hello {x!s:<30}' [out] MypyFile:1( AssignmentStmt:1( NameExpr(x) StrExpr(mypy)) ExpressionStmt:2( CallExpr:2( MemberExpr:2( StrExpr() join) Args( ListExpr:2( StrExpr(Hello ) CallExpr:2( MemberExpr:2( StrExpr({!s:{}}) format) Args( NameExpr(x) StrExpr(<30)))))))) [case testFStringWithFormatSpecifierExpression] x = 'mypy' y = 30 f'Hello {x!s:<{y+y}}' [out] MypyFile:1( AssignmentStmt:1( NameExpr(x) StrExpr(mypy)) AssignmentStmt:2( NameExpr(y) IntExpr(30)) ExpressionStmt:3( CallExpr:3( MemberExpr:3( StrExpr() join) Args( ListExpr:3( StrExpr(Hello ) CallExpr:3( MemberExpr:3( StrExpr({!s:{}}) format) Args( NameExpr(x) CallExpr:3( MemberExpr:3( StrExpr() join) Args( ListExpr:3( StrExpr(<) CallExpr:3( MemberExpr:3( StrExpr({:{}}) format) Args( OpExpr:3( + NameExpr(y) NameExpr(y)) StrExpr())))))))))))) [case testStripFunctionBodiesIfIgnoringErrors] # mypy: ignore-errors=True def f(self): self.x = 1 # Cannot define an attribute return 1 [out] MypyFile:1( FuncDef:2( f Args( Var(self)) Block:3())) [case testStripMethodBodiesIfIgnoringErrors] # mypy: ignore-errors=True class C: def f(self): x = self.x for x in y: pass with a as y: pass while self.foo(): self.bah() a[self.x] = 1 [out] MypyFile:1( ClassDef:2( C FuncDef:3( f Args( Var(self)) Block:4()))) [case testDoNotStripModuleTopLevelOrClassBody] # mypy: ignore-errors=True f() class C: x = 5 [out] MypyFile:1( ExpressionStmt:2( CallExpr:2( NameExpr(f) Args())) ClassDef:3( C AssignmentStmt:4( NameExpr(x) IntExpr(5)))) [case testDoNotStripMethodThatAssignsToAttribute] # mypy: ignore-errors=True class C: def m1(self): self.x = 0 def m2(self): a, self.y = 0 [out] MypyFile:1( ClassDef:2( C FuncDef:3( m1 Args( Var(self)) Block:4( AssignmentStmt:4( MemberExpr:4( NameExpr(self) x) IntExpr(0)))) FuncDef:5( m2 Args( Var(self)) Block:6( AssignmentStmt:6( TupleExpr:6( NameExpr(a) MemberExpr:6( NameExpr(self) y)) IntExpr(0)))))) [case testDoNotStripMethodThatAssignsToAttributeWithinStatement] # mypy: ignore-errors=True class C: def m1(self): for x in y: self.x = 0 def m2(self): with x: self.y = 0 def m3(self): if x: self.y = 0 else: x = 4 [out] MypyFile:1( ClassDef:2( C FuncDef:3( m1 Args( Var(self)) Block:4( ForStmt:4( NameExpr(x) NameExpr(y) Block:5( AssignmentStmt:5( MemberExpr:5( NameExpr(self) x) IntExpr(0)))))) FuncDef:6( m2 Args( Var(self)) Block:7( WithStmt:7( Expr( NameExpr(x)) Block:8( AssignmentStmt:8( MemberExpr:8( NameExpr(self) y) IntExpr(0)))))) FuncDef:9( m3 Args( Var(self)) Block:10( IfStmt:10( If( NameExpr(x)) Then( AssignmentStmt:11( MemberExpr:11( NameExpr(self) y) IntExpr(0))) Else( AssignmentStmt:13( NameExpr(x) IntExpr(4)))))))) [case testDoNotStripMethodThatDefinesAttributeWithoutAssignment] # mypy: ignore-errors=True class C: def m1(self): with y as self.x: pass def m2(self): for self.y in x: pass [out] MypyFile:1( ClassDef:2( C FuncDef:3( m1 Args( Var(self)) Block:4( WithStmt:4( Expr( NameExpr(y)) Target( MemberExpr:4( NameExpr(self) x)) Block:5( PassStmt:5())))) FuncDef:6( m2 Args( Var(self)) Block:7( ForStmt:7( MemberExpr:7( NameExpr(self) y) NameExpr(x) Block:8( PassStmt:8())))))) [case testStripDecoratedFunctionOrMethod] # mypy: ignore-errors=True @deco def f(): x = 0 class C: @deco def m1(self): x = 0 @deco def m2(self): self.x = 0 [out] MypyFile:1( Decorator:2( Var(f) NameExpr(deco) FuncDef:3( f Block:4())) ClassDef:6( C Decorator:7( Var(m1) NameExpr(deco) FuncDef:8( m1 Args( Var(self)) Block:9())) Decorator:11( Var(m2) NameExpr(deco) FuncDef:12( m2 Args( Var(self)) Block:13( AssignmentStmt:13( MemberExpr:13( NameExpr(self) x) IntExpr(0))))))) [case testStripOverloadedMethod] # mypy: ignore-errors=True class C: @overload def m1(self, x: int) -> None: ... @overload def m1(self, x: str) -> None: ... def m1(self, x): x = 0 @overload def m2(self, x: int) -> None: ... @overload def m2(self, x: str) -> None: ... def m2(self, x): self.x = 0 [out] MypyFile:1( ClassDef:2( C OverloadedFuncDef:3( Decorator:3( Var(m1) NameExpr(overload) FuncDef:4( m1 Args( Var(self) Var(x)) def (self: Any, x: int?) -> None? Block:4( ExpressionStmt:4( Ellipsis)))) Decorator:5( Var(m1) NameExpr(overload) FuncDef:6( m1 Args( Var(self) Var(x)) def (self: Any, x: str?) -> None? Block:6( ExpressionStmt:6( Ellipsis)))) FuncDef:7( m1 Args( Var(self) Var(x)) Block:8())) OverloadedFuncDef:10( Decorator:10( Var(m2) NameExpr(overload) FuncDef:11( m2 Args( Var(self) Var(x)) def (self: Any, x: int?) -> None? Block:11( ExpressionStmt:11( Ellipsis)))) Decorator:12( Var(m2) NameExpr(overload) FuncDef:13( m2 Args( Var(self) Var(x)) def (self: Any, x: str?) -> None? Block:13( ExpressionStmt:13( Ellipsis)))) FuncDef:14( m2 Args( Var(self) Var(x)) Block:15( AssignmentStmt:15( MemberExpr:15( NameExpr(self) x) IntExpr(0))))))) [case testStripMethodInNestedClass] # mypy: ignore-errors=True class C: class D: def m1(self): self.x = 1 def m2(self): return self.x [out] MypyFile:1( ClassDef:2( C ClassDef:3( D FuncDef:4( m1 Args( Var(self)) Block:5( AssignmentStmt:5( MemberExpr:5( NameExpr(self) x) IntExpr(1)))) FuncDef:6( m2 Args( Var(self)) Block:7())))) [case testDebugFString] f"test {x=}" [out] MypyFile:1( ExpressionStmt:1( CallExpr:1( MemberExpr:1( StrExpr() join) Args( ListExpr:1( StrExpr(test x=) CallExpr:1( MemberExpr:1( StrExpr({!r:{}}) format) Args( NameExpr(x) StrExpr())))))))