Skip to content

Commit 388b918

Browse files
authored
Merge pull request #29564 from jorenham/typing/py312-TypeAliasType
TYP: use ``TypeAliasType`` for ``ArrayLike`` and ``DTypeLike`` on py312+
2 parents 9b16cb3 + 7216e70 commit 388b918

2 files changed

Lines changed: 35 additions & 8 deletions

File tree

‎numpy/_typing/__init__.py‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
"""Private counterpart of ``numpy.typing``."""
22

3+
import sys
4+
35
from ._array_like import (
4-
ArrayLike as ArrayLike,
56
NDArray as NDArray,
67
_ArrayLike as _ArrayLike,
78
_ArrayLikeAnyString_co as _ArrayLikeAnyString_co,
@@ -84,7 +85,6 @@
8485

8586
#
8687
from ._dtype_like import (
87-
DTypeLike as DTypeLike,
8888
_DTypeLike as _DTypeLike,
8989
_DTypeLikeBool as _DTypeLikeBool,
9090
_DTypeLikeBytes as _DTypeLikeBytes,
@@ -118,8 +118,8 @@
118118
)
119119

120120
#
121-
from ._nbit_base import (
122-
NBitBase as NBitBase, # type: ignore[deprecated] # pyright: ignore[reportDeprecated]
121+
from ._nbit_base import ( # type: ignore[deprecated]
122+
NBitBase as NBitBase, # pyright: ignore[reportDeprecated]
123123
_8Bit as _8Bit,
124124
_16Bit as _16Bit,
125125
_32Bit as _32Bit,
@@ -156,3 +156,17 @@
156156
_UFunc_Nin2_Nout1 as _UFunc_Nin2_Nout1,
157157
_UFunc_Nin2_Nout2 as _UFunc_Nin2_Nout2,
158158
)
159+
160+
# wrapping the public aliases in `TypeAliasType` helps with introspection readability
161+
if sys.version_info >= (3, 12):
162+
from typing import TypeAliasType
163+
164+
from ._array_like import ArrayLike as _ArrayLikeAlias
165+
from ._dtype_like import DTypeLike as _DTypeLikeAlias
166+
167+
ArrayLike = TypeAliasType("ArrayLike", _ArrayLikeAlias)
168+
DTypeLike = TypeAliasType("DTypeLike", _DTypeLikeAlias)
169+
170+
else:
171+
from ._array_like import ArrayLike as ArrayLike
172+
from ._dtype_like import DTypeLike as DTypeLike

‎numpy/typing/tests/test_runtime.py‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,25 @@ class TypeTup(NamedTuple):
2222
origin: type | None
2323

2424

25+
def _flatten_type_alias(t: Any) -> Any:
26+
# "flattens" a TypeAliasType to its underlying type alias
27+
return getattr(t, "__value__", t)
28+
29+
2530
NDArrayTup = TypeTup(npt.NDArray, npt.NDArray.__args__, np.ndarray)
2631

2732
TYPES = {
28-
"ArrayLike": TypeTup(npt.ArrayLike, npt.ArrayLike.__args__, Union),
29-
"DTypeLike": TypeTup(npt.DTypeLike, npt.DTypeLike.__args__, Union),
30-
"NBitBase": TypeTup(npt.NBitBase, (), None),
33+
"ArrayLike": TypeTup(
34+
_flatten_type_alias(npt.ArrayLike),
35+
_flatten_type_alias(npt.ArrayLike).__args__,
36+
Union,
37+
),
38+
"DTypeLike": TypeTup(
39+
_flatten_type_alias(npt.DTypeLike),
40+
_flatten_type_alias(npt.DTypeLike).__args__,
41+
Union,
42+
),
43+
"NBitBase": TypeTup(npt.NBitBase, (), None), # type: ignore[deprecated] # pyright: ignore[reportDeprecated]
3144
"NDArray": NDArrayTup,
3245
}
3346

@@ -68,7 +81,7 @@ def test_get_type_hints_str(name: type, tup: TypeTup) -> None:
6881
def func(a: typ_str) -> None: pass
6982

7083
out = get_type_hints(func)
71-
ref = {"a": typ, "return": type(None)}
84+
ref = {"a": getattr(npt, str(name)), "return": type(None)}
7285
assert out == ref
7386

7487

0 commit comments

Comments
 (0)