Skip to content

Commit 835ee4d

Browse files
authored
Merge pull request numpy#26858 from jorenham/type-alias
TYP: Annotate type aliases as ``typing.TypeAlias``
2 parents af17cc1 + 840aefe commit 835ee4d

File tree

6 files changed

+110
-103
lines changed

6 files changed

+110
-103
lines changed

numpy/__init__.pyi

Lines changed: 51 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ from typing import (
195195
Final,
196196
final,
197197
ClassVar,
198+
TypeAlias
198199
)
199200

200201
# Ensures that the stubs are picked up
@@ -641,7 +642,7 @@ def show_config() -> None: ...
641642

642643
_NdArraySubClass = TypeVar("_NdArraySubClass", bound=NDArray[Any])
643644
_DTypeScalar_co = TypeVar("_DTypeScalar_co", covariant=True, bound=generic)
644-
_ByteOrder = L["S", "<", ">", "=", "|", "L", "B", "N", "I", "little", "big", "native"]
645+
_ByteOrder: TypeAlias = L["S", "<", ">", "=", "|", "L", "B", "N", "I", "little", "big", "native"]
645646

646647
@final
647648
class dtype(Generic[_DTypeScalar_co]):
@@ -894,7 +895,7 @@ class dtype(Generic[_DTypeScalar_co]):
894895
@property
895896
def type(self) -> type[_DTypeScalar_co]: ...
896897

