8000 ENH: Refactor the typing "reveal" tests using `typing.assert_type` by BvB93 · Pull Request #24637 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

ENH: Refactor the typing "reveal" tests using typing.assert_type #24637

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

Merged
merged 5 commits into from
Sep 4, 2023
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
2 changes: 1 addition & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ dependencies:
- hypothesis
# For type annotations
- typing_extensions>=4.2.0 # needed for python < 3.10
- mypy=1.4.1
- mypy=1.5.1
# For building docs
- sphinx>=4.5.0
- sphinx-design
Expand Down
3 changes: 2 additions & 1 deletion numpy/random/mtrand.pyi
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import builtins
from collections.abc import Callable
from typing import Any, Union, overload, Literal

Expand Down Expand Up @@ -224,7 +225,7 @@ class RandomState:
size: None | _ShapeLike = ...,
dtype: dtype[uint] | type[uint] | _UIntCodes | _SupportsDType[dtype[uint]] = ...,
) -> ndarray[Any, dtype[uint]]: ...
def bytes(self, length: int) -> bytes: ...
def bytes(self, length: int) -> builtins.bytes: ...
@overload
def choice(
self,
Expand Down
7 changes: 3 additions & 4 deletions numpy/typing/mypy_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ def _get_precision_dict() -> dict[str, str]:


def _get_extended_precision_list() -> list[str]:
extended_types = [np.ulonglong, np.longlong, np.longdouble, np.clongdouble]
extended_names = {
extended_names = [
"uint128",
"uint256",
"int128",
Expand All @@ -89,8 +88,8 @@ def _get_extended_precision_list() -> list[str]:
"complex192",
"complex256",
"complex512",
}
return [i.__name__ for i in extended_types if i.__name__ in extended_names]
]
return [i for i in extended_names if hasattr(np, i)]


def _get_c_intp_name() -> str:
Expand Down
10 changes: 5 additions & 5 deletions numpy/typing/tests/data/fail/lib_polynomial.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ AR_U: npt.NDArray[np.str_]

poly_obj: np.poly1d

np.polymul(AR_f8, AR_U) # E: incompatible type
np.polydiv(AR_f8, AR_U) # E: incompatible type

5**poly_obj # E: No overload variant

np.polyint(AR_U) # E: incompatible type
np.polyint(AR_f8, m=1j) # E: No overload variant

Expand All @@ -22,8 +27,3 @@ np.polyfit(AR_f8, AR_f8, 1, cov="bob") # E: No overload variant
np.polyval(AR_f8, AR_U) # E: incompatible type
np.polyadd(AR_f8, AR_U) # E: incompatible type
np.polysub(AR_f8, AR_U) # E: incompatible type
np.polymul(AR_f8, AR_U) # E: incompatible type
np.polydiv(AR_f8, AR_U) # E: incompatible type

5**poly_obj # E: No overload variant
hash(poly_obj)
32 changes: 20 additions & 12 deletions numpy/typing/tests/data/misc/extended_precision.pyi
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
import sys

import numpy as np
from numpy._typing import _80Bit, _96Bit, _128Bit, _256Bit

if sys.version_info >= (3, 11):
from typing import assert_type
else:
from typing_extensions import assert_type

reveal_type(np.uint128())
reveal_type(np.uint256())
assert_type(np.uint128(), np.unsignedinteger[_128Bit])
assert_type(np.uint256(), np.unsignedinteger[_256Bit])

reveal_type(np.int128())
reveal_type(np.int256())
assert_type(np.int128(), np.signedinteger[_128Bit])
assert_type(np.int256(), np.signedinteger[_256Bit])

reveal_type(np.float80())
reveal_type(np.float96())
reveal_type(np.float128())
reveal_type(np.float256())
assert_type(np.float80(), np.floating[_80Bit])
assert_type(np.float96(), np.floating[_96Bit])
assert_type(np.float128(), np.floating[_128Bit])
assert_type(np.float256(), np.floating[_256Bit])

reveal_type(np.complex160())
reveal_type(np.complex192())
reveal_type(np.complex256())
reveal_type(np.complex512())
assert_type(np.complex160(), np.complexfloating[_80Bit, _80Bit])
assert_type(np.complex192(), np.complexfloating[_96Bit, _96Bit])
assert_type(np.complex256(), np.complexfloating[_128Bit, _128Bit])
assert_type(np.complex512(), np.complexfloating[_256Bit, _256Bit])
1 change: 1 addition & 0 deletions numpy/typing/tests/data/mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
plugins = numpy.typing.mypy_plugin
show_absolute_path = True
implicit_reexport = False
pretty = True
Loading
0