8000 TYP,MAINT: Remove leftover `Union`s in favor of the PEP 604 pipe oper… · Python-Repository-Hub/numpy@191b925 · GitHub
[go: up one dir, main page]

Skip to content

Commit 191b925

Browse files
committed
TYP,MAINT: Remove leftover Unions in favor of the PEP 604 pipe operator
1 parent 1621dff commit 191b925

File tree

12 files changed

+106
-119
lines changed

12 files changed

+106
-119
lines changed

numpy/__init__.pyi

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,16 @@ from numpy._typing import (
4747

4848
# Scalars
4949
_CharLike_co,
50-
_BoolLike_co,
5150
_IntLike_co,
5251
_FloatLike_co,
53-
_ComplexLike_co,
5452
_TD64Like_co,
5553
_NumberLike_co,
5654
_ScalarLike_co,
5755

5856
# `number` precision
5957
NBitBase,
58+
# NOTE: Do not remove the extended precision bit-types even if seemingly unused;
59+
# they're used by the mypy plugin
6060
_256Bit,
6161
_128Bit,
6262
_96Bit,
@@ -169,26 +169,22 @@ from numpy._typing._extended_precision import (
169169

170170
from collections.abc import (
171171
Callable,
172-
Container,
173172
Iterable,
174173
Iterator,
175174
Mapping,
176175
Sequence,
177-
Sized,
178176
)
179177
from typing import (
180178
Literal as L,
181179
Any,
182180
Generator,
183181
Generic,
184-
IO,
185182
NoReturn,
186183
overload,
187184
SupportsComplex,
188185
SupportsFloat,
189186
SupportsInt,
190187
TypeVar,
191-
Union,
192188
Protocol,
193189
SupportsIndex,
194190
Final,
@@ -284,7 +280,6 @@ from numpy.core._ufunc_config import (
284280
geterrcall as geterrcall,
285281
_ErrKind,
286282
_ErrFunc,
287-
_ErrDictOptional,
288283
)
289284

290285
from numpy.core.arrayprint import (
@@ -883,13 +878,13 @@ class dtype(Generic[_DTypeScalar_co]):
883878
@property
884879
def type(self) -> type[_DTypeScalar_co]: ...
885880

886-
_ArrayLikeInt = Union[
887-
int,
888-
integer[Any],
889-
Sequence[Union[int, integer[Any]]],
890-
Sequence[Sequence[Any]], # TODO: wait for support for recursive types
891-
ndarray[Any, Any]
892-
]
881+
_ArrayLikeInt = (
882+
int
883+
| integer[Any]
884+
| Sequence[int | integer[Any]]
885+
| Sequence[Sequence[Any]] # TODO: wait for support for recursive types
886+
| ndarray[Any, Any]
887+
)
893888

894889
_FlatIterSelf = TypeVar("_FlatIterSelf", bound=flatiter[Any])
895890

@@ -1397,28 +1392,28 @@ _NumberType = TypeVar("_NumberType", bound=number[Any])
13971392

13981393
# There is currently no exhaustive way to type the buffer protocol,
13991394
# as it is implemented exclusively in the C API (python/typing#593)
1400-
_SupportsBuffer = Union[
1401-
bytes,
1402-
bytearray,
1403-
memoryview,
1404-
_array.array[Any],
1405-
mmap.mmap,
1406-
NDArray[Any],
1407-
generic,
1408-
]
1395+
_SupportsBuffer = (
1396+
bytes
1397+
| bytearray
1398+
| memoryview
1399+
| _array.array[Any]
1400+
| mmap.mmap
1401+
| NDArray[Any]
1402+
| generic
1403+
)
14091404

14101405
_T = TypeVar("_T")
14111406
_T_co = TypeVar("_T_co", covariant=True)
14121407
_T_contra = TypeVar("_T_contra", contravariant=True)
14131408
_2Tuple = tuple[_T, _T]
14141409
_CastingKind = L["no", "equiv", "safe", "same_kind", "unsafe"]
14151410

1416-
_ArrayUInt_co = NDArray[Union[bool_, unsignedinteger[Any]]]
1417-
_ArrayInt_co = NDArray[Union[bool_, integer[Any]]]
1418-
_ArrayFloat_co = NDArray[Union[bool_, integer[Any], floating[Any]]]
1419-
_ArrayComplex_co = NDArray[Union[bool_, integer[Any], floating[Any], complexfloating[Any, Any]]]
1420-
_ArrayNumber_co = NDArray[Union[bool_, number[Any]]]
1421-
_ArrayTD64_co = NDArray[Union[bool_, integer[Any], timedelta64]]
1411+
_ArrayUInt_co = NDArray[bool_ | unsignedinteger[Any]]
1412+
_ArrayInt_co = NDArray[bool_ | integer[Any]]
1413+
_ArrayFloat_co = NDArray[bool_ | integer[Any] | floating[Any]]
1414+
_ArrayComplex_co = NDArray[bool_ | integer[Any] | floating[Any] | complexfloating[Any, Any]]
1415+
_ArrayNumber_co = NDArray[bool_ | number[Any]]
1416+
_ArrayTD64_co = NDArray[bool_ | integer[Any] | timedelta64]
14221417

14231418
# Introduce an alias for `dtype` to avoid naming conflicts.
14241419
_dtype = dtype
@@ -2766,16 +2761,16 @@ class datetime64(generic):
27662761
__gt__: _ComparisonOp[datetime64, _ArrayLikeDT64_co]
276 10000 72762
__ge__: _ComparisonOp[datetime64, _ArrayLikeDT64_co]
27682763

2769-
_IntValue = Union[SupportsInt, _CharLike_co, SupportsIndex]
2770-
_FloatValue = Union[None, _CharLike_co, SupportsFloat, SupportsIndex]
2771-
_ComplexValue = Union[
2772-
None,
2773-
_CharLike_co,
2774-
SupportsFloat,
2775-
SupportsComplex,
2776-
SupportsIndex,
2777-
complex, # `complex` is not a subtype of `SupportsComplex`
2778-
]
2764+
_IntValue = SupportsInt | _CharLike_co | SupportsIndex
2765+
_FloatValue = None | _CharLike_co | SupportsFloat | SupportsIndex
2766+
_ComplexValue = (
2767+
None
2768+
| _CharLike_co
2769+
| SupportsFloat
2770+
| SupportsComplex
2771+
| SupportsIndex
2772+
| complex # `complex` is not a subtype of `SupportsComplex`
2773+
)
27792774

27802775
class integer(number[_NBit1]): # type: ignore
27812776
@property

numpy/core/_asarray.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from collections.abc import Iterable
2-
from typing import Any, TypeVar, Union, overload, Literal
2+
from typing import Any, TypeVar, overload, Literal
33

44
from numpy import ndarray
55
from numpy._typing import DTypeLike, _SupportsArrayFunc
@@ -14,7 +14,7 @@ _Requirements = Literal[
1414
"O", "OWNDATA"
1515
]
1616
_E = Literal["E", "ENSUREARRAY"]
17-
_RequirementsWithE = Union[_Requirements, _E]
17+
_RequirementsWithE = _Requirements | _E
1818

1919
@overload
2020
def require(

numpy/core/einsumfunc.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from collections.abc import Sequence
2-
from typing import TypeVar, Any, overload, Union, Literal
2+
from typing import TypeVar, Any, overload, Literal
33

44
from numpy import (
55
ndarray,
@@ -26,7 +26,7 @@ from numpy._typing import (
2626

2727
_ArrayType = TypeVar(
2828
"_ArrayType",
29-
bound=ndarray[Any, dtype[Union[bool_, number[Any]]]],
29+
bound=ndarray[Any, dtype[bool_ | number[Any]]],
3030
)
3131

3232
_OptimizeKind = None | bool | Literal["greedy", "optimal"] | Sequence[Any]

numpy/core/fromnumeric.pyi

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import datetime as dt
21
from collections.abc import Sequence
3-
from typing import Union, Any, overload, TypeVar, Literal, SupportsIndex
2+
from typing import Any, overload, TypeVar, Literal, SupportsIndex
43

54
from numpy import (
6-
ndarray,
75
number,
86
uint64,
97
int_,

numpy/core/numerictypes.pyi

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import sys
22
import types
3-
from collections.abc import Iterable
43
from typing import (
54
Literal as L,
6-
Union,
75
overload,
86
Any,
97
TypeVar,
@@ -71,16 +69,16 @@ class _typedict(dict[type[generic], _T]):
7169
def __getitem__(self, key: DTypeLike) -> _T: ...
7270

7371
if sys.version_info >= (3, 10):
74-
_TypeTuple = Union[
75-
type[Any],
76-
types.UnionType,
77-
tuple[Union[type[Any], types.UnionType, tuple[Any, ...]], ...],
78-
]
72+
_TypeTuple = (
73+
type[Any]
74+
| types.UnionType
75+
| tuple[type[Any] | types.UnionType | tuple[Any, ...], ...]
76+
)
7977
else:
80-
_TypeTuple = Union[
81-
type[Any],
82-
tuple[Union[type[Any], tuple[Any, ...]], ...],
83-
]
78+
_TypeTuple = (
79+
type[Any]
80+
| tuple[type[Any] | tuple[Any, ...], ...]
81+
)
8482

8583
__all__: list[str]
8684

numpy/ctypeslib.pyi

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@
33
from ctypes import c_int64 as _c_intp
44

55
import os
6-
import sys
76
import ctypes
87
from collections.abc import Iterable, Sequence
98
from typing import (
109
Literal as L,
1110
Any,
12-
Union,
1311
TypeVar,
1412
Generic,
1513
overload,

numpy/lib/_twodim_base_impl.pyi

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ from typing import (
33
Any,
44
overload,
55
TypeVar,
6-
Union,
76
)
87

98
from numpy import (
@@ -41,7 +40,7 @@ _SCT = TypeVar("_SCT", bound=generic)
4140
# The returned arrays dtype must be compatible with `np.equal`
4241
_MaskFunc = Callable[
4342
[NDArray[int_], _T],
44-
NDArray[Union[number[Any], bool_, timedelta64, datetime64, object_]],
43+
NDArray[number[Any] | bool_ | timedelta64 | datetime64 | object_],
4544
]
4645

4746
__all__: list[str]

numpy/lib/arrayterator.pyi

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ from collections.abc import Generator
22
from typing import (
33
Any,
44
TypeVar,
5-
Union,
65
overload,
76
)
87

@@ -14,10 +13,12 @@ _Shape = TypeVar("_Shape", bound=Any)
1413
_DType = TypeVar("_DType", bound=dtype[Any])
1514
_ScalarType = TypeVar("_ScalarType", bound=generic)
1615

17-
_Index = Union[
18-
Union[ellipsis, int, slice],
19-
tuple[Union[ellipsis, int, slice], ...],
20-
]
16+
_Index = (
17+
ellipsis
18+
| int
19+
| slice
20+
| tuple[ellipsis | int | slice, ...]
21+
)
2122

2223
__all__: list[str]
2324

numpy/random/_generator.pyi

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from collections.abc import Callable
2-
from typing import Any, Union, overload, TypeVar, Literal
2+
from typing import Any, overload, TypeVar, Literal
33

44
from numpy import (
55
bool_,
@@ -47,22 +47,22 @@ from numpy._typing import (
4747

4848
_ArrayType = TypeVar("_ArrayType", bound=ndarray[Any, Any])
4949

50-
_DTypeLikeFloat32 = Union[
51-
dtype[float32],
52-
_SupportsDType[dtype[float32]],
53-
type[float32],
54-
_Float32Codes,
55-
_SingleCodes,
56-
]
50+
_DTypeLikeFloat32 = (
51+
dtype[float32]
52+
| _SupportsDType[dtype[float32]]
53+
| type[float32]
54+
| _Float32Codes
55+
| _SingleCodes
56+
)
5757

58-
_DTypeLikeFloat64 = Union[
59-
dtype[float64],
60-
_SupportsDType[dtype[float64]],
61-
type[float],
62-
type[float64],
63-
_Float64Codes,
64-
_DoubleCodes,
65-
]
58+
_DTypeLikeFloat64 = (
59+
dtype[float64]
60+
| _SupportsDType[dtype[float64]]
61+
| type[float]
62+
| type[float64]
63+
| _Float64Codes
64+
| _DoubleCodes
65+
)
6666

6767
class Generator:
6868
def __init__(self, bit_generator: BitGenerator) -> None: ...

numpy/random/bit_generator.pyi

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ from typing import (
66
NamedTuple,
77
TypedDict,
88
TypeVar,
9-
Union,
109
overload,
1110
Literal,
1211
)
@@ -16,18 +15,18 @@ from numpy._typing import _ArrayLikeInt_co, _ShapeLike, _SupportsDType, _UInt32C
1615

1716
_T = TypeVar("_T")
1817

19-
_DTypeLikeUint32 = Union[
20-
dtype[uint32],
21-
_SupportsDType[dtype[uint32]],
22-
type[uint32],
23-
_UInt32Codes,
24-
]
25-
_DTypeLikeUint64 = Union[
26-
dtype[uint64],
27-
_SupportsDType[dtype[uint64]],
28-
type[uint64],
29-
_UInt64Codes,
30-
]
18+
_DTypeLikeUint32 = (
19+
dtype[uint32]
20+
| _SupportsDType[dtype[uint32]]
21+
| type[uint32]
22+
| _UInt32Codes
23+
)
24+
_DTypeLikeUint64 = (
25+
dtype[uint64]
26+
| _SupportsDType[dtype[uint64]]
27+
| type[uint64]
28+
| _UInt64Codes
29+
)
3130

3231
class _SeedSeqState(TypedDict):
3332
entropy: None | int | Sequence[int]

0 commit comments

Comments
 (0)
0