@@ -6,7 +6,7 @@ import decimal
66import fractions
77from collections .abc import Sequence
88from typing import Any , TypeAlias , type_check_only
9- from typing_extensions import Never , Protocol , TypeAliasType , TypeVar , Unpack
9+ from typing_extensions import Never , Protocol , TypeAliasType , TypeVar
1010
1111import numpy as np
1212from numpy ._typing import _NestedSequence
@@ -101,13 +101,25 @@ from ._scalar_co import (
101101 co_uint64 as co_uint64 ,
102102 co_ulong as co_ulong ,
103103)
104+ from ._shape import (
105+ Shape as Shape ,
106+ Shape0 as Shape0 ,
107+ Shape1 as Shape1 ,
108+ Shape1_ as Shape1_ ,
109+ Shape2 as Shape2 ,
110+ Shape2_ as Shape2_ ,
111+ Shape3 as Shape3 ,
112+ Shape3_ as Shape3_ ,
113+ Shape4 as Shape4 ,
114+ Shape4_ as Shape4_ ,
115+ )
104116
105117###
106118# Type parameters
107119
108120_T = TypeVar ("_T" )
109- _ShapeT = TypeVar ("_ShapeT" , bound = tuple [ int , ...], default = tuple [ int , ...] )
110- _ShapeT_co = TypeVar ("_ShapeT_co" , bound = tuple [ int , ...] , covariant = True )
121+ _ShapeT = TypeVar ("_ShapeT" , bound = Shape , default = Shape )
122+ _ShapeT_co = TypeVar ("_ShapeT_co" , bound = Shape , covariant = True )
111123_ScalarT = TypeVar ("_ScalarT" , bound = np .generic )
112124_ScalarT_co = TypeVar ("_ScalarT_co" , bound = np .generic , covariant = True )
113125_ScalarT0 = TypeVar ("_ScalarT0" , bound = np .generic , default = Any )
@@ -121,28 +133,28 @@ _ToT = TypeVar("_ToT")
121133
122134@type_check_only
123135class CanArray0D (Protocol [_ScalarT_co ]):
124- def __array__ (self , / ) -> np .ndarray [tuple [()] , np .dtype [_ScalarT_co ]]: ...
136+ def __array__ (self , / ) -> np .ndarray [Shape0 , np .dtype [_ScalarT_co ]]: ...
125137
126138@type_check_only
127139class CanArray1D (Protocol [_ScalarT_co ]):
128- def __array__ (self , / ) -> np .ndarray [tuple [ int ] , np .dtype [_ScalarT_co ]]: ...
140+ def __array__ (self , / ) -> np .ndarray [Shape1 , np .dtype [_ScalarT_co ]]: ...
129141
130142@type_check_only
131143class CanArray2D (Protocol [_ScalarT_co ]):
132- def __array__ (self , / ) -> np .ndarray [tuple [ int , int ] , np .dtype [_ScalarT_co ]]: ...
144+ def __array__ (self , / ) -> np .ndarray [Shape2 , np .dtype [_ScalarT_co ]]: ...
133145
134146@type_check_only
135147class CanArray3D (Protocol [_ScalarT_co ]):
136- def __array__ (self , / ) -> np .ndarray [tuple [ int , int , int ] , np .dtype [_ScalarT_co ]]: ...
148+ def __array__ (self , / ) -> np .ndarray [Shape3 , np .dtype [_ScalarT_co ]]: ...
137149
138150@type_check_only
139151class CanArrayND (Protocol [_ScalarT_co ]):
140- def __array__ (self , / ) -> np .ndarray [tuple [ int , ...] , np .dtype [_ScalarT_co ]]: ...
152+ def __array__ (self , / ) -> np .ndarray [Shape , np .dtype [_ScalarT_co ]]: ...
141153
142154@type_check_only
143155class CanLenArrayND (Protocol [_ScalarT_co ]):
144156 def __len__ (self , / ) -> int : ...
145- def __array__ (self , / ) -> np .ndarray [tuple [ int , ...] , np .dtype [_ScalarT_co ]]: ...
157+ def __array__ (self , / ) -> np .ndarray [Shape , np .dtype [_ScalarT_co ]]: ...
146158
147159@type_check_only
148160class CanLenArray (Protocol [_ScalarT_co , _ShapeT_co ]):
@@ -153,15 +165,6 @@ class CanLenArray(Protocol[_ScalarT_co, _ShapeT_co]):
153165class _CanStringArray (Protocol [_ShapeT_co , _NaT_co ]):
154166 def __array__ (self , / ) -> np .ndarray [_ShapeT_co , np .dtypes .StringDType [_NaT_co ]]: ...
155167
156- ###
157- # Shape aliases
158-
159- AtLeast0D : TypeAlias = tuple [int , ...]
160- AtLeast1D = TypeAliasType ("AtLeast1D" , tuple [int , Unpack [tuple [int , ...]]])
161- AtLeast2D = TypeAliasType ("AtLeast2D" , tuple [int , int , Unpack [tuple [int , ...]]])
162- AtLeast3D = TypeAliasType ("AtLeast3D" , tuple [int , int , int , Unpack [tuple [int , ...]]])
163- AtLeast4D = TypeAliasType ("AtLeast4D" , tuple [int , int , int , int , Unpack [tuple [int , ...]]])
164-
165168###
166169# Shape-typed sequences
167170
@@ -179,23 +182,19 @@ Sequence3ND: TypeAlias = Sequence[Sequence[_NestedSequence[_T]]]
179182# Shape-typed array aliases
180183
181184Array = TypeAliasType ("Array" , np .ndarray [_ShapeT , np .dtype [_ScalarT0 ]], type_params = (_ScalarT0 , _ShapeT ))
182- Array0D = TypeAliasType ("Array0D" , np .ndarray [tuple [()] , np .dtype [_ScalarT0 ]], type_params = (_ScalarT0 ,))
183- Array1D = TypeAliasType ("Array1D" , np .ndarray [tuple [ int ] , np .dtype [_ScalarT0 ]], type_params = (_ScalarT0 ,))
184- Array2D = TypeAliasType ("Array2D" , np .ndarray [tuple [ int , int ] , np .dtype [_ScalarT0 ]], type_params = (_ScalarT0 ,))
185- Array3D = TypeAliasType ("Array3D" , np .ndarray [tuple [ int , int , int ] , np .dtype [_ScalarT0 ]], type_params = (_ScalarT0 ,))
186- Array4D = TypeAliasType ("Array4D" , np .ndarray [tuple [ int , int , int , int ] , np .dtype [_ScalarT0 ]], type_params = (_ScalarT0 ,))
185+ Array0D = TypeAliasType ("Array0D" , np .ndarray [Shape0 , np .dtype [_ScalarT0 ]], type_params = (_ScalarT0 ,))
186+ Array1D = TypeAliasType ("Array1D" , np .ndarray [Shape1 , np .dtype [_ScalarT0 ]], type_params = (_ScalarT0 ,))
187+ Array2D = TypeAliasType ("Array2D" , np .ndarray [Shape2 , np .dtype [_ScalarT0 ]], type_params = (_ScalarT0 ,))
188+ Array3D = TypeAliasType ("Array3D" , np .ndarray [Shape3 , np .dtype [_ScalarT0 ]], type_params = (_ScalarT0 ,))
189+ Array4D = TypeAliasType ("Array4D" , np .ndarray [Shape4 , np .dtype [_ScalarT0 ]], type_params = (_ScalarT0 ,))
187190
188191MArray = TypeAliasType ("MArray" , np .ma .MaskedArray [_ShapeT , np .dtype [_ScalarT0 ]], type_params = (_ScalarT0 , _ShapeT ))
189- MArray0D = TypeAliasType ("MArray0D" , np .ma .MaskedArray [tuple [()], np .dtype [_ScalarT0 ]], type_params = (_ScalarT0 ,))
190- MArray1D = TypeAliasType ("MArray1D" , np .ma .MaskedArray [tuple [int ], np .dtype [_ScalarT0 ]], type_params = (_ScalarT0 ,))
191- MArray2D = TypeAliasType ("MArray2D" , np .ma .MaskedArray [tuple [int , int ], np .dtype [_ScalarT0 ]], type_params = (_ScalarT0 ,))
192- MArray3D = TypeAliasType (
193- "MArray3D" ,
194- np .ma .MaskedArray [tuple [int , int , int ], np .dtype [_ScalarT0 ]],
195- type_params = (_ScalarT0 ,),
196- )
192+ MArray0D = TypeAliasType ("MArray0D" , np .ma .MaskedArray [Shape0 , np .dtype [_ScalarT0 ]], type_params = (_ScalarT0 ,))
193+ MArray1D = TypeAliasType ("MArray1D" , np .ma .MaskedArray [Shape1 , np .dtype [_ScalarT0 ]], type_params = (_ScalarT0 ,))
194+ MArray2D = TypeAliasType ("MArray2D" , np .ma .MaskedArray [Shape2 , np .dtype [_ScalarT0 ]], type_params = (_ScalarT0 ,))
195+ MArray3D = TypeAliasType ("MArray3D" , np .ma .MaskedArray [Shape3 , np .dtype [_ScalarT0 ]], type_params = (_ScalarT0 ,))
197196
198- Matrix = TypeAliasType ("Matrix" , np .matrix [tuple [ int , int ] , np .dtype [_ScalarT0 ]], type_params = (_ScalarT0 ,))
197+ Matrix = TypeAliasType ("Matrix" , np .matrix [Shape2 , np .dtype [_ScalarT0 ]], type_params = (_ScalarT0 ,))
199198
200199StringArray = TypeAliasType (
201200 "StringArray" ,
@@ -204,27 +203,27 @@ StringArray = TypeAliasType(
204203)
205204StringArray0D = TypeAliasType (
206205 "StringArray0D" ,
207- np .ndarray [tuple [()] , np .dtypes .StringDType [_NaT ]],
206+ np .ndarray [Shape0 , np .dtypes .StringDType [_NaT ]],
208207 type_params = (_NaT ,),
209208)
210209StringArray1D = TypeAliasType (
211210 "StringArray1D" ,
212- np .ndarray [tuple [ int ] , np .dtypes .StringDType [_NaT ]],
211+ np .ndarray [Shape1 , np .dtypes .StringDType [_NaT ]],
213212 type_params = (_NaT ,),
214213)
215214StringArray2D = TypeAliasType (
216215 "StringArray2D" ,
217- np .ndarray [tuple [ int , int ] , np .dtypes .StringDType [_NaT ]],
216+ np .ndarray [Shape2 , np .dtypes .StringDType [_NaT ]],
218217 type_params = (_NaT ,),
219218)
220219StringArray3D = TypeAliasType (
221220 "StringArray3D" ,
222- np .ndarray [tuple [ int , int , int ] , np .dtypes .StringDType [_NaT ]],
221+ np .ndarray [Shape3 , np .dtypes .StringDType [_NaT ]],
223222 type_params = (_NaT ,),
224223)
225224StringArrayND = TypeAliasType (
226225 "StringArrayND" ,
227- np .ndarray [tuple [ int , ...] , np .dtypes .StringDType [_NaT ]],
226+ np .ndarray [Shape , np .dtypes .StringDType [_NaT ]],
228227 type_params = (_NaT ,),
229228)
230229
@@ -262,10 +261,10 @@ _ToArray2_3ds: TypeAlias = CanArray3D[_ScalarT] | Sequence[_ToArray2_2ds[_Scalar
262261# requires a lower bound on dimensionality, e.g. `_2nd` denotes `ndin` within `[2, n]`
263262_ToArray_1nd : TypeAlias = CanLenArrayND [_ScalarT ] | Sequence1ND [CanArrayND [_ScalarT ]]
264263_ToArray2_1nd : TypeAlias = CanLenArrayND [_ScalarT ] | Sequence1ND [_ToT | CanArrayND [_ScalarT ]]
265- _ToArray_2nd : TypeAlias = CanLenArray [_ScalarT , AtLeast2D ] | Sequence [_ToArray_1nd [_ScalarT ]]
266- _ToArray2_2nd : TypeAlias = CanLenArray [_ScalarT , AtLeast2D ] | Sequence [_ToArray2_1nd [_ScalarT , _ToT ]]
267- _ToArray_3nd : TypeAlias = CanLenArray [_ScalarT , AtLeast3D ] | Sequence [_ToArray_2nd [_ScalarT ]]
268- _ToArray2_3nd : TypeAlias = CanLenArray [_ScalarT , AtLeast3D ] | Sequence [_ToArray2_2nd [_ScalarT , _ToT ]]
264+ _ToArray_2nd : TypeAlias = CanLenArray [_ScalarT , Shape2_ ] | Sequence [_ToArray_1nd [_ScalarT ]]
265+ _ToArray2_2nd : TypeAlias = CanLenArray [_ScalarT , Shape2_ ] | Sequence [_ToArray2_1nd [_ScalarT , _ToT ]]
266+ _ToArray_3nd : TypeAlias = CanLenArray [_ScalarT , Shape3_ ] | Sequence [_ToArray_2nd [_ScalarT ]]
267+ _ToArray2_3nd : TypeAlias = CanLenArray [_ScalarT , Shape3_ ] | Sequence [_ToArray2_2nd [_ScalarT , _ToT ]]
269268
270269###
271270# Non-overlapping scalar- and array-like aliases for all scalar types.
@@ -685,13 +684,13 @@ ToObject_2nd = TypeAliasType("ToObject_2nd", _ToArray2_2nd[np.object_, _PyObject
685684ToObject_3nd = TypeAliasType ("ToObject_3nd" , _ToArray2_3nd [np .object_ , _PyObject ])
686685
687686# StringDType
688- ToString_nd = TypeAliasType ("ToString_nd" , _CanStringArray [AtLeast0D , _NaT0 ], type_params = (_NaT0 ,))
689- ToString_1ds = TypeAliasType ("ToString_1ds" , _CanStringArray [tuple [ int ] , _NaT0 ], type_params = (_NaT0 ,))
690- ToString_2ds = TypeAliasType ("ToString_2ds" , _CanStringArray [tuple [ int , int ] , _NaT0 ], type_params = (_NaT0 ,))
691- ToString_3ds = TypeAliasType ("ToString_3ds" , _CanStringArray [tuple [ int , int , int ] , _NaT0 ], type_params = (_NaT0 ,))
692- ToString_1nd = TypeAliasType ("ToString_1nd" , _CanStringArray [AtLeast1D , _NaT0 ], type_params = (_NaT0 ,))
693- ToString_2nd = TypeAliasType ("ToString_2nd" , _CanStringArray [AtLeast2D , _NaT0 ], type_params = (_NaT0 ,))
694- ToString_3nd = TypeAliasType ("ToString_3nd" , _CanStringArray [AtLeast3D , _NaT0 ], type_params = (_NaT0 ,))
687+ ToString_nd = TypeAliasType ("ToString_nd" , _CanStringArray [Shape , _NaT0 ], type_params = (_NaT0 ,))
688+ ToString_1ds = TypeAliasType ("ToString_1ds" , _CanStringArray [Shape1 , _NaT0 ], type_params = (_NaT0 ,))
689+ ToString_2ds = TypeAliasType ("ToString_2ds" , _CanStringArray [Shape2 , _NaT0 ], type_params = (_NaT0 ,))
690+ ToString_3ds = TypeAliasType ("ToString_3ds" , _CanStringArray [Shape3 , _NaT0 ], type_params = (_NaT0 ,))
691+ ToString_1nd = TypeAliasType ("ToString_1nd" , _CanStringArray [Shape1_ , _NaT0 ], type_params = (_NaT0 ,))
692+ ToString_2nd = TypeAliasType ("ToString_2nd" , _CanStringArray [Shape2_ , _NaT0 ], type_params = (_NaT0 ,))
693+ ToString_3nd = TypeAliasType ("ToString_3nd" , _CanStringArray [Shape3_ , _NaT0 ], type_params = (_NaT0 ,))
695694
696695# any scalar
697696ToGeneric_nd = TypeAliasType ("ToGeneric_nd" , _ToArray2_nd [np .generic , _PyScalar ])
@@ -976,8 +975,3 @@ CoStr_3ds = TypeAliasType("CoStr_3ds", _ToArray2_3ds[_ToCharacter, _PyCharacter]
976975CoStr_1nd = TypeAliasType ("CoStr_1nd" , _ToArray2_1nd [_ToCharacter , _PyCharacter ])
977976CoStr_2nd = TypeAliasType ("CoStr_2nd" , _ToArray2_2nd [_ToCharacter , _PyCharacter ])
978977CoStr_3nd = TypeAliasType ("CoStr_3nd" , _ToArray2_3nd [_ToCharacter , _PyCharacter ])
979-
980- ###
981- # DType-likes
982- # TODO(jorenham): see
983- # https://github.com/numpy/numtype/issues/304
0 commit comments