From 42e2bb47966c72cbb97d2afc518a7d37079debff Mon Sep 17 00:00:00 2001 From: Adrian Freund Date: Tue, 4 May 2021 12:59:20 +0200 Subject: [PATCH] Fix a bunch of test broken by python 3.10 (#10404) Python 3.10 changes a bunch of parser error messages. This commit introduces the version>= argument to [out] sections of tests, which allows testing against the new messages on newer python versions. --- mypy/test/data.py | 48 +++++++++--- test-data/unit/check-errorcodes.test | 6 +- test-data/unit/cmdline.test | 7 ++ test-data/unit/fine-grained-blockers.test | 89 +++++++++++++++++++++++ test-data/unit/fine-grained-suggest.test | 5 ++ test-data/unit/parse.test | 10 ++- test-data/unit/semanal-errors.test | 28 ++++++- test-data/unit/semanal-statements.test | 2 + 8 files changed, 178 insertions(+), 17 deletions(-) diff --git a/mypy/test/data.py b/mypy/test/data.py index 13e199588e6e..6bf164d7623d 100644 --- a/mypy/test/data.py +++ b/mypy/test/data.py @@ -104,19 +104,43 @@ def parse_test_case(case: 'DataDrivenTestCase') -> None: full = join(base_path, m.group(1)) deleted_paths.setdefault(num, set()).add(full) elif re.match(r'out[0-9]*$', item.id): - if item.arg == 'skip-path-normalization': - normalize_output = False - - tmp_output = [expand_variables(line) for line in item.data] - if os.path.sep == '\\' and normalize_output: - tmp_output = [fix_win_path(line) for line in tmp_output] - if item.id == 'out' or item.id == 'out1': - output = tmp_output + if item.arg is None: + args = [] else: - passnum = int(item.id[len('out'):]) - assert passnum > 1 - output2[passnum] = tmp_output - out_section_missing = False + args = item.arg.split(",") + + version_check = True + for arg in args: + if arg == 'skip-path-normalization': + normalize_output = False + if arg.startswith("version"): + if arg[7:9] != ">=": + raise ValueError( + "{}, line {}: Only >= version checks are currently supported".format( + case.file, item.line + ) + ) + version_str = arg[9:] + try: + version = tuple(int(x) for x in version_str.split(".")) + except ValueError: + raise ValueError( + '{}, line {}: "{}" is not a valid python version'.format( + case.file, item.line, version_str)) + if not sys.version_info >= version: + version_check = False + + if version_check: + tmp_output = [expand_variables(line) for line in item.data] + if os.path.sep == '\\' and normalize_output: + tmp_output = [fix_win_path(line) for line in tmp_output] + if item.id == 'out' or item.id == 'out1': + output = tmp_output + else: + passnum = int(item.id[len('out'):]) + assert passnum > 1 + output2[passnum] = tmp_output + out_section_missing = False elif item.id == 'triggered' and item.arg is None: triggered = item.data else: diff --git a/test-data/unit/check-errorcodes.test b/test-data/unit/check-errorcodes.test index 7e1713af2c7d..94ad52d16fad 100644 --- a/test-data/unit/check-errorcodes.test +++ b/test-data/unit/check-errorcodes.test @@ -31,7 +31,11 @@ class A: reveal_type(1) # N: Revealed type is "Literal[1]?" [case testErrorCodeSyntaxError] -1 '' # E: invalid syntax [syntax] +1 '' +[out] +main:1: error: invalid syntax [syntax] +[out version>=3.10] +main:1: error: invalid syntax. Perhaps you forgot a comma? [syntax] [case testErrorCodeSyntaxError2] def f(): # E: Type signature has too many arguments [syntax] diff --git a/test-data/unit/cmdline.test b/test-data/unit/cmdline.test index d25f69ed8cf4..31137dbd1050 100644 --- a/test-data/unit/cmdline.test +++ b/test-data/unit/cmdline.test @@ -753,6 +753,9 @@ def foo( [out] a.py:1: error: unexpected EOF while parsing == Return code: 2 +[out version>=3.10] +a.py:1: error: '(' was never closed +== Return code: 2 [case testParseErrorAnnots] # cmd: mypy a.py @@ -1236,6 +1239,10 @@ x: str = 0 pkg/x.py:1: error: invalid syntax Found 1 error in 1 file (errors prevented further checking) == Return code: 2 +[out version>=3.10] +pkg/x.py:1: error: invalid syntax. Perhaps you forgot a comma? +Found 1 error in 1 file (errors prevented further checking) +== Return code: 2 [case testCmdlinePackageAndFile] # cmd: mypy -p pkg file diff --git a/test-data/unit/fine-grained-blockers.test b/test-data/unit/fine-grained-blockers.test index 65e74107c8ce..7ef66b6753c8 100644 --- a/test-data/unit/fine-grained-blockers.test +++ b/test-data/unit/fine-grained-blockers.test @@ -23,6 +23,12 @@ a.py:1: error: invalid syntax == main:2: error: Missing positional argument "x" in call to "f" == +[out version>=3.10] +== +a.py:1: error: expected ':' +== +main:2: error: Missing positional argument "x" in call to "f" +== [case testParseErrorShowSource] # flags: --pretty --show-error-codes @@ -46,6 +52,16 @@ main:3: error: Missing positional argument "x" in call to "f" [call-arg] a.f() ^ == +[out version>=3.10] +== +a.py:1: error: expected ':' [syntax] + def f(x: int) -> + ^ +== +main:3: error: Missing positional argument "x" in call to "f" [call-arg] + a.f() + ^ +== [case testParseErrorMultipleTimes] import a @@ -66,6 +82,13 @@ a.py:1: error: invalid syntax a.py:2: error: invalid syntax == main:2: error: Missing positional argument "x" in call to "f" +[out version>=3.10] +== +a.py:1: error: expected ':' +== +a.py:2: error: expected ':' +== +main:2: error: Missing positional argument "x" in call to "f" [case testSemanticAnalysisBlockingError] import a @@ -105,6 +128,14 @@ a.py:1: error: invalid syntax == main:3: error: Too many arguments for "f" main:5: error: Too many arguments for "f" +[out version>=3.10] +main:3: error: Too many arguments for "f" +main:5: error: Too many arguments for "f" +== +a.py:1: error: expected ':' +== +main:3: error: Too many arguments for "f" +main:5: error: Too many arguments for "f" [case testUpdateClassReferenceAcrossBlockingError] import a @@ -125,6 +156,11 @@ class C: a.py:1: error: invalid syntax == main:5: error: Missing positional argument "x" in call to "f" of "C" +[out version>=3.10] +== +a.py:1: error: invalid syntax. Perhaps you forgot a comma? +== +main:5: error: Missing positional argument "x" in call to "f" of "C" [case testAddFileWithBlockingError] import a @@ -140,6 +176,13 @@ main:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missin a.py:1: error: invalid syntax == main:2: error: Too many arguments for "f" +[out version>=3.10] +main:1: error: Cannot find implementation or library stub for module named "a" +main:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports +== +a.py:1: error: invalid syntax. Perhaps you forgot a comma? +== +main:2: error: Too many arguments for "f" [case testModifyTwoFilesOneWithBlockingError1] import a @@ -216,6 +259,13 @@ a.py:1: error: invalid syntax a.py:1: error: invalid syntax == a.py:2: error: Missing positional argument "x" in call to "f" +[out version>=3.10] +== +a.py:1: error: invalid syntax. Perhaps you forgot a comma? +== +a.py:1: error: invalid syntax. Perhaps you forgot a comma? +== +a.py:2: error: Missing positional argument "x" in call to "f" [case testModifyTwoFilesIntroduceTwoBlockingErrors] import a @@ -280,6 +330,13 @@ a.py:1: error: invalid syntax main:1: error: Cannot find implementation or library stub for module named "a" main:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports b.py:1: error: Cannot find implementation or library stub for module named "a" +[out version>=3.10] +== +a.py:1: error: invalid syntax. Perhaps you forgot a comma? +== +main:1: error: Cannot find implementation or library stub for module named "a" +main:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports +b.py:1: error: Cannot find implementation or library stub for module named "a" [case testDeleteFileWithBlockingError-only_when_cache] -- Different cache/no-cache tests because: @@ -301,6 +358,13 @@ a.py:1: error: invalid syntax b.py:1: error: Cannot find implementation or library stub for module named "a" b.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports main:1: error: Cannot find implementation or library stub for module named "a" +[out version>=3.10] +== +a.py:1: error: invalid syntax. Perhaps you forgot a comma? +== +b.py:1: error: Cannot find implementation or library stub for module named "a" +b.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports +main:1: error: Cannot find implementation or library stub for module named "a" [case testModifyFileWhileBlockingErrorElsewhere] import a @@ -324,6 +388,14 @@ a.py:1: error: invalid syntax == b.py:2: error: Module has no attribute "f" b.py:3: error: "int" not callable +[out version>=3.10] +== +a.py:1: error: invalid syntax. Perhaps you forgot a comma? +== +a.py:1: error: invalid syntax. Perhaps you forgot a comma? +== +b.py:2: error: Module has no attribute "f" +b.py:3: error: "int" not callable [case testImportBringsAnotherFileWithBlockingError1] import a @@ -339,6 +411,11 @@ def f() -> None: pass /test-data/unit/lib-stub/blocker.pyi:2: error: invalid syntax == a.py:1: error: "int" not callable +[out version>=3.10] +== +/test-data/unit/lib-stub/blocker.pyi:2: error: invalid syntax. Perhaps you forgot a comma? +== +a.py:1: error: "int" not callable [case testImportBringsAnotherFileWithSemanticAnalysisBlockingError] import a @@ -413,6 +490,13 @@ a.py:1: error: invalid syntax /test-data/unit/lib-stub/blocker.pyi:2: error: invalid syntax == a.py:2: error: "int" not callable +[out version>=3.10] +== +a.py:1: error: invalid syntax. Perhaps you forgot a comma? +== +/test-data/unit/lib-stub/blocker.pyi:2: error: invalid syntax. Perhaps you forgot a comma? +== +a.py:2: error: "int" not callable [case testInitialBlocker] # cmd: mypy a.py b.py @@ -431,6 +515,11 @@ a.py:1: error: invalid syntax == b.py:2: error: Incompatible return value type (got "str", expected "int") == +[out version>=3.10] +a.py:1: error: invalid syntax. Perhaps you forgot a comma? +== +b.py:2: error: Incompatible return value type (got "str", expected "int") +== [case testDecodeErrorBlocker-posix] import a diff --git a/test-data/unit/fine-grained-suggest.test b/test-data/unit/fine-grained-suggest.test index 34bf0ff1ccf7..4a1bda8b0afd 100644 --- a/test-data/unit/fine-grained-suggest.test +++ b/test-data/unit/fine-grained-suggest.test @@ -1019,6 +1019,11 @@ foo.py:4: error: unexpected EOF while parsing Command 'suggest' is only valid after a 'check' command (that produces no parse errors) == foo.py:4: error: unexpected EOF while parsing +[out version>=3.10] +foo.py:4: error: '(' was never closed +Command 'suggest' is only valid after a 'check' command (that produces no parse errors) +== +foo.py:4: error: '(' was never closed -- ) [case testSuggestRefine] diff --git a/test-data/unit/parse.test b/test-data/unit/parse.test index 3b1d1198c269..22b1ab28481d 100644 --- a/test-data/unit/parse.test +++ b/test-data/unit/parse.test @@ -932,16 +932,22 @@ MypyFile:1( NameExpr(z))))) [case testNotAsBinaryOp] -x not y # E: invalid syntax +x not y [out] +main:1: error: invalid syntax +[out version>=3.10] +main:1: error: invalid syntax. Perhaps you forgot a comma? [case testNotIs] x not is y # E: invalid syntax [out] [case testBinaryNegAsBinaryOp] -1 ~ 2 # E: invalid syntax +1 ~ 2 [out] +main:1: error: invalid syntax +[out version>=3.10] +main:1: error: invalid syntax. Perhaps you forgot a comma? [case testDictionaryExpression] {} diff --git a/test-data/unit/semanal-errors.test b/test-data/unit/semanal-errors.test index eab1ff958bc1..be8f967c9fb0 100644 --- a/test-data/unit/semanal-errors.test +++ b/test-data/unit/semanal-errors.test @@ -361,11 +361,15 @@ main:2: error: 'yield' outside function 1 = 1 [out] main:1: error: can't assign to literal +[out version>=3.10] +main:1: error: can't assign to literal here. Maybe you meant '==' instead of '='? [case testInvalidLvalues2] (1) = 1 [out] main:1: error: can't assign to literal +[out version>=3.10] +main:1: error: can't assign to literal here. Maybe you meant '==' instead of '='? [case testInvalidLvalues3] (1, 1) = 1 @@ -404,26 +408,36 @@ main:3: error: can't assign to literal x + x = 1 [out] main:1: error: can't assign to operator +[out version>=3.10] +main:1: error: can't assign to expression here. Maybe you meant '==' instead of '='? [case testInvalidLvalues11] -x = 1 [out] main:1: error: can't assign to operator +[out version>=3.10] +main:1: error: can't assign to expression here. Maybe you meant '==' instead of '='? [case testInvalidLvalues12] 1.1 = 1 [out] main:1: error: can't assign to literal +[out version>=3.10] +main:1: error: can't assign to literal here. Maybe you meant '==' instead of '='? [case testInvalidLvalues13] 'x' = 1 [out] main:1: error: can't assign to literal +[out version>=3.10] +main:1: error: can't assign to literal here. Maybe you meant '==' instead of '='? [case testInvalidLvalues14] x() = 1 [out] main:1: error: can't assign to function call +[out version>=3.10] +main:1: error: can't assign to function call here. Maybe you meant '==' instead of '='? [case testTwoStarExpressions] a, *b, *c = 1 @@ -480,8 +494,11 @@ del x(1) # E: can't delete function call [case testInvalidDel2] x = 1 -del x + 1 # E: can't delete operator +del x + 1 [out] +main:2: error: cannot delete operator +[out version>=3.10] +main:2: error: cannot delete expression [case testInvalidDel3] del z # E: Name "z" is not defined @@ -868,6 +885,8 @@ def f(): pass f() = 1 # type: int [out] main:3: error: can't assign to function call +[out version>=3.10] +main:3: error: can't assign to function call here. Maybe you meant '==' instead of '='? [case testIndexedAssignmentWithTypeDeclaration] import typing @@ -941,8 +960,11 @@ 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 +a() = None # type: int [out] +main:2: error: cannot assign to function call +[out version>=3.10] +main:2: error: cannot assign to function call here. Maybe you meant '==' instead of '='? [case testInvalidLvalueWithExplicitType2] a = 1 @@ -1263,6 +1285,8 @@ def f() -> None: f() = 1 # type: int [out] main:3: error: can't assign to function call +[out version>=3.10] +main:3: error: can't assign to function call here. Maybe you meant '==' instead of '='? [case testInvalidReferenceToAttributeOfOuterClass] class A: diff --git a/test-data/unit/semanal-statements.test b/test-data/unit/semanal-statements.test index b6136da37f6b..97dd5f048834 100644 --- a/test-data/unit/semanal-statements.test +++ b/test-data/unit/semanal-statements.test @@ -551,6 +551,8 @@ def f(x, y) -> None: del x, y + 1 [out] main:2: error: can't delete operator +[out version>=3.10] +main:2: error: can't delete expression [case testTry] class c: pass