Skip to content

Commit 5098b5a

Browse files
authored
Merge pull request numpy#28657 from jorenham/typing/_typing-cleanup
TYP: simplified type-aliases in ``numpy._typing``
2 parents a6ebba8 + cec5cd7 commit 5098b5a

File tree

3 files changed

+77
-292
lines changed

3 files changed

+77
-292
lines changed

numpy/_typing/_array_like.py

Lines changed: 25 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,7 @@
55
from typing import Any, Protocol, TypeAlias, TypeVar, runtime_checkable, TYPE_CHECKING
66

77
import numpy as np
8-
from numpy import (
9-
ndarray,
10-
dtype,
11-
generic,
12-
unsignedinteger,
13-
integer,
14-
floating,
15-
complexfloating,
16-
number,
17-
timedelta64,
18-
datetime64,
19-
object_,
20-
void,
21-
str_,
22-
bytes_,
23-
)
8+
from numpy import dtype
249
from ._nbit_base import _32Bit, _64Bit
2510
from ._nested_sequence import _NestedSequence
2611
from ._shape import _Shape
@@ -33,12 +18,12 @@
3318
from numpy._core.multiarray import StringDType
3419

3520
_T = TypeVar("_T")
36-
_ScalarType = TypeVar("_ScalarType", bound=generic)
37-
_ScalarType_co = TypeVar("_ScalarType_co", bound=generic, covariant=True)
21+
_ScalarType = TypeVar("_ScalarType", bound=np.generic)
22+
_ScalarType_co = TypeVar("_ScalarType_co", bound=np.generic, covariant=True)
3823
_DType = TypeVar("_DType", bound=dtype[Any])
3924
_DType_co = TypeVar("_DType_co", covariant=True, bound=dtype[Any])
4025

41-
NDArray: TypeAlias = ndarray[_Shape, dtype[_ScalarType_co]]
26+
NDArray: TypeAlias = np.ndarray[_Shape, dtype[_ScalarType_co]]
4227

4328
# The `_SupportsArray` protocol only cares about the default dtype
4429
# (i.e. `dtype=None` or no `dtype` parameter at all) of the to-be returned
@@ -47,7 +32,7 @@
4732
# any and all remaining overloads
4833
@runtime_checkable
4934
class _SupportsArray(Protocol[_DType_co]):
50-
def __array__(self) -> ndarray[Any, _DType_co]: ...
35+
def __array__(self) -> np.ndarray[Any, _DType_co]: ...
5136

5237

