[case testReturn]
def f(x): return x
def g(): return
[out]
MypyFile:1(
  FuncDef:1(
    f
    Args(
      Var(x))
    Block:1(
      ReturnStmt:1(
        NameExpr(x [l]))))
  FuncDef:2(
    g
    Block:2(
      ReturnStmt:2())))

[case testRaise]
raise object()
[out]
MypyFile:1(
  RaiseStmt:1(
    CallExpr:1(
      NameExpr(object [builtins.object])
      Args())))

[case testYield]
def f(): yield f
[out]
MypyFile:1(
  FuncDef:1(
    f
    Generator
    Block:1(
      ExpressionStmt:1(
        YieldExpr:1(
          NameExpr(f [__main__.f]))))))

[case testAssert]
assert object
[out]
MypyFile:1(
  AssertStmt:1(
    NameExpr(object [builtins.object])))

[case testOperatorAssignment]
x = y = 1
x += y
y |= x
[out]
MypyFile:1(
  AssignmentStmt:1(
    Lvalues(
      NameExpr(x* [__main__.x])
      NameExpr(y* [__main__.y]))
    IntExpr(1))
  OperatorAssignmentStmt:2(
    +
    NameExpr(x [__main__.x])
    NameExpr(y [__main__.y]))
  OperatorAssignmentStmt:3(
    |
    NameExpr(y [__main__.y])
    NameExpr(x [__main__.x])))

[case testWhile]
x = y = 1
while x:
  y
[out]
MypyFile:1(
  AssignmentStmt:1(
    Lvalues(
      NameExpr(x* [__main__.x])
      NameExpr(y* [__main__.y]))
    IntExpr(1))
  WhileStmt:2(
    NameExpr(x [__main__.x])
    Block:2(
      ExpressionStmt:3(
        NameExpr(y [__main__.y])))))

[case testFor]
for x in object:
  x
[out]
MypyFile:1(
  ForStmt:1(
    NameExpr(x* [__main__.x])
    NameExpr(object [builtins.object])
    Block:1(
      ExpressionStmt:2(
        NameExpr(x [__main__.x])))))

[case testForInFunction]
def f():
  for x in f:
    x
[out]
MypyFile:1(
  FuncDef:1(
    f
    Block:1(
      ForStmt:2(
        NameExpr(x* [l])
        NameExpr(f [__main__.f])
        Block:2(
          ExpressionStmt:3(
            NameExpr(x [l])))))))

[case testMultipleForIndexVars]
for x, y in []:
  x, y
[out]
MypyFile:1(
  ForStmt:1(
    TupleExpr:1(
      NameExpr(x* [__main__.x])
      NameExpr(y* [__main__.y]))
    ListExpr:1()
    Block:1(
      ExpressionStmt:2(
        TupleExpr:2(
          NameExpr(x [__main__.x])
          NameExpr(y [__main__.y]))))))

[case testForIndexVarScope]
for x in []:
  pass
x
[out]
MypyFile:1(
  ForStmt:1(
    NameExpr(x* [__main__.x])
    ListExpr:1()
    Block:1(
      PassStmt:2()))
  ExpressionStmt:3(
    NameExpr(x [__main__.x])))

[case testForIndexVarScope2]
def f():
  for x in []:
    pass
  x
[out]
MypyFile:1(
  FuncDef:1(
    f
    Block:1(
      ForStmt:2(
        NameExpr(x* [l])
        ListExpr:2()
        Block:2(
          PassStmt:3()))
      ExpressionStmt:4(
        NameExpr(x [l])))))

[case testReusingForLoopIndexVariable]
for x in None:
    pass
for x in None:
    pass
[out]
MypyFile:1(
  ForStmt:1(
    NameExpr(x* [__main__.x])
    NameExpr(None [builtins.None])
    Block:1(
      PassStmt:2()))
  ForStmt:3(
    NameExpr(x [__main__.x])
    NameExpr(None [builtins.None])
    Block:3(
      PassStmt:4())))

[case testReusingForLoopIndexVariable2]
def f():
    for x in None:
        pass
    for x in None:
        pass
[out]
MypyFile:1(
  FuncDef:1(
    f
    Block:1(
      ForStmt:2(
        NameExpr(x* [l])
        NameExpr(None [builtins.None])
        Block:2(
          PassStmt:3()))
      ForStmt:4(
        NameExpr(x [l])
        NameExpr(None [builtins.None])
        Block:4(
          PassStmt:5())))))

