diff --git a/mypy/fastparse.py b/mypy/fastparse.py index 91f55ca99acf8..fa2d0bae4c63e 100644 --- a/mypy/fastparse.py +++ b/mypy/fastparse.py @@ -57,8 +57,10 @@ def parse(source: Union[str, bytes], fnam: str = None, errors: Errors = None, The pyversion (major, minor) argument determines the Python syntax variant. """ + raise_on_error = False if errors is None: errors = Errors() + raise_on_error = True errors.set_file('' if fnam is None else fnam) is_stub_file = bool(fnam) and fnam.endswith('.pyi') try: @@ -72,14 +74,14 @@ def parse(source: Union[str, bytes], fnam: str = None, errors: Errors = None, ).visit(ast) tree.path = fnam tree.is_stub = is_stub_file - return tree except SyntaxError as e: - if errors: - errors.report(e.lineno, e.offset, e.msg) - else: - raise + errors.report(e.lineno, e.offset, e.msg) + tree = MypyFile([], [], False, set()) + + if raise_on_error and errors.is_errors(): + errors.raise_error() - return MypyFile([], [], False, set()) + return tree def parse_type_comment(type_comment: str, line: int, errors: Errors) -> Optional[Type]: diff --git a/mypy/fastparse2.py b/mypy/fastparse2.py index b619d5c3fe2aa..772e9ae941071 100644 --- a/mypy/fastparse2.py +++ b/mypy/fastparse2.py @@ -74,8 +74,10 @@ def parse(source: Union[str, bytes], fnam: str = None, errors: Errors = None, The pyversion (major, minor) argument determines the Python syntax variant. """ + raise_on_error = False if errors is None: errors = Errors() + raise_on_error = True errors.set_file('' if fnam is None else fnam) is_stub_file = bool(fnam) and fnam.endswith('.pyi') try: @@ -89,14 +91,14 @@ def parse(source: Union[str, bytes], fnam: str = None, errors: Errors = None, assert isinstance(tree, MypyFile) tree.path = fnam tree.is_stub = is_stub_file - return tree except SyntaxError as e: - if errors: - errors.report(e.lineno, e.offset, e.msg) - else: - raise + errors.report(e.lineno, e.offset, e.msg) + tree = MypyFile([], [], False, set()) + + if raise_on_error and errors.is_errors(): + errors.raise_error() - return MypyFile([], [], False, set()) + return tree def with_line(f: Callable[['ASTConverter', T], U]) -> Callable[['ASTConverter', T], U]: diff --git a/mypy/main.py b/mypy/main.py index ea203f0f1a446..a38d3bd3c1383 100644 --- a/mypy/main.py +++ b/mypy/main.py @@ -225,8 +225,8 @@ def add_invertible_flag(flag: str, add_invertible_flag('--show-error-context', default=True, dest='hide_error_context', help='Precede errors with "note:" messages explaining context') - add_invertible_flag('--fast-parser', default=False, - help="enable fast parser (recommended)") + add_invertible_flag('--no-fast-parser', default=True, dest='fast_parser', + help="disable the fast parser (not recommended)") parser.add_argument('-i', '--incremental', action='store_true', help="enable experimental module cache") parser.add_argument('--cache-dir', action='store', metavar='DIR', diff --git a/mypy/options.py b/mypy/options.py index 0d53c3dbc9cdf..77f9713e3f52b 100644 --- a/mypy/options.py +++ b/mypy/options.py @@ -114,7 +114,7 @@ def __init__(self) -> None: self.use_builtins_fixtures = False # -- experimental options -- - self.fast_parser = False + self.fast_parser = True self.incremental = False self.cache_dir = defaults.CACHE_DIR self.debug_cache = False diff --git a/test-data/unit/parse-errors.test b/test-data/unit/parse-errors.test index f2251a9e6e2a6..22a3c5c50b006 100644 --- a/test-data/unit/parse-errors.test +++ b/test-data/unit/parse-errors.test @@ -12,245 +12,217 @@ def f() pass [out] -file:1: error: Parse error before end of line -file:2: error: Inconsistent indentation +file:1: error: invalid syntax [case testMissingIndent] if x: 1 [out] -file:2: error: Expected an indented block +file:2: error: invalid syntax [case testUnexpectedIndent] 1 2 [out] -file:2: error: Inconsistent indentation +file:2: error: unexpected indent [case testInconsistentIndent] if x: 1 1 [out] -file:3: error: Inconsistent indentation +file:3: error: unexpected indent -[case testInconsistentIndent] +[case testInconsistentIndent2] if x: 1 1 [out] -file:3: error: Inconsistent indentation +file:3: error: unindent does not match any outer indentation level [case testInvalidBinaryOp] 1> a* a+1* [out] -file:1: error: Parse error before end of line -file:2: error: Parse error before end of line -file:3: error: Parse error before end of line +file:1: error: invalid syntax [case testDoubleStar] **a [out] -file:1: error: Parse error before ** +file:1: error: invalid syntax [case testInvalidSuperClass] class A(C[): pass [out] -file:1: error: Parse error before ) -file:2: error: Parse error before end of file +file:1: error: invalid syntax [case testMissingSuperClass] class A(: pass [out] -file:1: error: Parse error before : -file:2: error: Parse error before end of file +file:1: error: invalid syntax [case testUnexpectedEof] if 1: [out] -file:1: error: Expected an indented block +file:1: error: unexpected EOF while parsing [case testInvalidKeywordArguments1] f(x=y, z) [out] -file:1: error: Parse error before "z" +file:1: error: positional argument follows keyword argument [case testInvalidKeywordArguments2] -f(**x, y=z) -[out] -file:1: error: Parse error before "y" - -[case testInvalidKeywordArguments3] f(**x, y) [out] -file:1: error: Parse error before "y" - -[case testInvalidVarArgs] -f(*x, y) -[out] -file:1: error: Parse error before "y" +file:1: error: positional argument follows keyword argument unpacking [case testInvalidBareAsteriskAndVarArgs2] def f(*x: A, *) -> None: pass [out] -file:1: error: Parse error before ) -file:1: error: Parse error before end of line +file:1: error: invalid syntax [case testInvalidBareAsteriskAndVarArgs3] def f(*, *x: A) -> None: pass [out] -file:1: error: Parse error before * -file:1: error: Parse error before end of line +file:1: error: invalid syntax [case testInvalidBareAsteriskAndVarArgs4] def f(*, **x: A) -> None: pass [out] -file:1: error: Parse error before ** -file:1: error: Parse error before end of line +file:1: error: named arguments must follow bare * [case testInvalidBareAsterisk1] def f(*) -> None: pass [out] -file:1: error: Parse error before ) -file:1: error: Parse error before end of line +file:1: error: named arguments must follow bare * [case testInvalidBareAsterisk2] def f(x, *) -> None: pass [out] -file:1: error: Parse error before ) -file:1: error: Parse error before end of line +file:1: error: named arguments must follow bare * [case testInvalidFuncDefArgs1] def f(x = y, x): pass [out] -file:1: error: Invalid argument list +file:1: error: non-default argument follows default argument [case testInvalidFuncDefArgs3] def f(**x, y): pass [out] -file:1: error: Invalid argument list +file:1: error: invalid syntax [case testInvalidFuncDefArgs4] def f(**x, y=x): pass [out] -file:1: error: Invalid argument list +file:1: error: invalid syntax [case testInvalidStringLiteralType] def f(x: 'A[' ) -> None: pass [out] -file:2: error: Parse error before end of line -file:3: error: Parse error before end of line +file:1: error: syntax error in type comment [case testInvalidStringLiteralType2] def f(x: 'A B' ) -> None: pass [out] -file:2: error: Parse error before "B" -file:3: error: Parse error before end of line +file:1: error: syntax error in type comment [case testInvalidTypeComment] 0 x = 0 # type: A A [out] -file:2: error: Parse error before "A" +file:2: error: syntax error in type comment [case testInvalidTypeComment2] 0 x = 0 # type: A[ [out] -file:2: error: Parse error before end of line +file:2: error: syntax error in type comment [case testInvalidTypeComment3] 0 x = 0 # type: [out] -file:2: error: Empty type annotation +file:2: error: syntax error in type comment [case testInvalidTypeComment4] 0 x = 0 # type: * [out] -file:2: error: Parse error before end of line +file:2: error: syntax error in type comment [case testInvalidMultilineLiteralType] def f() -> "A\nB": pass [out] -file:1: error: Parse error before end of line - -[case testInvalidMetaclass] -class A(metaclass=1): pass -[out] -file:1: error: Parse error before numeric literal -file:1: error: Parse error before end of file +file:1: error: syntax error in type comment [case testInvalidSignatureInComment1] def f(): # type: x pass [out] -file:1: error: Parse error before "x" +file:1: error: syntax error in type comment [case testInvalidSignatureInComment2] def f(): # type: pass [out] -file:1: error: Empty type annotation +file:1: error: syntax error in type comment [case testInvalidSignatureInComment3] def f(): # type: ( pass [out] -file:1: error: Parse error before end of line +file:1: error: syntax error in type comment [case testInvalidSignatureInComment4] def f(): # type: (. pass [out] -file:1: error: Parse error before . +file:1: error: syntax error in type comment [case testInvalidSignatureInComment5] def f(): # type: (x pass [out] -file:1: error: Parse error before end of line +file:1: error: syntax error in type comment [case testInvalidSignatureInComment6] def f(): # type: (x) pass [out] -file:1: error: Parse error before end of line +file:1: error: syntax error in type comment [case testInvalidSignatureInComment7] def f(): # type: (x) - pass [out] -file:1: error: Parse error before - +file:1: error: syntax error in type comment [case testInvalidSignatureInComment8] def f(): # type: (x) -> pass [out] -file:1: error: Parse error before end of line +file:1: error: syntax error in type comment [case testInvalidSignatureInComment9] def f(): # type: (x) -> . pass [out] -file:1: error: Parse error before . +file:1: error: syntax error in type comment [case testInvalidSignatureInComment10] def f(): # type: (x) -> x x pass [out] -file:1: error: Parse error before "x" +file:1: error: syntax error in type comment [case testDuplicateSignatures1] def f() -> None: # type: () -> None @@ -278,7 +250,8 @@ def f(x, y): # type: (X) -> z [out] file:1: error: Type signature has too few arguments -[case testCommentFunctionAnnotationVarArgMispatch] +[case testCommentFunctionAnnotationVarArgMispatch-skip] +# see mypy issue #1997 def f(x): # type: (*X) -> Y pass def g(*x): # type: (X) -> Y @@ -287,203 +260,178 @@ def g(*x): # type: (X) -> Y file:1: error: Inconsistent use of '*' in function signature file:3: error: Inconsistent use of '*' in function signature -[case testCommentFunctionAnnotationVarArgMispatch2] +[case testCommentFunctionAnnotationVarArgMispatch2-skip] +# see mypy issue #1997 def f(*x, **y): # type: (**X, *Y) -> Z pass def g(*x, **y): # type: (*X, *Y) -> Z pass [out] file:1: error: Inconsistent use of '*' in function signature -file:1: error: Inconsistent use of '**' in function signature +file:3: error: syntax error in type comment file:3: error: Inconsistent use of '*' in function signature file:3: error: Inconsistent use of '**' in function signature -[case testPrintStatementInPython3] +[case testPrintStatementInPython3-skip] print 1 [out] -file:1: error: Parse error before numeric literal +file:1: error: Missing parentheses in call to 'print' [case testInvalidConditionInConditionalExpression] 1 if 2, 3 else 4 [out] -file:1: error: Parse error before , +file:1: error: invalid syntax [case testInvalidConditionInConditionalExpression2] 1 if x for y in z else 4 [out] -file:1: error: Parse error before "for" +file:1: error: invalid syntax [case testInvalidConditionInConditionalExpression2] 1 if x else for y in z [out] -file:1: error: Parse error before "for" +file:1: error: invalid syntax [case testYieldFromNotRightParameter] def f(): yield from [out] -file:2: error: Parse error before end of line +file:2: error: invalid syntax [case testYieldFromAfterReturn] def f(): return yield from h() [out] -file:2: error: Parse error before "yield" +file:2: error: invalid syntax [case testImportDotModule] import .x [out] -file:1: error: Parse error before . +file:1: error: invalid syntax [case testImportDot] import . [out] -file:1: error: Parse error before . +file:1: error: invalid syntax [case testInvalidFunctionName] def while(): pass [out] -file:1: error: Parse error before "while" -file:1: error: Parse error before end of line +file:1: error: invalid syntax [case testInvalidEllipsis1] ...0 ..._ ...a [out] -file:1: error: Parse error before numeric literal -file:2: error: Parse error before "_" -file:3: error: Parse error before "a" +file:1: error: invalid syntax [case testBlockStatementInSingleLineIf] if 1: if 2: pass [out] -file:1: error: Parse error before "if" +file:1: error: invalid syntax [case testBlockStatementInSingleLineIf2] if 1: while 2: pass [out] -file:1: error: Parse error before "while" +file:1: error: invalid syntax [case testBlockStatementInSingleLineIf3] if 1: for x in y: pass [out] -file:1: error: Parse error before "for" +file:1: error: invalid syntax [case testUnexpectedEllipsis] a = a... [out] -file:1: error: Parse error before ... - -[case testFromFutureImportStar] -from __future__ import * -x = 1 -[out] -file:1: error: Parse error before * +file:1: error: invalid syntax [case testParseErrorBeforeUnicodeLiteral] x u'y' [out] -file:1: error: Parse error before string literal +file:1: error: invalid syntax [case testParseErrorInExtendedSlicing] x[:, [out] -file:1: error: Parse error before end of line +file:1: error: unexpected EOF while parsing [case testParseErrorInExtendedSlicing2] x[:,:: [out] -file:1: error: Parse error before end of line +file:1: error: unexpected EOF while parsing [case testParseErrorInExtendedSlicing3] x[:,: [out] -file:1: error: Parse error before end of line +file:1: error: unexpected EOF while parsing [case testPython2OctalIntLiteralInPython3] 0377 [out] -file:1: error: Invalid numeric literal +file:1: error: invalid token [case testInvalidEncoding] # foo # coding: uft-8 [out] -file:2: error: Unknown encoding 'uft-8' +file:0: error: unknown encoding: uft-8 [case testInvalidEncoding2] # coding=Uft.8 [out] -file:1: error: Unknown encoding 'Uft.8' +file:0: error: unknown encoding: Uft.8 [case testInvalidEncoding3] #!/usr/bin python # vim: set fileencoding=uft8 : [out] -file:2: error: Unknown encoding 'uft8' +file:0: error: unknown encoding: uft8 [case testDoubleEncoding] # coding: uft8 # coding: utf8 # The first coding cookie should be used and fail. [out] -file:1: error: Unknown encoding 'uft8' +file:0: error: unknown encoding: uft8 [case testDoubleEncoding2] # Again the first cookie should be used and fail. # coding: uft8 # coding: utf8 [out] -file:2: error: Unknown encoding 'uft8' - -[case testDoubleEncoding3] -# If the third line were interpreted as a coding cookie, we'd get a -# different error message. -# coding: ascii -á = 1 -[out] -file:4: error: Unrecognized character - -[case testDoubleEncoding4] - - -# coding: ascii -á = 1 -[out] -file:4: error: Unrecognized character +file:0: error: unknown encoding: uft8 [case testLongLiteralInPython3] 2L 0x2L [out] -file:1: error: Invalid numeric literal -file:2: error: Invalid numeric literal +file:1: error: invalid syntax [case testPython2LegacyInequalityInPython3] 1 <> 2 [out] -file:1: error: Parse error before > +file:1: error: invalid syntax [case testLambdaInListComprehensionInPython3] ([ 0 for x in 1, 2 if 3 ]) [out] -file:1: error: Parse error before , +file:1: error: invalid syntax [case testTupleArgListInPython3] def f(x, (y, z)): pass [out] -file:1: error: Tuples in argument lists only supported in Python 2 mode +file:1: error: invalid syntax [case testBackquoteInPython3] `1 + 2` [out] -file:1: error: Unrecognized character ` +file:1: error: invalid syntax [case testSmartQuotes] foo = ‘bar’ [out] -file:1: error: Unrecognized character +file:1: error: invalid character in identifier [case testExceptCommaInPython3] try: @@ -491,6 +439,10 @@ try: except KeyError, IndexError: pass [out] -file:3: error: Parse error before , -file:3: error: Parse error before : -file:4: error: Inconsistent indentation +file:3: error: invalid syntax + +[case testLocalVarWithTypeOnNextLine] +x = 0 + # type: int +[out] +file:2: error: misplaced type annotation diff --git a/test-data/unit/parse-python2.test b/test-data/unit/parse-python2.test index 7d4931e028855..7abc157b955cd 100644 --- a/test-data/unit/parse-python2.test +++ b/test-data/unit/parse-python2.test @@ -109,7 +109,8 @@ MypyFile:1( ExpressionStmt:3( IntExpr(255))) -[case testLongLiteral] +[case testLongLiteral-skip] +# see typed_ast issue #26 0L 123L 012L @@ -187,7 +188,7 @@ MypyFile:1( NameExpr(y))) [case testEllipsisInExpression_python2] -x = ... # E: Parse error before ... +x = ... # E: invalid syntax [out] [case testStrLiteralConcatenationWithMixedLiteralTypes] @@ -333,28 +334,27 @@ MypyFile:1( [case testInvalidExprInTupleArgListInPython2_1] def f(x, ()): pass [out] -main: error: Empty tuple not valid as an argument +main:1: error: invalid syntax [case testInvalidExprInTupleArgListInPython2_2] def f(x, (y, x[1])): pass [out] -main:1: error: Invalid item in tuple argument +main:1: error: invalid syntax [case testListLiteralAsTupleArgInPython2] def f(x, [x]): pass [out] -main:1: error: Parse error before [ -main:1: error: Parse error before end of line +main:1: error: invalid syntax [case testTupleArgAfterStarArgInPython2] def f(*a, (b, c)): pass [out] -main:1: error: Invalid argument list +main:1: error: invalid syntax [case testTupleArgAfterStarStarArgInPython2] def f(*a, (b, c)): pass [out] -main:1: error: Invalid argument list +main:1: error: invalid syntax [case testParenthesizedArgumentInPython2] def f(x, (y)): pass @@ -374,8 +374,8 @@ def f(a, (a, b)): def g((x, (x, y))): pass [out] -main:1: error: Duplicate argument name "a" -main:3: error: Duplicate argument name "x" +main:1: error: duplicate argument 'a' in function definition +main:3: error: duplicate argument 'x' in function definition [case testBackquotesInPython2] `1 + 2` diff --git a/test-data/unit/parse.test b/test-data/unit/parse.test index 0dec0f397281f..4335fffaa9389 100644 --- a/test-data/unit/parse.test +++ b/test-data/unit/parse.test @@ -109,8 +109,8 @@ MypyFile:1( StrExpr(foo)) ExpressionStmt:3( StrExpr(foobar)) - ExpressionStmt:4( - StrExpr(\nfoo\u000abar)) + ExpressionStmt:5( + StrExpr(\u000afoo\u000abar)) ExpressionStmt:6( StrExpr(fo''bar)) ExpressionStmt:7( @@ -119,8 +119,8 @@ MypyFile:1( StrExpr(foo)) ExpressionStmt:9( StrExpr(foobar)) - ExpressionStmt:10( - StrExpr(\nfoo\u000abar)) + ExpressionStmt:11( + StrExpr(\u000afoo\u000abar)) ExpressionStmt:12( StrExpr(fo""bar))) @@ -130,9 +130,9 @@ r"x\n\"" [out] MypyFile:1( ExpressionStmt:1( - StrExpr(x\n')) + StrExpr(x\n\')) ExpressionStmt:2( - StrExpr(x\n"))) + StrExpr(x\n\"))) --" fix syntax highlight [case testBytes] @@ -147,7 +147,7 @@ MypyFile:1( ExpressionStmt:2( BytesExpr(foobar)) ExpressionStmt:3( - BytesExpr(x\n'))) + BytesExpr(x\\n\\'))) [case testEscapesInStrings] '\r\n\t\x2f\u123f' @@ -157,7 +157,7 @@ MypyFile:1( ExpressionStmt:1( StrExpr(\u000d\u000a\u0009/\u123f)) ExpressionStmt:2( - BytesExpr(\u000d\u000a\u0009/\\u123f))) + BytesExpr(\r\n\t/\\\u123f))) -- Note \\u in the b'...' case (\u sequence not translated) [case testEscapedQuote] @@ -176,7 +176,7 @@ MypyFile:1( ExpressionStmt:1( StrExpr(\u0000\u0001\u007fS4)) ExpressionStmt:2( - BytesExpr(\u0001\u013e))) + BytesExpr(\x01>))) [case testUnicodeLiteralInPython3] u'foo' @@ -301,19 +301,23 @@ MypyFile:1( Then( ExpressionStmt:2( IntExpr(2))) - If( - IntExpr(3)) - Then( - ExpressionStmt:4( - IntExpr(4))) - If( - IntExpr(5)) - Then( - ExpressionStmt:6( - IntExpr(6))) Else( - ExpressionStmt:8( - IntExpr(7))))) + 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: @@ -432,16 +436,6 @@ MypyFile:1( NameExpr(None) Any?)))) -[case testLocalVarWithTypeOnNextLine] -x = 0 - # type: int -[out] -MypyFile:1( - AssignmentStmt:1( - NameExpr(x) - IntExpr(0) - int?)) - [case testFunctionDefWithType] def f(y: str) -> int: return @@ -948,18 +942,6 @@ MypyFile:1( NameExpr(x) NameExpr(y)))) -[case testNotAsRightOperand] -x in not y -[out] -MypyFile:1( - ExpressionStmt:1( - ComparisonExpr:1( - in - NameExpr(x) - UnaryExpr:1( - not - NameExpr(y))))) - [case testNotIn] x not in y not x not in y @@ -988,12 +970,15 @@ MypyFile:1( NameExpr(z))))) [case testNotAsBinaryOp] -x not y # E: Parse error before "y" -x not is y # E: Parse error before is +x not y # E: invalid syntax +[out] + +[case testNotIs] +x not is y # E: invalid syntax [out] [case testBinaryNegAsBinaryOp] -1 ~ 2 # E: Parse error before ~ +1 ~ 2 # E: invalid syntax [out] [case testDictionaryExpression] @@ -1188,7 +1173,7 @@ MypyFile:1( IntExpr(3))))) [case testGeneratorExpression] -x for y in z +(x for y in z) [out] MypyFile:1( ExpressionStmt:1( @@ -1198,7 +1183,7 @@ MypyFile:1( NameExpr(z)))) [case testGeneratorExpressionNested] -x for y, (p, q) in z +(x for y, (p, q) in z) [out] MypyFile:1( ExpressionStmt:1( @@ -1265,25 +1250,25 @@ MypyFile:1( ExpressionStmt:1( IndexExpr:1( NameExpr(x) - SliceExpr:1( + SliceExpr:-1( IntExpr(1) IntExpr(2)))) ExpressionStmt:2( IndexExpr:2( NameExpr(x) - SliceExpr:2( + SliceExpr:-1( IntExpr(1)))) ExpressionStmt:3( IndexExpr:3( NameExpr(x) - SliceExpr:3( + SliceExpr:-1( IntExpr(1) ))) ExpressionStmt:4( IndexExpr:4( NameExpr(x) - SliceExpr:4( + SliceExpr:-1( )))) @@ -1298,35 +1283,35 @@ MypyFile:1( ExpressionStmt:1( IndexExpr:1( NameExpr(x) - SliceExpr:1( + SliceExpr:-1( IntExpr(1) IntExpr(2) IntExpr(3)))) ExpressionStmt:2( IndexExpr:2( NameExpr(x) - SliceExpr:2( + SliceExpr:-1( IntExpr(1) IntExpr(2)))) ExpressionStmt:3( IndexExpr:3( NameExpr(x) - SliceExpr:3( + SliceExpr:-1( IntExpr(1) IntExpr(2)))) ExpressionStmt:4( IndexExpr:4( NameExpr(x) - SliceExpr:4( + SliceExpr:-1( IntExpr(2)))) ExpressionStmt:5( IndexExpr:5( NameExpr(x) - SliceExpr:5( + SliceExpr:-1( IntExpr(1) IntExpr(2))))) @@ -1541,7 +1526,7 @@ MypyFile:1( PassStmt:2()))) [case testGeneratorWithCondition] -x for y in z if 0 +(x for y in z if 0) [out] MypyFile:1( ExpressionStmt:1( @@ -2967,9 +2952,9 @@ MypyFile:1( PassStmt:8()))) [case testStarExprInGeneratorExpr] -x for y, *p in z -x for *p, y in z -x for y, *p, q in z +(x for y, *p in z) +(x for *p, y in z) +(x for y, *p, q in z) [out] MypyFile:1( ExpressionStmt:1( @@ -3034,7 +3019,7 @@ MypyFile:1( __class__))) [case testFunctionWithManyKindsOfArgs] -def f(x, *args, *, y=None, **kw): pass +def f(x, *args, y=None, **kw): pass [out] MypyFile:1( FuncDef:1( @@ -3145,10 +3130,10 @@ MypyFile:1( NameExpr(f) Args( NameExpr(y)) + VarArg KwArgs( x - IntExpr(1)) - VarArg))) + IntExpr(1))))) [case testConditionalExpressionInSetComprehension] { 1 if x else 2 for x in y } @@ -3214,11 +3199,11 @@ MypyFile:1( ExpressionStmt:1( IndexExpr:1( NameExpr(a) - TupleExpr:1( - SliceExpr:1( + TupleExpr:-1( + SliceExpr:-1( ) - SliceExpr:1( + SliceExpr:-1( ))))) @@ -3229,11 +3214,11 @@ MypyFile:1( ExpressionStmt:1( IndexExpr:1( NameExpr(a) - TupleExpr:1( - SliceExpr:1( + TupleExpr:-1( + SliceExpr:-1( IntExpr(1) IntExpr(2)) - SliceExpr:1( + SliceExpr:-1( ))))) @@ -3244,8 +3229,8 @@ MypyFile:1( ExpressionStmt:1( IndexExpr:1( NameExpr(a) - TupleExpr:1( - SliceExpr:1( + TupleExpr:-1( + SliceExpr:-1( IntExpr(1) IntExpr(2) IntExpr(3)) diff --git a/test-data/unit/python2eval.test b/test-data/unit/python2eval.test index 12fa5a62aea4f..b750de8b12a13 100644 --- a/test-data/unit/python2eval.test +++ b/test-data/unit/python2eval.test @@ -152,13 +152,14 @@ f(**params) [case testFromFutureImportUnicodeLiterals2_python2] from __future__ import unicode_literals -def f(x: str) -> None: pass +def f(x): # type: (str) -> None + pass f(b'') f(u'') f('') [out] -_program.py:4: error: Argument 1 to "f" has incompatible type "unicode"; expected "str" _program.py:5: error: Argument 1 to "f" has incompatible type "unicode"; expected "str" +_program.py:6: error: Argument 1 to "f" has incompatible type "unicode"; expected "str" [case testStrUnicodeCompatibility_python2] import typing @@ -391,9 +392,11 @@ f(b'') [file m.pyi] from typing import overload @overload -def f(x: unicode) -> int: pass +def f(x): # type: (unicode) -> int + pass @overload -def f(x: bytearray) -> int: pass +def f(x): # type: (bytearray) -> int + pass [out] _program.py:2: error: No overload variant of "f" matches argument types [builtins.int] @@ -465,7 +468,7 @@ re.subn(upat, lambda m: u'', u'')[0] + u'' [case testYieldRegressionTypingAwaitable_python2] # Make sure we don't reference typing.Awaitable in Python 2 mode. -def g() -> int: +def g(): # type: () -> int yield [out] _program.py:2: error: The return type of a generator function should be "Generator" or one of its supertypes diff --git a/test-data/unit/semanal-classes.test b/test-data/unit/semanal-classes.test index 431261fa3a182..a99f8511ad0c6 100644 --- a/test-data/unit/semanal-classes.test +++ b/test-data/unit/semanal-classes.test @@ -550,7 +550,7 @@ class A(None): pass class B(Any): pass class C(Callable[[], int]): pass [out] -main:2: error: Invalid base class +main: error: Invalid base class main:4: error: Invalid base class [case testTupleAsBaseClass] diff --git a/test-data/unit/semanal-errors.test b/test-data/unit/semanal-errors.test index 3d3a777a8ec72..ce4e684326223 100644 --- a/test-data/unit/semanal-errors.test +++ b/test-data/unit/semanal-errors.test @@ -1,11 +1,3 @@ -[case testPropagatingParseErrors] -in 1 -def f(): - 1 1 -[out] -main:1: error: Parse error before in -main:3: error: Parse error before numeric literal - [case testUndefinedVariableInGlobalStatement] import typing x @@ -95,8 +87,8 @@ from typing import overload class A: pass @overload def f(): pass -@overload -def f(x: A[int]) -> None: pass # E: "A" expects no type arguments, but 1 given +@overload # E: "A" expects no type arguments, but 1 given +def f(x: A[int]) -> None: pass [out] [case testInvalidNumberOfGenericArgsInBaseType] @@ -367,47 +359,78 @@ yield main:1: error: 'yield' outside function main:2: error: 'yield' outside function -[case testInvalidLvalues] +[case testInvalidLvalues1] 1 = 1 +[out] +main:1: error: can't assign to literal + +[case testInvalidLvalues2] (1) = 1 +[out] +main:1: error: can't assign to literal + +[case testInvalidLvalues3] (1, 1) = 1 +[out] +main:1: error: can't assign to literal + +[case testInvalidLvalues4] [1, 1] = 1 +[out] +main:1: error: can't assign to literal + +[case testInvalidLvalues5] () = 1 [out] -main:1: error: Invalid assignment target -main:2: error: Invalid assignment target -main:3: error: Invalid assignment target -main:4: error: Invalid assignment target -main:5: error: Can't assign to () ---' (hack to fix syntax highlighting) +main:1: error: can't assign to () -[case testInvalidLvalues2] -x = y = z = 1 +[case testInvalidLvalues6] +x = y = z = 1 # ok x, (y, 1) = 1 +[out] +main:2: error: can't assign to literal + +[case testInvalidLvalues7] x, [y, 1] = 1 +[out] +main:1: error: can't assign to literal + +[case testInvalidLvalues8] x, [y, [z, 1]] = 1 -x, (y, (z, 1)) = 1 +[out] +main:1: error: can't assign to literal + +[case testInvalidLvalues9] x, (y) = 1 # ok x, (y, (z, z)) = 1 # ok +x, (y, (z, 1)) = 1 [out] -main:2: error: Invalid assignment target -main:3: error: Invalid assignment target -main:4: error: Invalid assignment target -main:5: error: Invalid assignment target +main:3: error: can't assign to literal -[case testInvalidLvalues3] -x = 1 +[case testInvalidLvalues10] x + x = 1 +[out] +main:1: error: can't assign to operator + +[case testInvalidLvalues11] -x = 1 +[out] +main:1: error: can't assign to operator + +[case testInvalidLvalues12] 1.1 = 1 +[out] +main:1: error: can't assign to literal + +[case testInvalidLvalues13] 'x' = 1 +[out] +main:1: error: can't assign to literal + +[case testInvalidLvalues14] x() = 1 [out] -main:2: error: Invalid assignment target -main:3: error: Invalid assignment target -main:4: error: Invalid assignment target -main:5: error: Invalid assignment target -main:6: error: Invalid assignment target +main:1: error: can't assign to function call [case testTwoStarExpressions] a, *b, *c = 1 @@ -457,11 +480,17 @@ a = 1 [out] main:2: error: Can use starred expression only as assignment target -[case testInvalidDel] -import typing +[case testInvalidDel1] +x = 1 +del x(1) # E: can't delete function call +[out] + +[case testInvalidDel2] x = 1 -del x(1) # E: Invalid delete target -del x + 1 # E: Invalid delete target +del x + 1 # E: can't delete operator +[out] + +[case testInvalidDel3] del z # E: Name 'z' is not defined [out] @@ -820,7 +849,7 @@ import typing def f(): pass f() = 1 # type: int [out] -main:3: error: Invalid assignment target +main:3: error: can't assign to function call [case testIndexedAssignmentWithTypeDeclaration] import typing @@ -886,10 +915,14 @@ x, y = 1, 2 # type: int # E: Tuple type expected for multiple variables [case testInvalidLvalueWithExplicitType] a = 1 +a() = None # type: int # E: can't assign to function call +[out] + +[case testInvalidLvalueWithExplicitType2] +a = 1 a[1] = None # type: int # E: Unexpected type declaration a.x = None # type: int \ # E: Type cannot be declared in assignment to non-self attribute -a() = None # type: int # E: Invalid assignment target [out] [case testInvalidLvalueWithExplicitType3] @@ -1028,7 +1061,7 @@ def f(x: 'foo'): pass # E: Name 'foo' is not defined [out] [case testInvalidStrLiteralType2] -def f(x: 'int['): pass # E: Parse error before end of line +def f(x: 'int['): pass # E: syntax error in type comment [out] [case testInconsistentOverload] @@ -1200,7 +1233,7 @@ main:1: note: (Did you mean 'typing.Callable'?) [case testInvalidWithTarget] def f(): pass -with f() as 1: pass # E: Invalid assignment target +with f() as 1: pass # E: can't assign to literal [out] [case testUseObsoleteNameForTypeVar] @@ -1235,7 +1268,7 @@ import typing def f() -> None: f() = 1 # type: int [out] -main:3: error: Invalid assignment target +main:3: error: can't assign to function call [case testInvalidReferenceToAttributeOfOuterClass] class A: diff --git a/test-data/unit/semanal-expressions.test b/test-data/unit/semanal-expressions.test index 7c5728b01f373..cec2a3cc01106 100644 --- a/test-data/unit/semanal-expressions.test +++ b/test-data/unit/semanal-expressions.test @@ -115,20 +115,20 @@ MypyFile:1( ExpressionStmt:2( IndexExpr:2( NameExpr(x [__main__.x]) - SliceExpr:2( + SliceExpr:-1( NameExpr(y [__main__.y]) NameExpr(z [__main__.z]) NameExpr(x [__main__.x])))) ExpressionStmt:3( IndexExpr:3( NameExpr(x [__main__.x]) - SliceExpr:3( + SliceExpr:-1( ))) ExpressionStmt:4( IndexExpr:4( NameExpr(x [__main__.x]) - SliceExpr:4( + SliceExpr:-1( NameExpr(y [__main__.y]))))) @@ -309,7 +309,7 @@ MypyFile:1( [case testGeneratorExpression] a = 0 -x for x in a +(x for x in a) [out] MypyFile:1( AssignmentStmt:1( @@ -323,7 +323,7 @@ MypyFile:1( [case testGeneratorExpressionNestedIndex] a = 0 -x for x, (y, z) in a +(x for x, (y, z) in a) [out] MypyFile:1( AssignmentStmt:1( diff --git a/test-data/unit/semanal-statements.test b/test-data/unit/semanal-statements.test index 8825c8ead7e6c..e104ab75afab3 100644 --- a/test-data/unit/semanal-statements.test +++ b/test-data/unit/semanal-statements.test @@ -278,19 +278,23 @@ MypyFile:1( Then( ExpressionStmt:3( NameExpr(x [__main__.x]))) - If( - NameExpr(x [__main__.x])) - Then( - ExpressionStmt:5( - NameExpr(x [__main__.x]))) - If( - NameExpr(x [__main__.x])) - Then( - ExpressionStmt:7( - NameExpr(x [__main__.x]))) Else( - ExpressionStmt:9( - NameExpr(x [__main__.x]))))) + 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: @@ -537,7 +541,7 @@ MypyFile:1( def f(x, y) -> None: del x, y + 1 [out] -main:2: error: Invalid delete target +main:2: error: can't delete operator [case testTry] class c: pass