8000 Merge pull request #24635 from BvB93/assert_type · Python-Repository-Hub/numpy@4db6fce · GitHub
[go: up one dir, main page]

Skip to content

Commit 4db6fce

Browse files
authored
Merge pull request numpy#24635 from BvB93/assert_type
ENH: Refactor the typing "reveal" tests using `typing.assert_type`
2 parents 59867b9 + cef217a commit 4db6fce

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+5587
-5190
lines changed

environment.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ dependencies:
2626
- hypothesis
2727
# For type annotations
2828
- typing_extensions>=4.2.0 # needed for python < 3.10
29-
- mypy=1.4.1
29+
- mypy=1.5.1
3030
# For building docs
3131
- sphinx>=4.5.0
3232
- sphinx-design

numpy/random/mtrand.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import builtins
12
from collections.abc import Callable
23
from typing import Any, Union, overload, Literal
34

@@ -224,7 +225,7 @@ class RandomState:
224225
size: None | _ShapeLike = ...,
225226
dtype: dtype[uint] | type[uint] | _UIntCodes | _SupportsDType[dtype[uint]] = ...,
226227
) -> ndarray[Any, dtype[uint]]: ...
227-
def bytes(self, length: int) -> bytes: ...
228+
def bytes(self, length: int) -> builtins.bytes: ...
228229
@overload
229230
def choice(
230231
self,

numpy/typing/mypy_plugin.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ def _get_precision_dict() -> dict[str, str]:
7575

7676

7777
def _get_extended_precision_list() -> list[str]:
78-
extended_types = [np.ulonglong, np.longlong, np.longdouble, np.clongdouble]
79-
extended_names = {
78+
extended_names = [
8079
"uint128",
8180
"uint256",
8281
"int128",
@@ -89,8 +88,8 @@ def _get_extended_precision_list() -> list[str]:
8988
"complex192",
9089
"complex256",
9190
"complex512",
92-
}
93-
return [i.__name__ for i in extended_types if i.__name__ in extended_names]
91+
]
92+
return [i for i in extended_names if hasattr(np, i)]
9493

9594

9695
def _get_c_intp_name() -> str:

numpy/typing/tests/data/fail/lib_polynomial.pyi

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ AR_U: npt.NDArray[np.str_]
88

99
poly_obj: np.poly1d
1010

11+
np.polymul(AR_f8, AR_U) # E: incompatible type
12+
np.polydiv(AR_f8, AR_U) # E: incompatible type
13+
14+
5**poly_obj # E: No overload variant
15+
1116
np.polyint(AR_U) # E: incompatible type
1217
np.polyint(AR_f8, m=1j) # E: No overload variant
1318

@@ -22,8 +27,3 @@ np.polyfit(AR_f8, AR_f8, 1, cov="bob") # E: No overload variant
2227
np.polyval(AR_f8, AR_U) # E: incompatible type
2328
np.polyadd(AR_f8, AR_U) # E: incompatible type
2429
np.polysub(AR_f8, AR_U) # E: incompatible type
25-
np.polymul(AR_f8, AR_U) # E: incompatible type
26-
np.polydiv(AR_f8, AR_U) # E: incompatible type
27-
28-
5**poly_obj # E: No overload variant
29-
hash(poly_obj)
Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
1+
import sys
2+
13
import numpy as np
4+
from numpy._typing import _80Bit, _96Bit, _128Bit, _256Bit
5+
6+
if sys.version_info >= (3, 11):
7+
from typing import assert_type
8+
else:
9+
from typing_extensions import assert_type
210

3-
reveal_type(np.uint128())
4-
reveal_type(np.uint256())
11+
assert_type(np.uint128(), np.unsignedinteger[_128Bit])
12+
assert_type(np.uint256(), np.unsignedinteger[_256Bit])
513

6-
reveal_type(np.int128())
7-
reveal_type(np.int256())
14+
assert_type(np.int128(), np.signedinteger[_128Bit])
15+
assert_type(np.int256(), np.signedinteger[_256Bit])
816

9-
reveal_type(np.float80())
10-
reveal_type(np.float96())
11-
reveal_type(np.float128())
12-
reveal_type(np.float256())
17+
assert_type(np.float80(), np.floating[_80Bit])
18+
assert_type(np.float96(), np.floating[_96Bit])
19+
assert_type(np.float128(), np.floating[_128Bit])
20+
assert_type(np.float256(), np.floating[_256Bit])
1321

14-
reveal_type(np.complex160())
15-
reveal_type(np.complex192())
16-
reveal_type(np.complex256())
17-
reveal_type(np.complex512())
22+
assert_type(np.complex160(), np.complexfloating[_80Bit, _80Bit])
23+
assert_type(np.complex192(), np.complexfloating[_96Bit, _96Bit])
24+
assert_type(np.complex256(), np.complexfloating[_128Bit, _128Bit])
25+
assert_type(np.complex512(), np.complexfloating[_256Bit, _256Bit])

numpy/typing/tests/data/mypy.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
plugins = numpy.typing.mypy_plugin
33
show_absolute_path = True
44
implicit_reexport = False
5+
pretty = True

0 commit comments

Comments
 (0)
0