From 648e99e5c91e9b0dab34a7334455083608932076 Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Sat, 30 Apr 2022 16:07:59 -0600 Subject: [PATCH 01/13] Add test cases for `pow` that are meant to fail a type check --- test_cases/stdlib/builtins/test_pow.py | 8 ++++++++ tests/mypy_test.py | 7 ++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/test_cases/stdlib/builtins/test_pow.py b/test_cases/stdlib/builtins/test_pow.py index 093f05e18786..812ebea653d7 100644 --- a/test_cases/stdlib/builtins/test_pow.py +++ b/test_cases/stdlib/builtins/test_pow.py @@ -71,3 +71,11 @@ assert_type(pow(4.7, 9.2, None), Any) # See #7046 -- float for a positive 1st arg, complex otherwise assert_type((-95) ** 8.42, Any) + +# All of these should fail a type-checker +# mypy will emit an error if any of them do not fail, +# since we use --warn-unused-ignores when checking this subdirectory +pow(1.9, 4, 6) # type: ignore[misc] +pow(complex(6), 6.2, 7) # type: ignore[misc] +pow(Fraction(), 5, 8) # type: ignore[call-overload] +Decimal("8.7") ** 3.14 # type: ignore[operator] diff --git a/tests/mypy_test.py b/tests/mypy_test.py index dfa89f0b2239..ddceffbc308e 100755 --- a/tests/mypy_test.py +++ b/tests/mypy_test.py @@ -15,6 +15,7 @@ import os import re import subprocess +import shutil import sys import tempfile from pathlib import Path @@ -369,7 +370,11 @@ def test_the_test_cases(code: int, major: int, minor: int, args: argparse.Namesp flags = get_mypy_flags(args, major, minor, None, strict=True, custom_typeshed=True) print(f"Running mypy on the test_cases directory ({num_test_case_files} files)...") print("Running mypy " + " ".join(flags)) - this_code = subprocess.run([sys.executable, "-m", "mypy", "test_cases", *flags]).returncode + # --warn-unused-ignores doesn't work for files inside typeshed. + # SO, to work around this, we copy the test_cases directory into a TemporaryDirectory. + with tempfile.TemporaryDirectory() as td: + shutil.copytree(Path('test_cases'), Path(td) / 'test_cases') + this_code = subprocess.run([sys.executable, "-m", "mypy", td, *flags]).returncode code = max(code, this_code) return TestResults(code, num_test_case_files) From ba937511aa4fbb9cdc1c91b735341e8668273a20 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 30 Apr 2022 22:13:21 +0000 Subject: [PATCH 02/13] [pre-commit.ci] auto fixes from pre-commit.com hooks --- tests/mypy_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/mypy_test.py b/tests/mypy_test.py index ef9724d7a159..0a3a521dfb20 100755 --- a/tests/mypy_test.py +++ b/tests/mypy_test.py @@ -14,8 +14,8 @@ import argparse import os import re -import subprocess import shutil +import subprocess import sys import tempfile from pathlib import Path @@ -379,7 +379,7 @@ def test_the_test_cases(code: int, major: int, minor: int, args: argparse.Namesp # --warn-unused-ignores doesn't work for files inside typeshed. # SO, to work around this, we copy the test_cases directory into a TemporaryDirectory. with tempfile.TemporaryDirectory() as td: - shutil.copytree(Path('test_cases'), Path(td) / 'test_cases') + shutil.copytree(Path("test_cases"), Path(td) / "test_cases") this_code = subprocess.run([sys.executable, "-m", "mypy", td, *flags]).returncode code = max(code, this_code) return TestResults(code, num_test_case_files) From 0fe7a017ad8b1bdfc6e97948d97ed0fb23ae9867 Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Mon, 2 May 2022 21:15:44 +0100 Subject: [PATCH 03/13] Also get pyright to emit errors for unused ignores --- .github/workflows/tests.yml | 7 +++++ pyrightconfig.json | 3 +-- pyrightconfig.stricter.json | 3 +-- pyrightconfig.testcases.json | 51 ++++++++++++++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 pyrightconfig.testcases.json diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 285c62428ac0..189ad4f248bd 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -88,6 +88,13 @@ jobs: python-platform: ${{ matrix.python-platform }} python-version: ${{ matrix.python-version }} no-comments: ${{ matrix.python-version != '3.9' || matrix.python-platform != 'Linux' }} # Having each job create the same comment is too noisy. + project: ./pyrightconfig.tescases.json + - uses: jakebailey/pyright-action@v1 + with: + version: ${{ env.PYRIGHT_VERSION }} + python-platform: ${{ matrix.python-platform }} + python-version: ${{ matrix.python-version }} + no-comments: ${{ matrix.python-version != '3.9' || matrix.python-platform != 'Linux' }} # Having each job create the same comment is too noisy. stubtest-stdlib: name: Check stdlib with stubtest diff --git a/pyrightconfig.json b/pyrightconfig.json index 8bcc7aa96657..3d35f28a7250 100644 --- a/pyrightconfig.json +++ b/pyrightconfig.json @@ -2,8 +2,7 @@ "typeshedPath": ".", "include": [ "stdlib", - "stubs", - "test_cases" + "stubs" ], "exclude": [ "**/@python2" diff --git a/pyrightconfig.stricter.json b/pyrightconfig.stricter.json index 2f41cc1deedc..55eea79bf374 100644 --- a/pyrightconfig.stricter.json +++ b/pyrightconfig.stricter.json @@ -3,8 +3,7 @@ "typeshedPath": ".", "include": [ "stdlib", - "stubs", - "test_cases" + "stubs" ], "exclude": [ "**/@python2", diff --git a/pyrightconfig.testcases.json b/pyrightconfig.testcases.json new file mode 100644 index 000000000000..7f4f1b2a9013 --- /dev/null +++ b/pyrightconfig.testcases.json @@ -0,0 +1,51 @@ +{ + "$schema": "https://raw.githubusercontent.com/microsoft/pyright/main/packages/vscode-pyright/schemas/pyrightconfig.schema.json", + "typeshedPath": ".", + "include": [ + "test_cases" + ], + "typeCheckingMode": "basic", + "strictListInference": true, + "strictDictionaryInference": true, + "strictParameterNoneValue": true, + "reportFunctionMemberAccess": "error", + "reportMissingModuleSource": "none", + "reportMissingTypeStubs": "error", + "reportUnusedImport": "error", + "reportUnusedClass": "error", + "reportUnusedFunction": "error", + "reportUnusedVariable": "error", + "reportDuplicateImport": "error", + "reportOptionalSubscript": "error", + "reportOptionalMemberAccess": "error", + "reportOptionalCall": "error", + "reportOptionalIterable": "error", + "reportOptionalContextManager": "error", + "reportOptionalOperand": "error", + "reportUntypedFunctionDecorator": "error", + "reportUntypedClassDecorator": "error", + "reportUntypedBaseClass": "error", + "reportUntypedNamedTuple": "error", + "reportPrivateUsage": "none", + "reportConstantRedefinition": "error", + "reportInvalidStringEscapeSequence": "error", + "reportUnknownParameterType": "error", + "reportUnknownArgumentType": "error", + "reportUnknownLambdaType": "error", + "reportUnknownVariableType": "error", + "reportUnknownMemberType": "error", + "reportMissingTypeArgument": "error", + "reportUndefinedVariable": "error", + "reportUnboundVariable": "error", + "reportInvalidStubStatement": "error", + "reportInvalidTypeVarUse": "error", + "reportPropertyTypeMismatch": "error", + "reportSelfClsParameterName": "error", + "reportUnsupportedDunderAll": "error", + "reportIncompatibleMethodOverride": "error", + "reportIncompatibleVariableOverride": "error", + "reportOverlappingOverload": "error", + "reportUnnecessaryTypeIgnoreComment": "error", + "reportMissingTypeStubs": "error", + "reportMatchNotExhaustive": "error" +} \ No newline at end of file From b67dbd9fbbae1bb2895c853ed94d58d99bb12367 Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Mon, 2 May 2022 21:22:11 +0100 Subject: [PATCH 04/13] Tweak --- pyrightconfig.testcases.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pyrightconfig.testcases.json b/pyrightconfig.testcases.json index 7f4f1b2a9013..0eac46c5a16d 100644 --- a/pyrightconfig.testcases.json +++ b/pyrightconfig.testcases.json @@ -2,7 +2,7 @@ "$schema": "https://raw.githubusercontent.com/microsoft/pyright/main/packages/vscode-pyright/schemas/pyrightconfig.schema.json", "typeshedPath": ".", "include": [ - "test_cases" + "test_cases", ], "typeCheckingMode": "basic", "strictListInference": true, @@ -47,5 +47,5 @@ "reportOverlappingOverload": "error", "reportUnnecessaryTypeIgnoreComment": "error", "reportMissingTypeStubs": "error", - "reportMatchNotExhaustive": "error" -} \ No newline at end of file + "reportMatchNotExhaustive": "error", +} From cde04fccfd66fe66c2d2a20d0b6a60c332f2f4b5 Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Mon, 2 May 2022 21:26:58 +0100 Subject: [PATCH 05/13] Tweak again --- pyrightconfig.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pyrightconfig.json b/pyrightconfig.json index 3d35f28a7250..7e61059fb419 100644 --- a/pyrightconfig.json +++ b/pyrightconfig.json @@ -2,7 +2,8 @@ "typeshedPath": ".", "include": [ "stdlib", - "stubs" + "stubs", + "test_cases", ], "exclude": [ "**/@python2" From ba0ab67ac4c9d3b39e071f0e90183ef858f05c3f Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Mon, 2 May 2022 21:42:41 +0100 Subject: [PATCH 06/13] Maybe this --- pyrightconfig.json | 2 +- pyrightconfig.testcases.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pyrightconfig.json b/pyrightconfig.json index 7e61059fb419..8bcc7aa96657 100644 --- a/pyrightconfig.json +++ b/pyrightconfig.json @@ -3,7 +3,7 @@ "include": [ "stdlib", "stubs", - "test_cases", + "test_cases" ], "exclude": [ "**/@python2" diff --git a/pyrightconfig.testcases.json b/pyrightconfig.testcases.json index 0eac46c5a16d..b719608ca841 100644 --- a/pyrightconfig.testcases.json +++ b/pyrightconfig.testcases.json @@ -2,7 +2,7 @@ "$schema": "https://raw.githubusercontent.com/microsoft/pyright/main/packages/vscode-pyright/schemas/pyrightconfig.schema.json", "typeshedPath": ".", "include": [ - "test_cases", + "test_cases" ], "typeCheckingMode": "basic", "strictListInference": true, From 9c3cf0709993644b2296e321559b0880b5872059 Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Tue, 3 May 2022 10:49:19 +0100 Subject: [PATCH 07/13] Revert all changes to pyrightconfigs --- .github/workflows/tests.yml | 7 ----- pyrightconfig.stricter.json | 3 ++- pyrightconfig.testcases.json | 51 ------------------------------------ 3 files changed, 2 insertions(+), 59 deletions(-) delete mode 100644 pyrightconfig.testcases.json diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index d32940dde692..a9adcfe398b7 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -88,13 +88,6 @@ jobs: python-platform: ${{ matrix.python-platform }} python-version: ${{ matrix.python-version }} no-comments: ${{ matrix.python-version != '3.9' || matrix.python-platform != 'Linux' }} # Having each job create the same comment is too noisy. - project: ./pyrightconfig.tescases.json - - uses: jakebailey/pyright-action@v1 - with: - version: ${{ env.PYRIGHT_VERSION }} - python-platform: ${{ matrix.python-platform }} - python-version: ${{ matrix.python-version }} - no-comments: ${{ matrix.python-version != '3.9' || matrix.python-platform != 'Linux' }} # Having each job create the same comment is too noisy. stubtest-stdlib: name: Check stdlib with stubtest diff --git a/pyrightconfig.stricter.json b/pyrightconfig.stricter.json index 55eea79bf374..2f41cc1deedc 100644 --- a/pyrightconfig.stricter.json +++ b/pyrightconfig.stricter.json @@ -3,7 +3,8 @@ "typeshedPath": ".", "include": [ "stdlib", - "stubs" + "stubs", + "test_cases" ], "exclude": [ "**/@python2", diff --git a/pyrightconfig.testcases.json b/pyrightconfig.testcases.json deleted file mode 100644 index b719608ca841..000000000000 --- a/pyrightconfig.testcases.json +++ /dev/null @@ -1,51 +0,0 @@ -{ - "$schema": "https://raw.githubusercontent.com/microsoft/pyright/main/packages/vscode-pyright/schemas/pyrightconfig.schema.json", - "typeshedPath": ".", - "include": [ - "test_cases" - ], - "typeCheckingMode": "basic", - "strictListInference": true, - "strictDictionaryInference": true, - "strictParameterNoneValue": true, - "reportFunctionMemberAccess": "error", - "reportMissingModuleSource": "none", - "reportMissingTypeStubs": "error", - "reportUnusedImport": "error", - "reportUnusedClass": "error", - "reportUnusedFunction": "error", - "reportUnusedVariable": "error", - "reportDuplicateImport": "error", - "reportOptionalSubscript": "error", - "reportOptionalMemberAccess": "error", - "reportOptionalCall": "error", - "reportOptionalIterable": "error", - "reportOptionalContextManager": "error", - "reportOptionalOperand": "error", - "reportUntypedFunctionDecorator": "error", - "reportUntypedClassDecorator": "error", - "reportUntypedBaseClass": "error", - "reportUntypedNamedTuple": "error", - "reportPrivateUsage": "none", - "reportConstantRedefinition": "error", - "reportInvalidStringEscapeSequence": "error", - "reportUnknownParameterType": "error", - "reportUnknownArgumentType": "error", - "reportUnknownLambdaType": "error", - "reportUnknownVariableType": "error", - "reportUnknownMemberType": "error", - "reportMissingTypeArgument": "error", - "reportUndefinedVariable": "error", - "reportUnboundVariable": "error", - "reportInvalidStubStatement": "error", - "reportInvalidTypeVarUse": "error", - "reportPropertyTypeMismatch": "error", - "reportSelfClsParameterName": "error", - "reportUnsupportedDunderAll": "error", - "reportIncompatibleMethodOverride": "error", - "reportIncompatibleVariableOverride": "error", - "reportOverlappingOverload": "error", - "reportUnnecessaryTypeIgnoreComment": "error", - "reportMissingTypeStubs": "error", - "reportMatchNotExhaustive": "error", -} From 52480c5dc816d21aa24f19e578fe2eae3979fabb Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Tue, 3 May 2022 10:50:59 +0100 Subject: [PATCH 08/13] Add the setting for this specific file, as a workaround --- test_cases/stdlib/builtins/test_pow.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test_cases/stdlib/builtins/test_pow.py b/test_cases/stdlib/builtins/test_pow.py index 812ebea653d7..1116f000355d 100644 --- a/test_cases/stdlib/builtins/test_pow.py +++ b/test_cases/stdlib/builtins/test_pow.py @@ -1,3 +1,5 @@ +# pyright: reportUnnecessaryTypeIgnoreComment=true + from decimal import Decimal from fractions import Fraction from typing import Any, NoReturn From 0efc871cd427a95f39814fee30074ace4948b34e Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Tue, 3 May 2022 10:56:09 +0100 Subject: [PATCH 09/13] Double check that both mypy and pyright fail on this file if there are unused ignores --- test_cases/stdlib/builtins/test_pow.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test_cases/stdlib/builtins/test_pow.py b/test_cases/stdlib/builtins/test_pow.py index 1116f000355d..9b128660aacc 100644 --- a/test_cases/stdlib/builtins/test_pow.py +++ b/test_cases/stdlib/builtins/test_pow.py @@ -7,8 +7,8 @@ # See #7163 assert_type(pow(1, 0), Literal[1]) -assert_type(1**0, Literal[1]) -assert_type(pow(1, 0, None), Literal[1]) +assert_type(1**0, Literal[1]) # type: ignore[misc] +assert_type(pow(1, 0, None), Literal[1]) # type: ignore assert_type(pow(2, 4, 0), NoReturn) From 056ca63a7fabcd17ff99282604f35b09f61cbb55 Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Tue, 3 May 2022 11:05:45 +0100 Subject: [PATCH 10/13] They do --- test_cases/stdlib/builtins/test_pow.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test_cases/stdlib/builtins/test_pow.py b/test_cases/stdlib/builtins/test_pow.py index 9b128660aacc..1116f000355d 100644 --- a/test_cases/stdlib/builtins/test_pow.py +++ b/test_cases/stdlib/builtins/test_pow.py @@ -7,8 +7,8 @@ # See #7163 assert_type(pow(1, 0), Literal[1]) -assert_type(1**0, Literal[1]) # type: ignore[misc] -assert_type(pow(1, 0, None), Literal[1]) # type: ignore +assert_type(1**0, Literal[1]) +assert_type(pow(1, 0, None), Literal[1]) assert_type(pow(2, 4, 0), NoReturn) From e5333f49783826f92e4280a7fe32ed2456a448d2 Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Tue, 3 May 2022 11:24:05 +0100 Subject: [PATCH 11/13] Improve comments --- test_cases/stdlib/builtins/test_pow.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/test_cases/stdlib/builtins/test_pow.py b/test_cases/stdlib/builtins/test_pow.py index 1116f000355d..2456efba7914 100644 --- a/test_cases/stdlib/builtins/test_pow.py +++ b/test_cases/stdlib/builtins/test_pow.py @@ -56,7 +56,8 @@ assert_type(Decimal("4.6") ** 7, Decimal) # These would ideally be more precise, but `Any` is acceptable -# They have to be `Any` due to the fact that type-checkers can't distinguish between positive and negative numbers for the second argument to `pow()` +# They have to be `Any` due to the fact that type-checkers can't distinguish +# between positive and negative numbers for the second argument to `pow()` # # int for positive 2nd-arg, float otherwise assert_type(pow(4, 65), Any) @@ -74,9 +75,11 @@ # See #7046 -- float for a positive 1st arg, complex otherwise assert_type((-95) ** 8.42, Any) -# All of these should fail a type-checker -# mypy will emit an error if any of them do not fail, -# since we use --warn-unused-ignores when checking this subdirectory +# All of the following cases should fail a type-checker. +# +# mypy/pyright will emit errors if any of them do not fail: +# - We use --warn-unused-ignores for mypy when checking this subdirectory; +# - For pyright, we have reportUnnecessaryTypeIgnoreComment=true at the top of this file pow(1.9, 4, 6) # type: ignore[misc] pow(complex(6), 6.2, 7) # type: ignore[misc] pow(Fraction(), 5, 8) # type: ignore[call-overload] From 25c7435a00d1a703daf56a81475a3f3f97006bd8 Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Tue, 3 May 2022 11:33:47 +0100 Subject: [PATCH 12/13] Add two more cases and a TODO --- test_cases/stdlib/builtins/test_pow.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test_cases/stdlib/builtins/test_pow.py b/test_cases/stdlib/builtins/test_pow.py index 2456efba7914..c44dba056781 100644 --- a/test_cases/stdlib/builtins/test_pow.py +++ b/test_cases/stdlib/builtins/test_pow.py @@ -81,6 +81,11 @@ # - We use --warn-unused-ignores for mypy when checking this subdirectory; # - For pyright, we have reportUnnecessaryTypeIgnoreComment=true at the top of this file pow(1.9, 4, 6) # type: ignore[misc] +pow(4, 7, 4.32) # type: ignore[misc] +pow(6.2, 5.9, 73) # type: ignore[misc] pow(complex(6), 6.2, 7) # type: ignore[misc] pow(Fraction(), 5, 8) # type: ignore[call-overload] Decimal("8.7") ** 3.14 # type: ignore[operator] + +# TODO: This fails at runtime, but currently passes mypy and pyright: +pow(Decimal("8.5"), 3.21) \ No newline at end of file From 1222418fd4ec303273058b6a19c7e48854962665 Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Tue, 3 May 2022 11:34:40 +0100 Subject: [PATCH 13/13] Newline at EOF --- test_cases/stdlib/builtins/test_pow.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test_cases/stdlib/builtins/test_pow.py b/test_cases/stdlib/builtins/test_pow.py index c44dba056781..297c0449bf98 100644 --- a/test_cases/stdlib/builtins/test_pow.py +++ b/test_cases/stdlib/builtins/test_pow.py @@ -88,4 +88,4 @@ Decimal("8.7") ** 3.14 # type: ignore[operator] # TODO: This fails at runtime, but currently passes mypy and pyright: -pow(Decimal("8.5"), 3.21) \ No newline at end of file +pow(Decimal("8.5"), 3.21)