Skip to content

Commit 3a6dbaf

Browse files
committed
TYP: Replace typing.Union with | in numpy._typing
1 parent 66e1e36 commit 3a6dbaf

File tree

3 files changed

+196
-204
lines changed

3 files changed

+196
-204
lines changed

numpy/_typing/_array_like.py

Lines changed: 49 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import sys
44
from collections.abc import Collection, Callable, Sequence
5-
from typing import Any, Protocol, Union, TypeAlias, TypeVar, runtime_checkable
5+
from typing import Any, Protocol, TypeAlias, TypeVar, runtime_checkable
66

77
import numpy as np
88
from numpy import (
@@ -54,41 +54,41 @@ def __array_function__(
5454

5555

5656
# TODO: Wait until mypy supports recursive objects in combination with typevars
57-
_FiniteNestedSequence: TypeAlias = Union[
58-
_T,
59-
Sequence[_T],
60-
Sequence[Sequence[_T]],
61-
Sequence[Sequence[Sequence[_T]]],
62-
Sequence[Sequence[Sequence[Sequence[_T]]]],
63-
]
57+
_FiniteNestedSequence: TypeAlias = (
58+
_T
59+
| Sequence[_T]
60+
| Sequence[Sequence[_T]]
61+
| Sequence[Sequence[Sequence[_T]]]
62+
| Sequence[Sequence[Sequence[Sequence[_T]]]]
63+
)
6464

6565
# A subset of `npt.ArrayLike` that can be parametrized w.r.t. `np.generic`
66-
_ArrayLike: TypeAlias = Union[
67-
_SupportsArray[dtype[_ScalarType]],
68-
_NestedSequence[_SupportsArray[dtype[_ScalarType]]],
69-
]
66+
_ArrayLike: TypeAlias = (
67+
_SupportsArray[dtype[_ScalarType]]
68+
| _NestedSequence[_SupportsArray[dtype[_ScalarType]]]
69+
)
7070

7171
# A union representing array-like objects; consists of two typevars:
7272
# One representing types that can be parametrized w.r.t. `np.dtype`
7373
# and another one for the rest
74-
_DualArrayLike: TypeAlias = Union[
75-
_SupportsArray[_DType],
76-
_NestedSequence[_SupportsArray[_DType]],
77-
_T,
78-
_NestedSequence[_T],
79-
]
74+
_DualArrayLike: TypeAlias = (
75+
_SupportsArray[_DType]
76+
| _NestedSequence[_SupportsArray[_DType]]
77+
| _T
78+
| _NestedSequence[_T]
79+
)
8080

8181
if sys.version_info >= (3, 12):
8282
from collections.abc import Buffer
8383

8484
ArrayLike: TypeAlias = Buffer | _DualArrayLike[
8585
dtype[Any],
86-
Union[bool, int, float, complex, str, bytes],
86+
bool | int | float | complex | str | bytes,
8787
]
8888
else:
8989
ArrayLike: TypeAlias = _DualArrayLike[
9090
dtype[Any],
91-
Union[bool, int, float, complex, str, bytes],
91+
bool | int | float | complex | str | bytes,
9292
]
9393

9494
# `ArrayLike<X>_co`: array-like objects that can be coerced into `X`
@@ -98,47 +98,47 @@ def __array_function__(
9898
bool,
9999
]
100100
_ArrayLikeUInt_co: TypeAlias = _DualArrayLike[
101-
dtype[Union[np.bool, unsignedinteger[Any]]],
101+
dtype[np.bool] | dtype[unsignedinteger[Any]],
102102
bool,
103103
]
104104
_ArrayLikeInt_co: TypeAlias = _DualArrayLike[
105-
dtype[Union[np.bool, integer[Any]]],
106-
Union[bool, int],
105+
dtype[np.bool] | dtype[integer[Any]],
106+
bool | int,
107107
]
108108
_ArrayLikeFloat_co: TypeAlias = _DualArrayLike[
109-
dtype[Union[np.bool, integer[Any], floating[Any]]],
110-
Union[bool, int, float],
109+
dtype[np.bool] | dtype[integer[Any]] | dtype[floating[Any]],
110+
bool | int | float,
111111
]
112112
_ArrayLikeComplex_co: TypeAlias = _DualArrayLike[
113-
dtype[Union[
114-
np.bool,
115-
integer[Any],
116-
floating[Any],
117-
complexfloating[Any, Any],
118-
]],
119-
Union[bool, int, float, complex],
113+
(
114+
dtype[np.bool]
115+
| dtype[integer[Any]]
116+
| dtype[floating[Any]]
117+
| dtype[complexfloating[Any, Any]]
118+
),
119+
bool | int | float | complex,
120120
]
121121
_ArrayLikeNumber_co: TypeAlias = _DualArrayLike[
122-
dtype[Union[np.bool, number[Any]]],
123-
Union[bool, int, float, complex],
122+
dtype[np.bool] | dtype[number[Any]],
123+
bool | int | float | complex,
124124
]
125125
_ArrayLikeTD64_co: TypeAlias = _DualArrayLike[
126-
dtype[Union[np.bool, integer[Any], timedelta64]],
127-
Union[bool, int],
128-
]
129-
_ArrayLikeDT64_co: TypeAlias = Union[
130-
_SupportsArray[dtype[datetime64]],
131-
_NestedSequence[_SupportsArray[dtype[datetime64]]],
132-
]
133-
_ArrayLikeObject_co: TypeAlias = Union[
134-
_SupportsArray[dtype[object_]],
135-
_NestedSequence[_SupportsArray[dtype[object_]]],
126+
dtype[np.bool] | dtype[integer[Any]] | dtype[timedelta64],
127+
bool | int,
136128
]
129+
_ArrayLikeDT64_co: TypeAlias = (
130+
_SupportsArray[dtype[datetime64]]
131+
| _NestedSequence[_SupportsArray[dtype[datetime64]]]
132+
)
133+
_ArrayLikeObject_co: TypeAlias = (
134+
_SupportsArray[dtype[object_]]
135+
| _NestedSequence[_SupportsArray[dtype[object_]]]
136+
)
137137

138-
_ArrayLikeVoid_co: TypeAlias = Union[
139-
_SupportsArray[dtype[void]],
140-
_NestedSequence[_SupportsArray[dtype[void]]],
141-
]
138+
_ArrayLikeVoid_co: TypeAlias = (
139+
_SupportsArray[dtype[void]]
140+
| _NestedSequence[_SupportsArray[dtype[void]]]
141+
)
142142
_ArrayLikeStr_co: TypeAlias = _DualArrayLike[
143143
dtype[str_],
144144
str,
@@ -148,6 +148,7 @@ def __array_function__(
148148
bytes,
149149
]
150150

151+
# NOTE: This includes `builtins.bool`, but not `numpy.bool`.
151152
_ArrayLikeInt: TypeAlias = _DualArrayLike[
152153
dtype[integer[Any]],
153154
int,

0 commit comments

Comments
 (0)