[case testLoopWithElse]
for x in []:
  pass
else:
  x
while 1:
  pass
else:
  x
[out]
MypyFile:1(
  ForStmt:1(
    NameExpr(x* [__main__.x])
    ListExpr:1()
    Block:1(
      PassStmt:2())
    Else(
      ExpressionStmt:4(
        NameExpr(x [__main__.x]))))
  WhileStmt:5(
    IntExpr(1)
    Block:5(
      PassStmt:6())
    Else(
      ExpressionStmt:8(
        NameExpr(x [__main__.x])))))

[case testBreak]
while 1:
  break
for x in []:
  break
[out]
MypyFile:1(
  WhileStmt:1(
    IntExpr(1)
    Block:1(
      BreakStmt:2()))
  ForStmt:3(
    NameExpr(x* [__main__.x])
    ListExpr:3()
    Block:3(
      BreakStmt:4())))

[case testContinue]
while 1:
  continue
for x in []:
  continue
[out]
MypyFile:1(
  WhileStmt:1(
    IntExpr(1)
    Block:1(
      ContinueStmt:2()))
  ForStmt:3(
    NameExpr(x* [__main__.x])
    ListExpr:3()
    Block:3(
      ContinueStmt:4())))

[case testIf]
x = 1
if x:
  x
elif x:
  x
elif x:
  x
else:
  x
[out]
MypyFile:1(
  AssignmentStmt:1(
    NameExpr(x* [__main__.x])
    IntExpr(1))
  IfStmt:2(
    If(
      NameExpr(x [__main__.x]))
    Then(
      ExpressionStmt:3(
        NameExpr(x [__main__.x])))
    Else(
      IfStmt:4(
        If(
          NameExpr(x [__main__.x]))
        Then(
          ExpressionStmt:5(
            NameExpr(x [__main__.x])))
        Else(
          IfStmt:6(
            If(
              NameExpr(x [__main__.x]))
            Then(
              ExpressionStmt:7(
                NameExpr(x [__main__.x])))
            Else(
              ExpressionStmt:9(
                NameExpr(x [__main__.x])))))))))

[case testSimpleIf]
if object:
  object
[out]
MypyFile:1(
  IfStmt:1(
    If(
      NameExpr(object [builtins.object]))
    Then(
      ExpressionStmt:2(
        NameExpr(object [builtins.object])))))

[case testLvalues]
x = y = 1
x = 1
x.m = 1
x[y] = 1
x, y = 1
[x, y] = 1
(x, y) = 1
[out]
MypyFile:1(
  AssignmentStmt:1(
    Lvalues(
      NameExpr(x* [__main__.x])
      NameExpr(y* [__main__.y]))
    IntExpr(1))
  AssignmentStmt:2(
    NameExpr(x [__main__.x])
    IntExpr(1))
  AssignmentStmt:3(
    MemberExpr:3(
      NameExpr(x [__main__.x])
      m)
    IntExpr(1))
  AssignmentStmt:4(
    IndexExpr:4(
      NameExpr(x [__main__.x])
      NameExpr(y [__main__.y]))
    IntExpr(1))
  AssignmentStmt:5(
    TupleExpr:5(
      NameExpr(x [__main__.x])
      NameExpr(y [__main__.y]))
    IntExpr(1))
  AssignmentStmt:6(
    TupleExpr:6(
      NameExpr(x [__main__.x])
      NameExpr(y [__main__.y]))
    IntExpr(1))
  AssignmentStmt:7(
    TupleExpr:7(
      NameExpr(x [__main__.x])
      NameExpr(y [__main__.y]))
    IntExpr(1)))

[case testStarLvalues]
*x, y = 1
*x, (y, *z) = 1
*(x, q), r = 1
[out]
MypyFile:1(
  AssignmentStmt:1(
    TupleExpr:1(
      StarExpr:1(
        NameExpr(x* [__main__.x]))
      NameExpr(y* [__main__.y]))
    IntExpr(1))
  AssignmentStmt:2(
    TupleExpr:2(
      StarExpr:2(
        NameExpr(x [__main__.x]))
      TupleExpr:2(
        NameExpr(y [__main__.y])
        StarExpr:2(
          NameExpr(z* [__main__.z]))))
    IntExpr(1))
  AssignmentStmt:3(
    TupleExpr:3(
      StarExpr:3(
        TupleExpr:3(
          NameExpr(x [__main__.x])
          NameExpr(q* [__main__.q])))
      NameExpr(r* [__main__.r]))
    IntExpr(1)))

