Skip to content

Commit 2773480

Browse files
authored
Add regression tests for builtins.pow and object.__reduce__ (#7663)
1 parent 66383ee commit 2773480

File tree

5 files changed

+79
-2
lines changed

5 files changed

+79
-2
lines changed

pyrightconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"typeshedPath": ".",
33
"include": [
44
"stdlib",
5-
"stubs"
5+
"stubs",
6+
"test_cases"
67
],
78
"exclude": [
89
"**/@python2"

pyrightconfig.stricter.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"typeshedPath": ".",
44
"include": [
55
"stdlib",
6-
"stubs"
6+
"stubs",
7+
"test_cases"
78
],
89
"exclude": [
910
"**/@python2",

test_cases/README.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
## Regression tests for typeshed
2+
3+
This directory contains regression tests for the stubs found elsewhere in the
4+
typeshed repo. Each file contains a number of test cases, all of which should
5+
pass a type checker without error.
6+
7+
**This directory should *only* contain tests for functions and classes which
8+
are known to have caused problems in the past, where the stubs are difficult to
9+
get right.** 100% test coverage for typeshed is neither necessary nor
10+
desirable, as it would lead to code duplication. Moreover, typeshed has
11+
multiple other mechanisms for spotting errors in the stubs.
12+
13+
Unlike the rest of typeshed, this directory largely contains `.py` files. This
14+
is because the purpose of this folder is to test the implications of typeshed
15+
changes for end users.
16+
17+
Another difference to the rest of typeshed is that the test cases in this
18+
directory cannot always use modern syntax for type hints. For example, PEP 604
19+
syntax (unions with a pipe `|` operator) is new in Python 3.10. While this
20+
syntax can be used on older Python versions in a `.pyi` file, code using this
21+
syntax will fail at runtime on Python <=3.9. Since the test cases all use `.py`
22+
extensions, and since the tests need to pass on all Python versions >=3.6, PEP
23+
604 syntax cannot be used in a test case. Use `typing.Union` and
24+
`typing.Optional` instead.
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from typing import Any, Tuple, Union
2+
3+
4+
# The following should pass without error (see #6661):
5+
class Diagnostic:
6+
def __reduce__(self) -> Union[str, Tuple[Any, ...]]:
7+
res = super().__reduce__()
8+
if isinstance(res, tuple) and len(res) >= 3:
9+
res[2]["_info"] = 42
10+
11+
return res
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from decimal import Decimal
2+
from fractions import Fraction
3+
from typing import Any, NoReturn
4+
from typing_extensions import Literal, assert_type
5+
6+
assert_type(pow(1, 0), Literal[1]) # See #7163
7+
assert_type(pow(1, 0, None), Literal[1]) # See #7163
8+
assert_type(pow(2, 4, 0), NoReturn)
9+
assert_type(pow(2, 4), int)
10+
assert_type(pow(4, 6, None), int)
11+
assert_type(pow(5, -7), float)
12+
assert_type(pow(2, 4, 5), int) # pow(<smallint>, <smallint>, <smallint>)
13+
assert_type(pow(2, 35, 3), int) # pow(<smallint>, <bigint>, <smallint>)
14+
assert_type(pow(4.6, 8), float)
15+
assert_type(pow(5.1, 4, None), float)
16+
assert_type(pow(complex(6), 6.2), complex)
17+
assert_type(pow(complex(9), 7.3, None), complex)
18+
assert_type(pow(Fraction(), 4, None), Fraction)
19+
assert_type(pow(Fraction(3, 7), complex(1, 8)), complex)
20+
assert_type(pow(complex(4, -8), Fraction(2, 3)), complex)
21+
assert_type(pow(Decimal("1.0"), Decimal("1.6")), Decimal)
22+
assert_type(pow(Decimal("1.0"), Decimal("1.0"), Decimal("1.0")), Decimal)
23+
assert_type(pow(Decimal("4.6"), 7, None), Decimal)
24+
25+
# These would ideally be more precise, but `Any` is acceptable
26+
# 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()`
27+
#
28+
# int for positive 2nd-arg, float otherwise
29+
assert_type(pow(4, 65), Any)
30+
assert_type(pow(2, -45), Any)
31+
assert_type(pow(3, 57, None), Any)
32+
# pow(<pos-float>, <pos-or-neg-float>) -> float
33+
# pow(<neg-float>, <pos-or-neg-float>) -> complex
34+
assert_type(pow(4.7, 7.4), Any)
35+
assert_type(pow(-9.8, 8.3), Any)
36+
assert_type(pow(-9.3, -88.2), Any)
37+
assert_type(pow(8.2, -9.8), Any)
38+
assert_type(pow(4.7, 9.2, None), Any)
39+
# See #7046 -- float for a positive 1st arg, complex otherwise
40+
assert_type((-2) ** 0.5, Any)

0 commit comments

Comments
 (0)