5338
@runtime_checkable
@@ -94,87 +79,33 @@ def __array_function__(
9479
class _Buffer(Protocol):
9580
def __buffer__(self, flags: int, /) -> memoryview: ...
9681

97-
ArrayLike: TypeAlias = _Buffer | _DualArrayLike[
98-
dtype[Any],
99-
bool | int | float | complex | str | bytes,
100-
]
82+
ArrayLike: TypeAlias = _Buffer | _DualArrayLike[dtype[Any], complex | bytes | str]
10183

10284
# `ArrayLike<X>_co`: array-like objects that can be coerced into `X`
10385
# given the casting rules `same_kind`
104-
_ArrayLikeBool_co: TypeAlias = _DualArrayLike[
105-
dtype[np.bool],
106-
bool,
107-
]
108-
_ArrayLikeUInt_co: TypeAlias = _DualArrayLike[
109-
dtype[np.bool] | dtype[unsignedinteger[Any]],
110-
bool,
111-
]
112-
_ArrayLikeInt_co: TypeAlias = _DualArrayLike[
113-
dtype[np.bool] | dtype[integer[Any]],
114-
bool | int,
115-
]
116-
_ArrayLikeFloat_co: TypeAlias = _DualArrayLike[
117-
dtype[np.bool] | dtype[integer[Any]] | dtype[floating[Any]],
118-
bool | int | float,
119-
]
120-
_ArrayLikeComplex_co: TypeAlias = _DualArrayLike[
121-
(
122-
dtype[np.bool]
123-
| dtype[integer[Any]]
124-
| dtype[floating[Any]]
125-
| dtype[complexfloating[Any, Any]]
126-
),
127-
bool | int | float | complex,
128-
]
129-
_ArrayLikeNumber_co: TypeAlias = _DualArrayLike[
130-
dtype[np.bool] | dtype[number[Any]],
131-
bool | int | float | complex,
132-
]
133-
_ArrayLikeTD64_co: TypeAlias = _DualArrayLike[
134-
dtype[np.bool] | dtype[integer[Any]] | dtype[timedelta64],
135-
bool | int,
136-
]
137-
_ArrayLikeDT64_co: TypeAlias = (
138-
_SupportsArray[dtype[datetime64]]
139-
| _NestedSequence[_SupportsArray[dtype[datetime64]]]
140-
)
141-
_ArrayLikeObject_co: TypeAlias = (
142-
_SupportsArray[dtype[object_]]
143-
| _NestedSequence[_SupportsArray[dtype[object_]]]
144-
)
145-
146-
_ArrayLikeVoid_co: TypeAlias = (
147-
_SupportsArray[dtype[void]]
148-
| _NestedSequence[_SupportsArray[dtype[void]]]
149-
)
150-
_ArrayLikeStr_co: TypeAlias = _DualArrayLike[
151-
dtype[str_],
152-
str,
153-
]
154-
_ArrayLikeBytes_co: TypeAlias = _DualArrayLike[
155-
dtype[bytes_],
156-
bytes,
157-
]
158-
_ArrayLikeString_co: TypeAlias = _DualArrayLike[
159-
StringDType,
160-
str
161-
]
162-
_ArrayLikeAnyString_co: TypeAlias = (
163-
_ArrayLikeStr_co |
164-
_ArrayLikeBytes_co |
165-
_ArrayLikeString_co
166-
)
86+
_ArrayLikeBool_co: TypeAlias = _DualArrayLike[dtype[np.bool], bool]
87+
_ArrayLikeUInt_co: TypeAlias = _DualArrayLike[dtype[np.bool | np.unsignedinteger], bool]
88+
_ArrayLikeInt_co: TypeAlias = _DualArrayLike[dtype[np.bool | np.integer], int]
89+
_ArrayLikeFloat_co: TypeAlias = _DualArrayLike[dtype[np.bool | np.integer | np.floating], float]
90+
_ArrayLikeComplex_co: TypeAlias = _DualArrayLike[dtype[np.bool | np.number], complex]
91+
_ArrayLikeNumber_co: TypeAlias = _ArrayLikeComplex_co
92+
_ArrayLikeTD64_co: TypeAlias = _DualArrayLike[dtype[np.bool | np.integer | np.timedelta64], int]
93+
_ArrayLikeDT64_co: TypeAlias = _ArrayLike[np.datetime64]
94+
_ArrayLikeObject_co: TypeAlias = _ArrayLike[np.object_]
95+
96+
_ArrayLikeVoid_co: TypeAlias = _ArrayLike[np.void]
97+
_ArrayLikeBytes_co: TypeAlias = _DualArrayLike[dtype[np.bytes_], bytes]
98+
_ArrayLikeStr_co: TypeAlias = _DualArrayLike[dtype[np.str_], str]
99+
_ArrayLikeString_co: TypeAlias = _DualArrayLike[StringDType, str]
100+
_ArrayLikeAnyString_co: TypeAlias = _DualArrayLike[dtype[np.character] | StringDType, bytes | str]
167101

168102
__Float64_co: TypeAlias = np.floating[_64Bit] | np.float32 | np.float16 | np.integer | np.bool
169103
__Complex128_co: TypeAlias = np.number[_64Bit] | np.number[_32Bit] | np.float16 | np.integer | np.bool
170-
_ArrayLikeFloat64_co: TypeAlias = _DualArrayLike[dtype[__Float64_co], float | int]
171-
_ArrayLikeComplex128_co: TypeAlias = _DualArrayLike[dtype[__Complex128_co], complex | float | int]
104+
_ArrayLikeFloat64_co: TypeAlias = _DualArrayLike[dtype[__Float64_co], float]
105+
_ArrayLikeComplex128_co: TypeAlias = _DualArrayLike[dtype[__Complex128_co], complex]
172106

173107
# NOTE: This includes `builtins.bool`, but not `numpy.bool`.
174-
_ArrayLikeInt: TypeAlias = _DualArrayLike[
175-
dtype[integer[Any]],
176-
int,
177-
]
108+
_ArrayLikeInt: TypeAlias = _DualArrayLike[dtype[np.integer], int]
178109

179110
# Extra ArrayLike type so that pyright can deal with NDArray[Any]
180111
# Used as the first overload, should only match NDArray[Any],
@@ -185,8 +116,4 @@ def __buffer__(self, flags: int, /) -> memoryview: ...
185116
else:
186117
from typing import NoReturn as _UnknownType
187118

188-
189-
_ArrayLikeUnknown: TypeAlias = _DualArrayLike[
190-
dtype[_UnknownType],
191-
_UnknownType,
192-
]
119+
_ArrayLikeUnknown: TypeAlias = _DualArrayLike[dtype[_UnknownType], _UnknownType]

0 commit comments

Comments
 (0)