[case testMultipleDefinition]
x, y = 1
x, y = 2
[out]
MypyFile:1(
  AssignmentStmt:1(
    TupleExpr:1(
      NameExpr(x* [__main__.x])
      NameExpr(y* [__main__.y]))
    IntExpr(1))
  AssignmentStmt:2(
    TupleExpr:2(
      NameExpr(x [__main__.x])
      NameExpr(y [__main__.y]))
    IntExpr(2)))

[case testComplexDefinitions]
(x) = 1
([y]) = 2
[out]
MypyFile:1(
  AssignmentStmt:1(
    NameExpr(x* [__main__.x])
    IntExpr(1))
  AssignmentStmt:2(
    TupleExpr:2(
      NameExpr(y* [__main__.y]))
    IntExpr(2)))

[case testLocalComplexDefinition]
def f():
  (x) = 1
  x
[out]
MypyFile:1(
  FuncDef:1(
    f
    Block:1(
      AssignmentStmt:2(
        NameExpr(x* [l])
        IntExpr(1))
      ExpressionStmt:3(
        NameExpr(x [l])))))

[case testMultipleDefOnlySomeNew]
x = 1
y, x = 1
[out]
MypyFile:1(
  AssignmentStmt:1(
    NameExpr(x* [__main__.x])
    IntExpr(1))
  AssignmentStmt:2(
    TupleExpr:2(
      NameExpr(y* [__main__.y])
      NameExpr(x [__main__.x]))
    IntExpr(1)))

[case testMultipleDefOnlySomeNewNestedTuples]
x = 1
y, (x, z) = 1
[out]
MypyFile:1(
  AssignmentStmt:1(
    NameExpr(x* [__main__.x])
    IntExpr(1))
  AssignmentStmt:2(
    TupleExpr:2(
      NameExpr(y* [__main__.y])
      TupleExpr:2(
        NameExpr(x [__main__.x])
        NameExpr(z* [__main__.z])))
    IntExpr(1)))

[case testMultipleDefOnlySomeNewNestedLists]
x = 1
y, [x, z] = 1
[p, [x, r]] = 1
[out]
MypyFile:1(
  AssignmentStmt:1(
    NameExpr(x* [__main__.x])
    IntExpr(1))
  AssignmentStmt:2(
    TupleExpr:2(
      NameExpr(y* [__main__.y])
      TupleExpr:2(
        NameExpr(x [__main__.x])
        NameExpr(z* [__main__.z])))
    IntExpr(1))
  AssignmentStmt:3(
    TupleExpr:3(
      NameExpr(p* [__main__.p])
      TupleExpr:3(
        NameExpr(x [__main__.x])
        NameExpr(r* [__main__.r])))
    IntExpr(1)))

[case testIndexedDel]
x = y = 1
del x[y]
[out]
MypyFile:1(
  AssignmentStmt:1(
    Lvalues(
      NameExpr(x* [__main__.x])
      NameExpr(y* [__main__.y]))
    IntExpr(1))
  DelStmt:2(
    IndexExpr:2(
      NameExpr(x [__main__.x])
      NameExpr(y [__main__.y]))))

[case testDelGlobalName]
x = 1
del x
[out]
MypyFile:1(
  AssignmentStmt:1(
    NameExpr(x* [__main__.x])
    IntExpr(1))
  DelStmt:2(
    NameExpr(x [__main__.x])))

[case testDelLocalName]
def f(x):
    del x
[out]
MypyFile:1(
  FuncDef:1(
    f
    Args(
      Var(x))
    Block:1(
      DelStmt:2(
        NameExpr(x [l])))))

[case testDelMultipleThings]
def f(x, y):
    del x, y[0]
[out]
MypyFile:1(
  FuncDef:1(
    f
    Args(
      Var(x)
      Var(y))
    Block:1(
      DelStmt:2(
        TupleExpr:2(
          NameExpr(x [l])
          IndexExpr:2(
            NameExpr(y [l])
            IntExpr(0)))))))

[case testDelMultipleThingsInvalid]
def f(x, y) -> None:
    del x, y + 1
[out]
main:2: error: can't delete operator

