|
-- Test cases for parser -- Python 2 syntax. |
|
-- |
|
-- See parse.test for a description of this file format. |
|
|
|
[case testEmptyFile] |
|
[out] |
|
MypyFile:1() |
|
|
|
[case testStringLiterals] |
|
'bar' |
|
u'foo' |
|
ur'foo' |
|
u'''bar''' |
|
b'foo' |
|
[out] |
|
MypyFile:1( |
|
ExpressionStmt:1( |
|
StrExpr(bar)) |
|
ExpressionStmt:2( |
|
UnicodeExpr(foo)) |
|
ExpressionStmt:3( |
|
UnicodeExpr(foo)) |
|
ExpressionStmt:4( |
|
UnicodeExpr(bar)) |
|
ExpressionStmt:5( |
|
StrExpr(foo))) |
|
|
|
[case testSimplePrint] |
|
print 1 |
|
print 2, 3 |
|
print (4, 5) |
|
[out] |
|
MypyFile:1( |
|
PrintStmt:1( |
|
IntExpr(1) |
|
Newline) |
|
PrintStmt:2( |
|
IntExpr(2) |
|
IntExpr(3) |
|
Newline) |
|
PrintStmt:3( |
|
TupleExpr:3( |
|
IntExpr(4) |
|
IntExpr(5)) |
|
Newline)) |
|
|
|
[case testPrintWithNoArgs] |
|
print |
|
[out] |
|
MypyFile:1( |
|
PrintStmt:1( |
|
Newline)) |
|
|
|
[case testPrintWithTarget] |
|
print >>foo |
|
[out] |
|
MypyFile:1( |
|
PrintStmt:1( |
|
Target( |
|
NameExpr(foo)) |
|
Newline)) |
|
|
|
[case testPrintWithTargetAndArgs] |
|
print >>foo, x |
|
[out] |
|
MypyFile:1( |
|
PrintStmt:1( |
|
NameExpr(x) |
|
Target( |
|
NameExpr(foo)) |
|
Newline)) |
|
|
|
[case testPrintWithTargetAndArgsAndTrailingComma] |
|
print >>foo, x, y, |
|
[out] |
|
MypyFile:1( |
|
PrintStmt:1( |
|
NameExpr(x) |
|
NameExpr(y) |
|
Target( |
|
NameExpr(foo)))) |
|
|
|
[case testSimpleWithTrailingComma] |
|
print 1, |
|
print 2, 3, |
|
print (4, 5), |
|
[out] |
|
MypyFile:1( |
|
PrintStmt:1( |
|
IntExpr(1)) |
|
PrintStmt:2( |
|
IntExpr(2) |
|
IntExpr(3)) |
|
PrintStmt:3( |
|
TupleExpr:3( |
|
IntExpr(4) |
|
IntExpr(5)))) |
|
|
|
[case testOctalIntLiteral] |
|
00 |
|
01 |
|
0377 |
|
[out] |
|
MypyFile:1( |
|
ExpressionStmt:1( |
|
IntExpr(0)) |
|
ExpressionStmt:2( |
|
IntExpr(1)) |
|
ExpressionStmt:3( |
|
IntExpr(255))) |
|
|
|
[case testLongLiteral-skip] |
|
# see typed_ast issue #26 |
|
0L |
|
123L |
|
012L |
|
0x123l |
|
[out] |
|
MypyFile:1( |
|
ExpressionStmt:1( |
|
IntExpr(0)) |
|
ExpressionStmt:2( |
|
IntExpr(123)) |
|
ExpressionStmt:3( |
|
IntExpr(10)) |
|
ExpressionStmt:4( |
|
IntExpr(291))) |
|
|
|
[case testTryExceptWithComma] |
|
try: |
|
x |
|
except Exception, e: |
|
y |
|
[out] |
|
MypyFile:1( |
|
TryStmt:1( |
|
Block:1( |
|
ExpressionStmt:2( |
|
NameExpr(x))) |
|
NameExpr(Exception) |
|
NameExpr(e) |
|
Block:3( |
|
ExpressionStmt:4( |
|
NameExpr(y))))) |
|
|
|
[case testTryExceptWithNestedComma] |
|
try: |
|
x |
|
except (KeyError, IndexError): |
|
y |
|
[out] |
|
MypyFile:1( |
|
TryStmt:1( |
|
Block:1( |
|
ExpressionStmt:2( |
|
NameExpr(x))) |
|
TupleExpr:3( |
|
NameExpr(KeyError) |
|
NameExpr(IndexError)) |
|
Block:3( |
|
ExpressionStmt:4( |
|
NameExpr(y))))) |
|
|
|
[case testExecStatement] |
|
exec a |
|
[out] |
|
MypyFile:1( |
|
ExecStmt:1( |
|
NameExpr(a))) |
|
|
|
[case testExecStatementWithIn] |
|
exec a in globals() |
|
[out] |
|
MypyFile:1( |
|
ExecStmt:1( |
|
NameExpr(a) |
|
CallExpr:1( |
|
NameExpr(globals) |
|
Args()))) |
|
|
|
[case testExecStatementWithInAnd2Expressions] |
|
exec a in x, y |
|
[out] |
|
MypyFile:1( |
|
ExecStmt:1( |
|
NameExpr(a) |
|
NameExpr(x) |
|
NameExpr(y))) |
|
|
|
[case testEllipsisInExpression_python2] |
|
x = ... # E: invalid syntax |
|
[out] |
|
|
|
[case testStrLiteralConcatenationWithMixedLiteralTypes] |
|
u'foo' 'bar' |
|
'bar' u'foo' |
|
[out] |
|
MypyFile:1( |
|
ExpressionStmt:1( |
|
UnicodeExpr(foobar)) |
|
ExpressionStmt:2( |
|
UnicodeExpr(barfoo))) |
|
|
|
[case testLegacyInequality] |
|
1 <> 2 |
|
[out] |
|
MypyFile:1( |
|
ExpressionStmt:1( |
|
ComparisonExpr:1( |
|
!= |
|
IntExpr(1) |
|
IntExpr(2)))) |
|
|
|
[case testListComprehensionInPython2] |
|
([ 0 for x in 1, 2 if 3 ]) |
|
[out] |
|
MypyFile:1( |
|
ExpressionStmt:1( |
|
ListComprehension:1( |
|
GeneratorExpr:1( |
|
IntExpr(0) |
|
NameExpr(x) |
|
TupleExpr:1( |
|
IntExpr(1) |
|
IntExpr(2)) |
|
IntExpr(3))))) |
|
|
|
[case testTupleArgListInPython2] |
|
def f(x, (y, z)): pass |
|
[out] |
|
MypyFile:1( |
|
FuncDef:1( |
|
f |
|
Args( |
|
Var(x) |
|
Var(__tuple_arg_2)) |
|
Block:1( |
|
AssignmentStmt:1( |
|
TupleExpr:1( |
|
NameExpr(y) |
|
NameExpr(z)) |
|
NameExpr(__tuple_arg_2)) |
|
PassStmt:1()))) |
|
|
|
[case testTupleArgListWithTwoTupleArgsInPython2] |
|
def f((x, y), (z, zz)): pass |
|
[out] |
|
MypyFile:1( |
|
FuncDef:1( |
|
f |
|
Args( |
|
Var(__tuple_arg_1) |
|
Var(__tuple_arg_2)) |
|
Block:1( |
|
AssignmentStmt:1( |
|
TupleExpr:1( |
|
NameExpr(x) |
|
NameExpr(y)) |
|
NameExpr(__tuple_arg_1)) |
|
AssignmentStmt:1( |
|
TupleExpr:1( |
|
NameExpr(z) |
|
NameExpr(zz)) |
|
NameExpr(__tuple_arg_2)) |
|
PassStmt:1()))) |
|
|
|
[case testTupleArgListWithInitializerInPython2] |
|
def f((y, z) = (1, 2)): pass |
|
[out] |
|
MypyFile:1( |
|
FuncDef:1( |
|
f |
|
Args( |
|
default( |
|
Var(__tuple_arg_1) |
|
TupleExpr:1( |
|
IntExpr(1) |
|
IntExpr(2)))) |
|
Block:1( |
|
AssignmentStmt:1( |
|
TupleExpr:1( |
|
NameExpr(y) |
|
NameExpr(z)) |
|
NameExpr(__tuple_arg_1)) |
|
PassStmt:1()))) |
|
|
|
[case testLambdaTupleArgListInPython2] |
|
lambda (x, y): z |
|
[out] |
|
MypyFile:1( |
|
ExpressionStmt:1( |
|
LambdaExpr:1( |
|
Args( |
|
Var(__tuple_arg_1)) |
|
Block:1( |
|
AssignmentStmt:1( |
|
TupleExpr:1( |
|
NameExpr(x) |
|
NameExpr(y)) |
|
NameExpr(__tuple_arg_1)) |
|
ReturnStmt:1( |
|
NameExpr(z)))))) |
|
|
|
[case testLambdaSingletonTupleArgListInPython2] |
|
lambda (x,): z |
|
[out] |
|
MypyFile:1( |
|
ExpressionStmt:1( |
|
LambdaExpr:1( |
|
Args( |
|
Var(__tuple_arg_1)) |
|
Block:1( |
|
AssignmentStmt:1( |
|
TupleExpr:1( |
|
NameExpr(x)) |
|
NameExpr(__tuple_arg_1)) |
|
ReturnStmt:1( |
|
NameExpr(z)))))) |
|
|
|
[case testLambdaNoTupleArgListInPython2] |
|
lambda (x): z |
|
[out] |
|
MypyFile:1( |
|
ExpressionStmt:1( |
|
LambdaExpr:1( |
|
Args( |
|
Var(x)) |
|
Block:1( |
|
ReturnStmt:1( |
|
NameExpr(z)))))) |
|
|
|
[case testInvalidExprInTupleArgListInPython2_1] |
|
def f(x, ()): pass |
|
[out] |
|
main:1: error: invalid syntax |
|
|
|
[case testInvalidExprInTupleArgListInPython2_2] |
|
def f(x, (y, x[1])): pass |
|
[out] |
|
main:1: error: invalid syntax |
|
|
|
[case testListLiteralAsTupleArgInPython2] |
|
def f(x, [x]): pass |
|
[out] |
|
main:1: error: invalid syntax |
|
|
|
[case testTupleArgAfterStarArgInPython2] |
|
def f(*a, (b, c)): pass |
|
[out] |
|
main:1: error: invalid syntax |
|
|
|
[case testTupleArgAfterStarStarArgInPython2] |
|
def f(*a, (b, c)): pass |
|
[out] |
|
main:1: error: invalid syntax |
|
|
|
[case testParenthesizedArgumentInPython2] |
|
def f(x, (y)): pass |
|
[out] |
|
MypyFile:1( |
|
FuncDef:1( |
|
f |
|
Args( |
|
Var(x) |
|
Var(y)) |
|
Block:1( |
|
PassStmt:1()))) |
|
|
|
[case testDuplicateNameInTupleArgList_python2] |
|
def f(a, (a, b)): |
|
pass |
|
def g((x, (x, y))): |
|
pass |
|
[out] |
|
main:1: error: Duplicate argument 'a' in function definition |
|
main:3: error: Duplicate argument 'x' in function definition |
|
|
|
[case testBackquotesInPython2] |
|
`1 + 2` |
|
[out] |
|
MypyFile:1( |
|
ExpressionStmt:1( |
|
BackquoteExpr:1( |
|
OpExpr:1( |
|
+ |
|
IntExpr(1) |
|
IntExpr(2))))) |
|
|
|
[case testBackquoteSpecialCasesInPython2] |
|
`1, 2` |
|
[out] |
|
MypyFile:1( |
|
ExpressionStmt:1( |
|
BackquoteExpr:1( |
|
TupleExpr:1( |
|
IntExpr(1) |
|
IntExpr(2))))) |
|
|
|
[case testSuperInPython2] |
|
class A: |
|
def f(self): |
|
super(A, self).x |
|
[out] |
|
MypyFile:1( |
|
ClassDef:1( |
|
A |
|
FuncDef:2( |
|
f |
|
Args( |
|
Var(self)) |
|
Block:2( |
|
ExpressionStmt:3( |
|
SuperExpr:3( |
|
x |
|
CallExpr:3( |
|
NameExpr(super) |
|
Args( |
|
NameExpr(A) |
|
NameExpr(self))))))))) |
|
|
|
[case testTypeCommentsInPython2] |
|
x = 1 # type: List[int] |
|
|
|
def f(x, y=0): |
|
# type: (List[int], str) -> None |
|
pass |
|
[out] |
|
MypyFile:1( |
|
AssignmentStmt:1( |
|
NameExpr(x) |
|
IntExpr(1) |
|
List?[int?]) |
|
FuncDef:3( |
|
f |
|
Args( |
|
Var(x) |
|
default( |
|
Var(y) |
|
IntExpr(0))) |
|
def (x: List?[int?], y: str? =) -> None? |
|
Block:3( |
|
PassStmt:5()))) |
|
|
|
[case testMultiLineTypeCommentInPython2] |
|
def f(x, # type: List[int] |
|
y, |
|
z=1, # type: str |
|
): |
|
# type: (...) -> None |
|
pass |
|
[out] |
|
MypyFile:1( |
|
FuncDef:1( |
|
f |
|
Args( |
|
Var(x) |
|
Var(y) |
|
default( |
|
Var(z) |
|
IntExpr(1))) |
|
def (x: List?[int?], y: Any, z: str? =) -> None? |
|
Block:1( |
|
PassStmt:6()))) |
|
|
|
[case testIfStmtInPython2] |
|
if x: |
|
y |
|
elif z: |
|
a |
|
else: |
|
b |
|
[out] |
|
MypyFile:1( |
|
IfStmt:1( |
|
If( |
|
NameExpr(x)) |
|
Then( |
|
ExpressionStmt:2( |
|
NameExpr(y))) |
|
Else( |
|
IfStmt:3( |
|
If( |
|
NameExpr(z)) |
|
Then( |
|
ExpressionStmt:4( |
|
NameExpr(a))) |
|
Else( |
|
ExpressionStmt:6( |
|
NameExpr(b))))))) |
|
|
|
[case testWhileStmtInPython2] |
|
while x: |
|
y |
|
else: |
|
z |
|
[out] |
|
MypyFile:1( |
|
WhileStmt:1( |
|
NameExpr(x) |
|
Block:1( |
|
ExpressionStmt:2( |
|
NameExpr(y))) |
|
Else( |
|
ExpressionStmt:4( |
|
NameExpr(z))))) |
|
|
|
[case testForStmtInPython2] |
|
for x, y in z: |
|
a |
|
else: |
|
b |
|
[out] |
|
MypyFile:1( |
|
ForStmt:1( |
|
TupleExpr:1( |
|
NameExpr(x) |
|
NameExpr(y)) |
|
NameExpr(z) |
|
Block:1( |
|
ExpressionStmt:2( |
|
NameExpr(a))) |
|
Else( |
|
ExpressionStmt:4( |
|
NameExpr(b))))) |
|
|
|
[case testWithStmtInPython2] |
|
with x as y: |
|
z |
|
[out] |
|
MypyFile:1( |
|
WithStmt:1( |
|
Expr( |
|
NameExpr(x)) |
|
Target( |
|
NameExpr(y)) |
|
Block:1( |
|
ExpressionStmt:2( |
|
NameExpr(z))))) |
|
|
|
[case testExpressionsInPython2] |
|
x[y] |
|
x + y |
|
~z |
|
x.y |
|
([x, y]) |
|
{x, y} |
|
{x: y} |
|
x < y > z |
|
[out] |
|
MypyFile:1( |
|
ExpressionStmt:1( |
|
IndexExpr:1( |
|
NameExpr(x) |
|
NameExpr(y))) |
|
ExpressionStmt:2( |
|
OpExpr:2( |
|
+ |
|
NameExpr(x) |
|
NameExpr(y))) |
|
ExpressionStmt:3( |
|
UnaryExpr:3( |
|
~ |
|
NameExpr(z))) |
|
ExpressionStmt:4( |
|
MemberExpr:4( |
|
NameExpr(x) |
|
y)) |
|
ExpressionStmt:5( |
|
ListExpr:5( |
|
NameExpr(x) |
|
NameExpr(y))) |
|
ExpressionStmt:6( |
|
SetExpr:6( |
|
NameExpr(x) |
|
NameExpr(y))) |
|
ExpressionStmt:7( |
|
DictExpr:7( |
|
NameExpr(x) |
|
NameExpr(y))) |
|
ExpressionStmt:8( |
|
ComparisonExpr:8( |
|
< |
|
> |
|
NameExpr(x) |
|
NameExpr(y) |
|
NameExpr(z)))) |
|
|
|
[case testSlicingInPython2] |
|
x[y:] |
|
x[y:z] |
|
x[::y] |
|
[out] |
|
MypyFile:1( |
|
ExpressionStmt:1( |
|
IndexExpr:1( |
|
NameExpr(x) |
|
SliceExpr:1( |
|
NameExpr(y) |
|
<empty>))) |
|
ExpressionStmt:2( |
|
IndexExpr:2( |
|
NameExpr(x) |
|
SliceExpr:2( |
|
NameExpr(y) |
|
NameExpr(z)))) |
|
ExpressionStmt:3( |
|
IndexExpr:3( |
|
NameExpr(x) |
|
SliceExpr:3( |
|
<empty> |
|
<empty> |
|
NameExpr(y))))) |
|
|
|
[case testStarArgsInPython2] |
|
def f(*x): # type: (*int) -> None |
|
pass |
|
f(x, *y) |
|
[out] |
|
MypyFile:1( |
|
FuncDef:1( |
|
f |
|
def (*x: int?) -> None? |
|
VarArg( |
|
Var(x)) |
|
Block:1( |
|
PassStmt:2())) |
|
ExpressionStmt:3( |
|
CallExpr:3( |
|
NameExpr(f) |
|
Args( |
|
NameExpr(x) |
|
NameExpr(y)) |
|
VarArg))) |
|
|
|
[case testKwArgsInPython2] |
|
def f(**x): # type: (**int) -> None |
|
pass |
|
f(x, **y) |
|
[out] |
|
MypyFile:1( |
|
FuncDef:1( |
|
f |
|
def (**x: int?) -> None? |
|
DictVarArg( |
|
Var(x)) |
|
Block:1( |
|
PassStmt:2())) |
|
ExpressionStmt:3( |
|
CallExpr:3( |
|
NameExpr(f) |
|
Args( |
|
NameExpr(x)) |
|
DictVarArg( |
|
NameExpr(y))))) |
|
|
|
[case testBoolOpInPython2] |
|
x and y or z |
|
[out] |
|
MypyFile:1( |
|
ExpressionStmt:1( |
|
OpExpr:1( |
|
or |
|
OpExpr:1( |
|
and |
|
NameExpr(x) |
|
NameExpr(y)) |
|
NameExpr(z)))) |
|
|
|
[case testImportsInPython2] |
|
from x import y, z as zz |
|
import m |
|
import n as nn |
|
from aa import * |
|
[out] |
|
MypyFile:1( |
|
ImportFrom:1(x, [y, z : zz]) |
|
Import:2(m) |
|
Import:3(n : nn) |
|
ImportAll:4(aa)) |
|
|
|
[case testTryFinallyInPython2] |
|
try: |
|
x |
|
finally: |
|
y |
|
[out] |
|
MypyFile:1( |
|
TryStmt:1( |
|
Block:1( |
|
ExpressionStmt:2( |
|
NameExpr(x))) |
|
Finally( |
|
ExpressionStmt:4( |
|
NameExpr(y))))) |
|
|
|
[case testRaiseInPython2] |
|
raise |
|
raise x |
|
[out] |
|
MypyFile:1( |
|
RaiseStmt:1() |
|
RaiseStmt:2( |
|
NameExpr(x))) |
|
|
|
[case testAssignmentInPython2] |
|
x = y |
|
x, (y, z) = aa |
|
[out] |
|
MypyFile:1( |
|
AssignmentStmt:1( |
|
NameExpr(x) |
|
NameExpr(y)) |
|
AssignmentStmt:2( |
|
TupleExpr:2( |
|
NameExpr(x) |
|
TupleExpr:2( |
|
NameExpr(y) |
|
NameExpr(z))) |
|
NameExpr(aa))) |
|
|
|
[case testAugmentedAssignmentInPython2] |
|
x += y |
|
x *= 2 |
|
[out] |
|
MypyFile:1( |
|
OperatorAssignmentStmt:1( |
|
+ |
|
NameExpr(x) |
|
NameExpr(y)) |
|
OperatorAssignmentStmt:2( |
|
* |
|
NameExpr(x) |
|
IntExpr(2))) |
|
|
|
[case testDelStatementInPython2] |
|
del x |
|
del x.y, x[y] |
|
[out] |
|
MypyFile:1( |
|
DelStmt:1( |
|
NameExpr(x)) |
|
DelStmt:2( |
|
TupleExpr:2( |
|
MemberExpr:2( |
|
NameExpr(x) |
|
y) |
|
IndexExpr:2( |
|
NameExpr(x) |
|
NameExpr(y))))) |
|
|
|
[case testClassDecoratorInPython2] |
|
@dec() |
|
class C: |
|
pass |
|
[out] |
|
MypyFile:1( |
|
ClassDef:2( |
|
C |
|
Decorators( |
|
CallExpr:1( |
|
NameExpr(dec) |
|
Args())) |
|
PassStmt:3())) |
|
|
|
[case testFunctionDecaratorInPython2] |
|
@dec() |
|
def f(): |
|
pass |
|
[out] |
|
MypyFile:1( |
|
Decorator:1( |
|
Var(f) |
|
CallExpr:1( |
|
NameExpr(dec) |
|
Args()) |
|
FuncDef:2( |
|
f |
|
Block:2( |
|
PassStmt:3())))) |
|
|
|
[case testOverloadedFunctionInPython2] |
|
@overload |
|
def g(): |
|
pass |
|
@overload |
|
def g(): |
|
pass |
|
def g(): |
|
pass |
|
[out] |
|
MypyFile:1( |
|
OverloadedFuncDef:1( |
|
Decorator:1( |
|
Var(g) |
|
NameExpr(overload) |
|
FuncDef:2( |
|
g |
|
Block:2( |
|
PassStmt:3()))) |
|
Decorator:4( |
|
Var(g) |
|
NameExpr(overload) |
|
FuncDef:5( |
|
g |
|
Block:5( |
|
PassStmt:6()))) |
|
FuncDef:7( |
|
g |
|
Block:7( |
|
PassStmt:8())))) |