Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 15 commits into from
May 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion test_cases/stdlib/builtins/test_pow.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# pyright: reportUnnecessaryTypeIgnoreComment=true

from decimal import Decimal
from fractions import Fraction
from typing import Any, NoReturn
Expand Down Expand Up @@ -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)
Expand All @@ -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)
7 changes: 6 additions & 1 deletion tests/mypy_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import argparse
import os
import re
import shutil
import subprocess
import sys
import tempfile
Expand Down Expand Up @@ -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)

Expand Down