2
2
3
3
import sys
4
4
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
6
6
7
7
import numpy as np
8
8
from numpy import (
@@ -54,41 +54,41 @@ def __array_function__(
54
54
55
55
56
56
# 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
+ )
64
64
65
65
# 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
+ )
70
70
71
71
# A union representing array-like objects; consists of two typevars:
72
72
# One representing types that can be parametrized w.r.t. `np.dtype`
73
73
# 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
+ )
80
80
81
81
if sys .version_info >= (3 , 12 ):
82
82
from collections .abc import Buffer
83
83
84
84
ArrayLike : TypeAlias = Buffer | _DualArrayLike [
85
85
dtype [Any ],
86
- Union [ bool , int , float , complex , str , bytes ] ,
86
+ bool | int | float | complex | str | bytes ,
87
87
]
88
88
else :
89
89
ArrayLike : TypeAlias = _DualArrayLike [
90
90
dtype [Any ],
91
- Union [ bool , int , float , complex , str , bytes ] ,
91
+ bool | int | float | complex | str | bytes ,
92
92
]
93
93
94
94
# `ArrayLike<X>_co`: array-like objects that can be coerced into `X`
@@ -98,47 +98,47 @@ def __array_function__(
98
98
bool ,
99
99
]
100
100
_ArrayLikeUInt_co : TypeAlias = _DualArrayLike [
101
- dtype [Union [ np .bool , unsignedinteger [Any ] ]],
101
+ dtype [np .bool ] | dtype [ unsignedinteger [Any ]],
102
102
bool ,
103
103
]
104
104
_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 ,
107
107
]
108
108
_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 ,
111
111
]
112
112
_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 ,
120
120
]
121
121
_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 ,
124
124
]
125
125
_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 ,
136
128
]
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
+ )
137
137
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
+ )
142
142
_ArrayLikeStr_co : TypeAlias = _DualArrayLike [
143
143
dtype [str_ ],
144
144
str ,
@@ -148,6 +148,7 @@ def __array_function__(
148
148
bytes ,
149
149
]
150
150
151
+ # NOTE: This includes `builtins.bool`, but not `numpy.bool`.
151
152
_ArrayLikeInt : TypeAlias = _DualArrayLike [
152
153
dtype [integer [Any ]],
153
154
int ,
0 commit comments