Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 557ed6a

Browse filesBrowse files
authored
Merge pull request #18050 from BvB93/array-like2
MAINT: Add aliases for commonly used `ArrayLike` objects
2 parents e3f288e + 864848e commit 557ed6a
Copy full SHA for 557ed6a

File tree

Expand file treeCollapse file tree

2 files changed

+96
-7
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+96
-7
lines changed

‎numpy/typing/__init__.py

Copy file name to clipboardExpand all lines: numpy/typing/__init__.py
+17-1Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,25 @@ class _8Bit(_16Bit): ... # type: ignore[misc]
219219
_ScalarLike,
220220
_VoidLike,
221221
)
222-
from ._array_like import _SupportsArray, ArrayLike
223222
from ._shape import _Shape, _ShapeLike
224223
from ._dtype_like import _SupportsDType, _VoidDTypeLike, DTypeLike
224+
from ._array_like import (
225+
ArrayLike,
226+
_ArrayLike,
227+
_NestedSequence,
228+
_SupportsArray,
229+
_ArrayLikeBool,
230+
_ArrayLikeUInt,
231+
_ArrayLikeInt,
232+
_ArrayLikeFloat,
233+
_ArrayLikeComplex,
234+
_ArrayLikeTD64,
235+
_ArrayLikeDT64,
236+
_ArrayLikeObject,
237+
_ArrayLikeVoid,
238+
_ArrayLikeStr,
239+
_ArrayLikeBytes,
240+
)
225241

226242
if __doc__ is not None:
227243
from ._add_docstring import _docstrings

‎numpy/typing/_array_like.py

Copy file name to clipboardExpand all lines: numpy/typing/_array_like.py
+79-6Lines changed: 79 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,22 @@
33
import sys
44
from typing import Any, overload, Sequence, TYPE_CHECKING, Union, TypeVar
55

6-
from numpy import ndarray, dtype
7-
from ._scalars import _ScalarLike
6+
from numpy import (
7+
ndarray,
8+
dtype,
9+
generic,
10+
bool_,
11+
unsignedinteger,
12+
integer,
13+
floating,
14+
complexfloating,
15+
timedelta64,
16+
datetime64,
17+
object_,
18+
void,
19+
str_,
20+
bytes_,
21+
)
822
from ._dtype_like import DTypeLike
923

1024
if sys.version_info >= (3, 8):
@@ -18,6 +32,7 @@
1832
else:
1933
HAVE_PROTOCOL = True
2034

35+
_T = TypeVar("_T")
2136
_DType = TypeVar("_DType", bound="dtype[Any]")
2237

2338
if TYPE_CHECKING or HAVE_PROTOCOL:
@@ -30,6 +45,24 @@ def __array__(self, dtype: None = ...) -> ndarray[Any, _DType]: ...
3045
else:
3146
_SupportsArray = Any
3247

48+
# TODO: Wait for support for recursive types
49+
_NestedSequence = Union[
50+
_T,
51+
Sequence[_T],
52+
Sequence[Sequence[_T]],
53+
Sequence[Sequence[Sequence[_T]]],
54+
Sequence[Sequence[Sequence[Sequence[_T]]]],
55+
]
56+
_RecursiveSequence = Sequence[Sequence[Sequence[Sequence[Sequence[Any]]]]]
57+
58+
# A union representing array-like objects; consists of two typevars:
59+
# One representing types that can be parametrized w.r.t. `np.dtype`
60+
# and another one for the rest
61+
_ArrayLike = Union[
62+
_NestedSequence[_SupportsArray[_DType]],
63+
_NestedSequence[_T],
64+
]
65+
3366
# TODO: support buffer protocols once
3467
#
3568
# https://bugs.python.org/issue27501
@@ -38,8 +71,48 @@ def __array__(self, dtype: None = ...) -> ndarray[Any, _DType]: ...
3871
#
3972
# https://github.com/python/typing/issues/593
4073
ArrayLike = Union[
41-
_ScalarLike,
42-
Sequence[_ScalarLike],
43-
Sequence[Sequence[Any]], # TODO: Wait for support for recursive types
44-
"_SupportsArray[Any]",
74+
_RecursiveSequence,
75+
_ArrayLike[
76+
"dtype[Any]",
77+
Union[bool, int, float, complex, str, bytes]
78+
],
79+
]
80+
81+
# `ArrayLike<X>`: array-like objects that can be coerced into `X`
82+
# given the casting rules `same_kind`
83+
_ArrayLikeBool = _ArrayLike[
84+
"dtype[bool_]",
85+
bool,
86+
]
87+
_ArrayLikeUInt = _ArrayLike[
88+
"dtype[Union[bool_, unsignedinteger[Any]]]",
89+
bool,
90+
]
91+
_ArrayLikeInt = _ArrayLike[
92+
"dtype[Union[bool_, integer[Any]]]",
93+
Union[bool, int],
94+
]
95+
_ArrayLikeFloat = _ArrayLike[
96+
"dtype[Union[bool_, integer[Any], floating[Any]]]",
97+
Union[bool, int, float],
98+
]
99+
_ArrayLikeComplex = _ArrayLike[
100+
"dtype[Union[bool_, integer[Any], floating[Any], complexfloating[Any, Any]]]",
101+
Union[bool, int, float, complex],
102+
]
103+
_ArrayLikeTD64 = _ArrayLike[
104+
"dtype[Union[bool_, integer[Any], timedelta64]]",
105+
Union[bool, int],
106+
]
107+
_ArrayLikeDT64 = _NestedSequence[_SupportsArray["dtype[datetime64]"]]
108+
_ArrayLikeObject = _NestedSequence[_SupportsArray["dtype[object_]"]]
109+
110+
_ArrayLikeVoid = _NestedSequence[_SupportsArray["dtype[void]"]]
111+
_ArrayLikeStr = _ArrayLike[
112+
"dtype[str_]",
113+
str,
114+
]
115+
_ArrayLikeBytes = _ArrayLike[
116+
"dtype[bytes_]",
117+
bytes,
45118
]

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.