Add test cases for pow that are meant to fail a type check (#7760)

This commit is contained in:
Alex Waygood
2022-05-08 16:16:37 +01:00
committed by GitHub
parent 032d937bdf
commit bb39bdfd30
2 changed files with 25 additions and 2 deletions

View File

@@ -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(pow(Decimal("4.6"), 7, None), Decimal)
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(8.2, -9.8), Any)
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)

View File

@@ -14,6 +14,7 @@ from __future__ import annotations
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)