[case testTry]
class c: pass
try:
  c
except object:
  c
except c as e:
  e
except:
  c
finally:
  c
[out]
MypyFile:1(
  ClassDef:1(
    c
    PassStmt:1())
  TryStmt:2(
    Block:2(
      ExpressionStmt:3(
        NameExpr(c [__main__.c])))
    NameExpr(object [builtins.object])
    Block:4(
      ExpressionStmt:5(
        NameExpr(c [__main__.c])))
    NameExpr(c [__main__.c])
    NameExpr(e* [__main__.e])
    Block:6(
      ExpressionStmt:7(
        NameExpr(e [__main__.e])))
    Block:8(
      ExpressionStmt:9(
        NameExpr(c [__main__.c])))
    Finally(
      ExpressionStmt:11(
        NameExpr(c [__main__.c])))))

[case testTryElse]
try:
  pass
except:
  pass
else:
  object
[out]
MypyFile:1(
  TryStmt:1(
    Block:1(
      PassStmt:2())
    Block:3(
      PassStmt:4())
    Else(
      ExpressionStmt:6(
        NameExpr(object [builtins.object])))))

[case testTryWithOnlyFinally]
try:
  pass
finally:
  pass
[out]
MypyFile:1(
  TryStmt:1(
    Block:1(
      PassStmt:2())
    Finally(
      PassStmt:4())))

[case testExceptWithMultipleTypes]
class c: pass
try:
  pass
except (c, object) as e:
  e
[out]
MypyFile:1(
  ClassDef:1(
    c
    PassStmt:1())
  TryStmt:2(
    Block:2(
      PassStmt:3())
    TupleExpr:4(
      NameExpr(c [__main__.c])
      NameExpr(object [builtins.object]))
    NameExpr(e* [__main__.e])
    Block:4(
      ExpressionStmt:5(
        NameExpr(e [__main__.e])))))

[case testRaiseWithoutExpr]
raise
[out]
MypyFile:1(
  RaiseStmt:1())

[case testWith]
with object:
  object
[out]
MypyFile:1(
  WithStmt:1(
    Expr(
      NameExpr(object [builtins.object]))
    Block:1(
      ExpressionStmt:2(
        NameExpr(object [builtins.object])))))

[case testWithAndVariable]
with object as x:
  x
[out]
MypyFile:1(
  WithStmt:1(
    Expr(
      NameExpr(object [builtins.object]))
    Target(
      NameExpr(x* [__main__.x]))
    Block:1(
      ExpressionStmt:2(
        NameExpr(x [__main__.x])))))

[case testWithInFunction]
def f():
  with f as x:
    x
[out]
MypyFile:1(
  FuncDef:1(
    f
    Block:1(
      WithStmt:2(
        Expr(
          NameExpr(f [__main__.f]))
        Target(
          NameExpr(x* [l]))
        Block:2(
          ExpressionStmt:3(
            NameExpr(x [l])))))))

[case testComplexWith]
with object, object:
  pass
with object as a, object as b:
  pass
[out]
MypyFile:1(
  WithStmt:1(
    Expr(
      NameExpr(object [builtins.object]))
    Expr(
      NameExpr(object [builtins.object]))
    Block:1(
      PassStmt:2()))
  WithStmt:3(
    Expr(
      NameExpr(object [builtins.object]))
    Target(
      NameExpr(a* [__main__.a]))
    Expr(
      NameExpr(object [builtins.object]))
    Target(
      NameExpr(b* [__main__.b]))
    Block:3(
      PassStmt:4())))

[case testVariableInBlock]
while object:
  x = None
  x = x
[out]
MypyFile:1(
  WhileStmt:1(
    NameExpr(object [builtins.object])
    Block:1(
      AssignmentStmt:2(
        NameExpr(x* [__main__.x])
        NameExpr(None [builtins.None]))
      AssignmentStmt:3(
        NameExpr(x [__main__.x])
        NameExpr(x [__main__.x])))))

[case testVariableInExceptHandler]
try:
  pass
except object as o:
  x = None
  o = x
[out]
MypyFile:1(
  TryStmt:1(
    Block:1(
      PassStmt:2())
    NameExpr(object [builtins.object])
    NameExpr(o* [__main__.o])
    Block:3(
      AssignmentStmt:4(
        NameExpr(x* [__main__.x])
        NameExpr(None [builtins.None]))
      AssignmentStmt:5(
        NameExpr(o [__main__.o])
        NameExpr(x [__main__.x])))))

