From bb39bdfd30da94aa9398f3ea5eb53de7b182f4fb Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Sun, 8 May 2022 16:16:37 +0100 Subject: [PATCH] Add test cases for `pow` that are meant to fail a type check (#7760) --- test_cases/stdlib/builtins/test_pow.py | 20 +++++++++++++++++++- tests/mypy_test.py | 7 ++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/test_cases/stdlib/builtins/test_pow.py b/test_cases/stdlib/builtins/test_pow.py index 093f05e18786..297c0449bf98 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 @@ -54,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) @@ -71,3 +74,18 @@ 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 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(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) diff --git a/tests/mypy_test.py b/tests/mypy_test.py index 80ad98438440..0a3a521dfb20 100755 --- a/tests/mypy_test.py +++ b/tests/mypy_test.py @@ -14,6 +14,7 @@ import argparse import os import re +import shutil import subprocess import sys import tempfile @@ -375,7 +376,11 @@ def test_the_test_cases(code: int, major: int, minor: int, args: argparse.Namesp if args.dry_run: this_code = 0 else: - 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)