Skip to content

Commit bb39bdf

Browse files
authored
Add test cases for pow that are meant to fail a type check (#7760)
1 parent 032d937 commit bb39bdf

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

test_cases/stdlib/builtins/test_pow.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# pyright: reportUnnecessaryTypeIgnoreComment=true
2+
13
from decimal import Decimal
24
from fractions import Fraction
35
from typing import Any, NoReturn
@@ -54,7 +56,8 @@
5456
assert_type(Decimal("4.6") ** 7, Decimal)
5557

5658
# These would ideally be more precise, but `Any` is acceptable
57-
# 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()`
59+
# They have to be `Any` due to the fact that type-checkers can't distinguish
60+
# between positive and negative numbers for the second argument to `pow()`
5861
#
5962
# int for positive 2nd-arg, float otherwise
6063
assert_type(pow(4, 65), Any)
@@ -71,3 +74,18 @@
7174
assert_type(pow(4.7, 9.2, None), Any)
7275
# See #7046 -- float for a positive 1st arg, complex otherwise
7376
assert_type((-95) ** 8.42, Any)
77+
78+
# All of the following cases should fail a type-checker.
79+
#
80+
# mypy/pyright will emit errors if any of them do not fail:
81+
# - We use --warn-unused-ignores for mypy when checking this subdirectory;
82+
# - For pyright, we have reportUnnecessaryTypeIgnoreComment=true at the top of this file
83+
pow(1.9, 4, 6) # type: ignore[misc]
84+
pow(4, 7, 4.32) # type: ignore[misc]
85+
pow(6.2, 5.9, 73) # type: ignore[misc]
86+
pow(complex(6), 6.2, 7) # type: ignore[misc]
87+
pow(Fraction(), 5, 8) # type: ignore[call-overload]
88+
Decimal("8.7") ** 3.14 # type: ignore[operator]
89+
90+
# TODO: This fails at runtime, but currently passes mypy and pyright:
91+
pow(Decimal("8.5"), 3.21)

tests/mypy_test.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import argparse
1515
import os
1616
import re
17+
import shutil
1718
import subprocess
1819
import sys
1920
import tempfile
@@ -375,7 +376,11 @@ def test_the_test_cases(code: int, major: int, minor: int, args: argparse.Namesp
375376
if args.dry_run:
376377
this_code = 0
377378
else:
378-
this_code = subprocess.run([sys.executable, "-m", "mypy", "test_cases", *flags]).returncode
379+
# --warn-unused-ignores doesn't work for files inside typeshed.
380+
# SO, to work around this, we copy the test_cases directory into a TemporaryDirectory.
381+
with tempfile.TemporaryDirectory() as td:
382+
shutil.copytree(Path("test_cases"), Path(td) / "test_cases")
383+
this_code = subprocess.run([sys.executable, "-m", "mypy", td, *flags]).returncode
379384
code = max(code, this_code)
380385
return TestResults(code, num_test_case_files)
381386

0 commit comments

Comments
 (0)