[case testCallInExceptHandler]
try:
  pass
except object as o:
  o = object()
[out]
MypyFile:1(
  TryStmt:1(
    Block:1(
      PassStmt:2())
    NameExpr(object [builtins.object])
    NameExpr(o* [__main__.o])
    Block:3(
      AssignmentStmt:4(
        NameExpr(o [__main__.o])
        CallExpr:4(
          NameExpr(object [builtins.object])
          Args())))))

[case testTryExceptWithMultipleHandlers]
try:
    pass
except BaseException as e:
    pass
except Err as f:
    f = BaseException() # Fail
    f = Err()
class Err(BaseException): pass
[builtins fixtures/exception.pyi]
[out]
MypyFile:1(
  TryStmt:1(
    Block:1(
      PassStmt:2())
    NameExpr(BaseException [builtins.BaseException])
    NameExpr(e* [__main__.e])
    Block:3(
      PassStmt:4())
    NameExpr(Err [__main__.Err])
    NameExpr(f* [__main__.f])
    Block:5(
      AssignmentStmt:6(
        NameExpr(f [__main__.f])
        CallExpr:6(
          NameExpr(BaseException [builtins.BaseException])
          Args()))
      AssignmentStmt:7(
        NameExpr(f [__main__.f])
        CallExpr:7(
          NameExpr(Err [__main__.Err])
          Args()))))
  ClassDef:8(
    Err
    BaseType(
      builtins.BaseException)
    PassStmt:8()))

[case testMultipleAssignmentWithPartialNewDef]
o = None
x, o = o, o
[out]
MypyFile:1(
  AssignmentStmt:1(
    NameExpr(o* [__main__.o])
    NameExpr(None [builtins.None]))
  AssignmentStmt:2(
    TupleExpr:2(
      NameExpr(x* [__main__.x])
      NameExpr(o [__main__.o]))
    TupleExpr:2(
      NameExpr(o [__main__.o])
      NameExpr(o [__main__.o]))))

[case testFunctionDecorator]
def decorate(f): pass
@decorate
def g():
    g()
[out]
MypyFile:1(
  FuncDef:1(
    decorate
    Args(
      Var(f))
    Block:1(
      PassStmt:1()))
  Decorator:2(
    Var(g)
    NameExpr(decorate [__main__.decorate])
    FuncDef:3(
      g
      Block:3(
        ExpressionStmt:4(
          CallExpr:4(
            NameExpr(g [__main__.g])
            Args()))))))

[case testTryWithinFunction]
def f() -> None:
    try:
        pass
    except object as o:
        pass
[out]
MypyFile:1(
  FuncDef:1(
    f
    def ()
    Block:1(
      TryStmt:2(
        Block:2(
          PassStmt:3())
        NameExpr(object [builtins.object])
        NameExpr(o* [l])
        Block:4(
          PassStmt:5())))))

[case testReuseExceptionVariable]
def f() -> None:
    try:
        pass
    except object as o:
        pass
    except object as o:
        pass
[out]
MypyFile:1(
  FuncDef:1(
    f
    def ()
    Block:1(
      TryStmt:2(
        Block:2(
          PassStmt:3())
        NameExpr(object [builtins.object])
        NameExpr(o* [l])
        Block:4(
          PassStmt:5())
        NameExpr(object [builtins.object])
        NameExpr(o [l])
        Block:6(
          PassStmt:7())))))

[case testWithMultiple]
def f(a):
    pass
def main():
    with f(0) as a, f(a) as b:
        x = a, b
[out]
MypyFile:1(
  FuncDef:1(
    f
    Args(
      Var(a))
    Block:1(
      PassStmt:2()))
  FuncDef:3(
    main
    Block:3(
      WithStmt:4(
        Expr(
          CallExpr:4(
            NameExpr(f [__main__.f])
            Args(
              IntExpr(0))))
        Target(
          NameExpr(a* [l]))
        Expr(
          CallExpr:4(
            NameExpr(f [__main__.f])
            Args(
              NameExpr(a [l]))))
        Target(
          NameExpr(b* [l]))
        Block:4(
          AssignmentStmt:5(
            NameExpr(x* [l])
            TupleExpr:5(
              NameExpr(a [l])
              NameExpr(b [l]))))))))