897-
_ArrayLikeInt = (
898+
_ArrayLikeInt: TypeAlias = (
898899
int
899900
| integer[Any]
900901
| Sequence[int | integer[Any]]
@@ -941,14 +942,14 @@ class flatiter(Generic[_NdArraySubClass]):
941942
@overload
942943
def __array__(self, dtype: _DType, /) -> ndarray[Any, _DType]: ...
943944

944-
_OrderKACF = L[None, "K", "A", "C", "F"]
945-
_OrderACF = L[None, "A", "C", "F"]
946-
_OrderCF = L[None, "C", "F"]
945+
_OrderKACF: TypeAlias = L[None, "K", "A", "C", "F"]
946+
_OrderACF: TypeAlias = L[None, "A", "C", "F"]
947+
_OrderCF: TypeAlias = L[None, "C", "F"]
947948

948-
_ModeKind = L["raise", "wrap", "clip"]
949-
_PartitionKind = L["introselect"]
950-
_SortKind = L["quicksort", "mergesort", "heapsort", "stable"]
951-
_SortSide = L["left", "right"]
949+
_ModeKind: TypeAlias = L["raise", "wrap", "clip"]
950+
_PartitionKind: TypeAlias = L["introselect"]
951+
_SortKind: TypeAlias = L["quicksort", "mergesort", "heapsort", "stable"]
952+
_SortSide: TypeAlias = L["left", "right"]
952953

953954
_ArraySelf = TypeVar("_ArraySelf", bound=_ArrayOrScalarCommon)
954955

@@ -1391,7 +1392,7 @@ _NumberType = TypeVar("_NumberType", bound=number[Any])
13911392
if sys.version_info >= (3, 12):
13921393
from collections.abc import Buffer as _SupportsBuffer
13931394
else:
1394-
_SupportsBuffer = (
1395+
_SupportsBuffer: TypeAlias = (
13951396
bytes
13961397
| bytearray
13971398
| memoryview
@@ -1404,22 +1405,22 @@ else:
14041405
_T = TypeVar("_T")
14051406
_T_co = TypeVar("_T_co", covariant=True)
14061407
_T_contra = TypeVar("_T_contra", contravariant=True)
1407-
_2Tuple = tuple[_T, _T]
1408-
_CastingKind = L["no", "equiv", "safe", "same_kind", "unsafe"]
1408+
_2Tuple: TypeAlias = tuple[_T, _T]
1409+
_CastingKind: TypeAlias = L["no", "equiv", "safe", "same_kind", "unsafe"]
14091410

1410-
_ArrayUInt_co = NDArray[np.bool | unsignedinteger[Any]]
1411-
_ArrayInt_co = NDArray[np.bool | integer[Any]]
1412-
_ArrayFloat_co = NDArray[np.bool | integer[Any] | floating[Any]]
1413-
_ArrayComplex_co = NDArray[np.bool | integer[Any] | floating[Any] | complexfloating[Any, Any]]
1414-
_ArrayNumber_co = NDArray[np.bool | number[Any]]
1415-
_ArrayTD64_co = NDArray[np.bool | integer[Any] | timedelta64]
1411+
_ArrayUInt_co: TypeAlias = NDArray[np.bool | unsignedinteger[Any]]
1412+
_ArrayInt_co: TypeAlias = NDArray[np.bool | integer[Any]]
1413+
_ArrayFloat_co: TypeAlias = NDArray[np.bool | integer[Any] | floating[Any]]
1414+
_ArrayComplex_co: TypeAlias = NDArray[np.bool | integer[Any] | floating[Any] | complexfloating[Any, Any]]
1415+
_ArrayNumber_co: TypeAlias = NDArray[np.bool | number[Any]]
1416+
_ArrayTD64_co: TypeAlias = NDArray[np.bool | integer[Any] | timedelta64]
14161417

14171418
# Introduce an alias for `dtype` to avoid naming conflicts.
1418-
_dtype = dtype
1419+
_dtype: TypeAlias = dtype
14191420

14201421
# `builtins.PyCapsule` unfortunately lacks annotations as of the moment;
14211422
# use `Any` as a stopgap measure
1422-
_PyCapsule = Any
1423+
_PyCapsule: TypeAlias = Any
14231424

14241425
class _SupportsItem(Protocol[_T_co]):
14251426
def item(self, args: Any, /) -> _T_co: ...
@@ -2837,7 +2838,7 @@ class bool(generic):
28372838
__gt__: _ComparisonOp[_NumberLike_co, _ArrayLikeNumber_co]
28382839
__ge__: _ComparisonOp[_NumberLike_co, _ArrayLikeNumber_co]
28392840

2840-
bool_ = bool
2841+
bool_: TypeAlias = bool
28412842

28422843
class object_(generic):
28432844
def __init__(self, value: object = ..., /) -> None: ...
@@ -2893,9 +2894,9 @@ class datetime64(generic):
28932894
__gt__: _ComparisonOp[datetime64, _ArrayLikeDT64_co]
28942895
__ge__: _ComparisonOp[datetime64, _ArrayLikeDT64_co]
28952896

2896-
_IntValue = SupportsInt | _CharLike_co | SupportsIndex
2897-
_FloatValue = None | _CharLike_co | SupportsFloat | SupportsIndex
2898-
_ComplexValue = (
2897+
_IntValue: TypeAlias = SupportsInt | _CharLike_co | SupportsIndex
2898+
_FloatValue: TypeAlias = None | _CharLike_co | SupportsFloat | SupportsIndex
2899+
_ComplexValue: TypeAlias = (
28992900
None
29002901
| _CharLike_co
29012902
| SupportsFloat
@@ -3049,18 +3050,18 @@ class unsignedinteger(integer[_NBit1]):
30493050
__divmod__: _UnsignedIntDivMod[_NBit1]
30503051
__rdivmod__: _UnsignedIntDivMod[_NBit1]
30513052

3052-
uint8 = unsignedinteger[_8Bit]
3053-
uint16 = unsignedinteger[_16Bit]
3054-
uint32 = unsignedinteger[_32Bit]
3055-
uint64 = unsignedinteger[_64Bit]
3053+
uint8: TypeAlias = unsignedinteger[_8Bit]
3054+
uint16: TypeAlias = unsignedinteger[_16Bit]
3055+
uint32: TypeAlias = unsignedinteger[_32Bit]
3056+
uint64: TypeAlias = unsignedinteger[_64Bit]
30563057

3057-
ubyte = unsignedinteger[_NBitByte]
3058-
ushort = unsignedinteger[_NBitShort]
3059-
uintc = unsignedinteger[_NBitIntC]
3060-
uintp = unsignedinteger[_NBitIntP]
3061-
uint = uintp
3062-
ulong = unsignedinteger[_NBitLong]
3063-
ulonglong = unsignedinteger[_NBitLongLong]
3058+
ubyte: TypeAlias = unsignedinteger[_NBitByte]
3059+
ushort: TypeAlias = unsignedinteger[_NBitShort]
3060+
uintc: TypeAlias = unsignedinteger[_NBitIntC]
3061+
uintp: TypeAlias = unsignedinteger[_NBitIntP]
3062+
uint: TypeAlias = uintp
3063+
ulong: TypeAlias = unsignedinteger[_NBitLong]
3064+
ulonglong: TypeAlias = unsignedinteger[_NBitLongLong]
30643065

30653066
class inexact(number[_NBit1]): # type: ignore
30663067
def __getnewargs__(self: inexact[_64Bit]) -> tuple[float, ...]: ...
@@ -3106,14 +3107,14 @@ class floating(inexact[_NBit1]):
31063107
__divmod__: _FloatDivMod[_NBit1]
31073108
__rdivmod__: _FloatDivMod[_NBit1]
31083109

3109-
float16 = floating[_16Bit]
3110-
float32 = floating[_32Bit]
3111-
float64 = floating[_64Bit]
3110+
float16: TypeAlias = floating[_16Bit]
3111+
float32: TypeAlias = floating[_32Bit]
3112+
float64: TypeAlias = floating[_64Bit]
31123113

3113-
half = floating[_NBitHalf]
3114-
single = floating[_NBitSingle]
3115-
double = floating[_NBitDouble]
3116-
longdouble = floating[_NBitLongDouble]
3114+
half: TypeAlias = floating[_NBitHalf]
3115+
single: TypeAlias = floating[_NBitSingle]
3116+
double: TypeAlias = floating[_NBitDouble]
3117+
longdouble: TypeAlias = floating[_NBitLongDouble]
31173118

31183119
# The main reason for `complexfloating` having two typevars is cosmetic.
31193120
# It is used to clarify why `complex128`s precision is `_64Bit`, the latter
@@ -3144,12 +3145,12 @@ class complexfloating(inexact[_NBit1], Generic[_NBit1, _NBit2]):
31443145
__pow__: _ComplexOp[_NBit1]
31453146
__rpow__: _ComplexOp[_NBit1]
31463147

3147-
complex64 = complexfloating[_32Bit, _32Bit]
3148-
complex128 = complexfloating[_64Bit, _64Bit]
3148+
complex64: TypeAlias = complexfloating[_32Bit, _32Bit]
3149+
complex128: TypeAlias = complexfloating[_64Bit, _64Bit]
31493150

3150-
csingle = complexfloating[_NBitSingle, _NBitSingle]
3151-
cdouble = complexfloating[_NBitDouble, _NBitDouble]
3152-
clongdouble = complexfloating[_NBitLongDouble, _NBitLongDouble]
3151+
csingle: TypeAlias = complexfloating[_NBitSingle, _NBitSingle]
3152+
cdouble: TypeAlias = complexfloating[_NBitDouble, _NBitDouble]
3153+
clongdouble: TypeAlias = complexfloating[_NBitLongDouble, _NBitLongDouble]
31533154

31543155
class flexible(generic): ... # type: ignore
31553156

@@ -3528,7 +3529,7 @@ class iinfo(Generic[_IntType]):
35283529
@overload
35293530
def __new__(cls, dtype: str) -> iinfo[Any]: ...
35303531

3531-
_NDIterFlagsKind = L[
3532+
_NDIterFlagsKind: TypeAlias = L[
35323533
"buffered",
35333534
"c_index",
35343535
"copy_if_overlap",
@@ -3544,7 +3545,7 @@ _NDIterFlagsKind = L[
35443545
"zerosize_ok",
35453546
]
35463547

3547-
_NDIterOpFlagsKind = L[
3548+
_NDIterOpFlagsKind: TypeAlias = L[
35483549
"aligned",
35493550
"allocate",
35503551
"arraymask",
@@ -3635,7 +3636,7 @@ class nditer:
36353636
@property
36363637
def value(self) -> tuple[NDArray[Any], ...]: ...
36373638

3638-
_MemMapModeKind = L[
3639+
_MemMapModeKind: TypeAlias = L[
36393640
"readonly", "r",
36403641
"copyonwrite", "c",
36413642
"readwrite", "r+",

numpy/_typing/_array_like.py

Lines changed: 21 additions & 21 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, TypeVar, runtime_checkable
5+
from typing import Any, Protocol, Union, TypeAlias, TypeVar, runtime_checkable
66

77
import numpy as np
88
from numpy import (
@@ -29,7 +29,7 @@
2929
_DType = TypeVar("_DType", bound=dtype[Any])
3030
_DType_co = TypeVar("_DType_co", covariant=True, bound=dtype[Any])
3131

32-
NDArray = ndarray[Any, dtype[_ScalarType_co]]
32+
NDArray: TypeAlias = ndarray[Any, dtype[_ScalarType_co]]
3333

3434
# The `_SupportsArray` protocol only cares about the default dtype
3535
# (i.e. `dtype=None` or no `dtype` parameter at all) of the to-be returned
@@ -54,7 +54,7 @@ def __array_function__(
5454

5555

5656
# TODO: Wait until mypy supports recursive objects in combination with typevars
57-
_FiniteNestedSequence = Union[
57+
_FiniteNestedSequence: TypeAlias = Union[
5858
_T,
5959
Sequence[_T],
6060
Sequence[Sequence[_T]],
@@ -63,15 +63,15 @@ def __array_function__(
6363
]
6464

6565
# A subset of `npt.ArrayLike` that can be parametrized w.r.t. `np.generic`
66-
_ArrayLike = Union[
66+
_ArrayLike: TypeAlias = Union[
6767
_SupportsArray[dtype[_ScalarType]],
6868
_NestedSequence[_SupportsArray[dtype[_ScalarType]]],
6969
]
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 = Union[
74+
_DualArrayLike: TypeAlias = Union[
7575
_SupportsArray[_DType],
7676
_NestedSequence[_SupportsArray[_DType]],
7777
_T,
@@ -81,35 +81,35 @@ def __array_function__(
8181
if sys.version_info >= (3, 12):
8282
from collections.abc import Buffer
8383

84-
ArrayLike = Buffer | _DualArrayLike[
84+
ArrayLike: TypeAlias = Buffer | _DualArrayLike[
8585
dtype[Any],
8686
Union[bool, int, float, complex, str, bytes],
8787
]
8888
else:
89-
ArrayLike = _DualArrayLike[
89+
ArrayLike: TypeAlias = _DualArrayLike[
9090
dtype[Any],
9191
Union[bool, int, float, complex, str, bytes],
9292
]
9393

9494
# `ArrayLike<X>_co`: array-like objects that can be coerced into `X`
9595
# given the casting rules `same_kind`
96-
_ArrayLikeBool_co = _DualArrayLike[
96+
_ArrayLikeBool_co: TypeAlias = _DualArrayLike[
9797
dtype[np.bool],
9898
bool,
9999
]
100-
_ArrayLikeUInt_co = _DualArrayLike[
100+
_ArrayLikeUInt_co: TypeAlias = _DualArrayLike[
101101
dtype[Union[np.bool, unsignedinteger[Any]]],
102102
bool,
103103
]
104-
_ArrayLikeInt_co = _DualArrayLike[
104+
_ArrayLikeInt_co: TypeAlias = _DualArrayLike[
105105
dtype[Union[np.bool, integer[Any]]],
106106
Union[bool, int],
107107
]
108-
_ArrayLikeFloat_co = _DualArrayLike[
108+
_ArrayLikeFloat_co: TypeAlias = _DualArrayLike[
109109
dtype[Union[np.bool, integer[Any], floating[Any]]],
110110
Union[bool, int, float],
111111
]
112-
_ArrayLikeComplex_co = _DualArrayLike[
112+
_ArrayLikeComplex_co: TypeAlias = _DualArrayLike[
113113
dtype[Union[
114114
np.bool,
115115
integer[Any],
@@ -118,37 +118,37 @@ def __array_function__(
118118
]],
119119
Union[bool, int, float, complex],
120120
]
121-
_ArrayLikeNumber_co = _DualArrayLike[
121+
_ArrayLikeNumber_co: TypeAlias = _DualArrayLike[
122122
dtype[Union[np.bool, number[Any]]],
123123
Union[bool, int, float, complex],
124124
]
125-
_ArrayLikeTD64_co = _DualArrayLike[
125+
_ArrayLikeTD64_co: TypeAlias = _DualArrayLike[
126126
dtype[Union[np.bool, integer[Any], timedelta64]],
127127
Union[bool, int],
128128
]
129-
_ArrayLikeDT64_co = Union[
129+
_ArrayLikeDT64_co: TypeAlias = Union[
130130
_SupportsArray[dtype[datetime64]],
131131
_NestedSequence[_SupportsArray[dtype[datetime64]]],
132132
]
133-
_ArrayLikeObject_co = Union[
133+
_ArrayLikeObject_co: TypeAlias = Union[
134134
_SupportsArray[dtype[object_]],
135135
_NestedSequence[_SupportsArray[dtype[object_]]],
136136
]
137137

138-
_ArrayLikeVoid_co = Union[
138+
_ArrayLikeVoid_co: TypeAlias = Union[
139139
_SupportsArray[dtype[void]],
140140
_NestedSequence[_SupportsArray[dtype[void]]],
141141
]
142-
_ArrayLikeStr_co = _DualArrayLike[
142+
_ArrayLikeStr_co: TypeAlias = _DualArrayLike[
143143
dtype[str_],
144144
str,
145145
]
146-
_ArrayLikeBytes_co = _DualArrayLike[
146+
_ArrayLikeBytes_co: TypeAlias = _DualArrayLike[
147147
dtype[bytes_],
148148
bytes,
149149
]
150150

151-
_ArrayLikeInt = _DualArrayLike[
151+
_ArrayLikeInt: TypeAlias = _DualArrayLike[
152152
dtype[integer[Any]],
153153
int,
154154
]
@@ -161,7 +161,7 @@ class _UnknownType:
161161
...
162162

163163

164-
_ArrayLikeUnknown = _DualArrayLike[
164+
_ArrayLikeUnknown: TypeAlias = _DualArrayLike[
165165
dtype[_UnknownType],
166166
_UnknownType,
167167
]

0 commit comments

Comments
 (0)