11
11
)
12
12
13
13
import numpy as np
14
+ import numpy .typing as npt
14
15
15
16
if TYPE_CHECKING :
16
17
from typing_extensions import Self
20
21
21
22
# TODO: create a protocol for the attributes we need, for now we alias Numpy's ndarray
22
23
# both for the array-like and ndarray-like
23
- ArrayLike : TypeAlias = np . ndarray
24
- NDArrayLike : TypeAlias = np . ndarray
24
+ ArrayLike : TypeAlias = npt . NDArray [ Any ]
25
+ NDArrayLike : TypeAlias = npt . NDArray [ Any ]
25
26
26
27
27
28
def check_item_key_is_1d_contiguous (key : Any ) -> None :
@@ -40,7 +41,7 @@ def __call__(
40
41
self ,
41
42
* ,
42
43
shape : Iterable [int ],
43
- dtype : np .DTypeLike ,
44
+ dtype : npt .DTypeLike ,
44
45
order : Literal ["C" , "F" ],
45
46
fill_value : Any | None ,
46
47
) -> NDBuffer :
@@ -163,7 +164,7 @@ def as_array_like(self) -> NDArrayLike:
163
164
"""
164
165
return self ._data
165
166
166
- def as_nd_buffer (self , * , dtype : np .DTypeLike ) -> NDBuffer :
167
+ def as_nd_buffer (self , * , dtype : npt .DTypeLike ) -> NDBuffer :
167
168
"""Create a new NDBuffer from this one.
168
169
169
170
This will never copy data.
@@ -179,7 +180,7 @@ def as_nd_buffer(self, *, dtype: np.DTypeLike) -> NDBuffer:
179
180
"""
180
181
return NDBuffer .from_ndarray_like (self ._data .view (dtype = dtype ))
181
182
182
- def as_numpy_array (self ) -> np . ndarray :
183
+ def as_numpy_array (self ) -> npt . NDArray [ Any ] :
183
184
"""Return the buffer as a NumPy array (host memory).
184
185
185
186
Warning
@@ -271,7 +272,7 @@ def create(
271
272
cls ,
272
273
* ,
273
274
shape : Iterable [int ],
274
- dtype : np .DTypeLike ,
275
+ dtype : npt .DTypeLike ,
275
276
order : Literal ["C" , "F" ] = "C" ,
276
277
fill_value : Any | None = None ,
277
278
) -> Self :
@@ -298,7 +299,7 @@ def create(
298
299
A subclass can overwrite this method to create a ndarray-like object
299
300
other then the default Numpy array.
300
301
"""
301
- ret = cls (np .empty (shape = shape , dtype = dtype , order = order ))
302
+ ret = cls (np .empty (shape = tuple ( shape ) , dtype = dtype , order = order ))
302
303
if fill_value is not None :
303
304
ret .fill (fill_value )
304
305
return ret
@@ -319,7 +320,7 @@ def from_ndarray_like(cls, ndarray_like: NDArrayLike) -> Self:
319
320
return cls (ndarray_like )
320
321
321
322
@classmethod
322
- def from_numpy_array (cls , array_like : np .ArrayLike ) -> Self :
323
+ def from_numpy_array (cls , array_like : npt .ArrayLike ) -> Self :
323
324
"""Create a new buffer of Numpy array-like object
324
325
325
326
Parameters
@@ -360,7 +361,7 @@ def as_buffer(self) -> Buffer:
360
361
data = np .ascontiguousarray (self ._data )
361
362
return Buffer (data .reshape (- 1 ).view (dtype = "b" )) # Flatten the array without copy
362
363
363
- def as_numpy_array (self ) -> np . ndarray :
364
+ def as_numpy_array (self ) -> npt . NDArray [ Any ] :
364
365
"""Return the buffer as a NumPy array (host memory).
365
366
366
367
Warning
@@ -393,9 +394,9 @@ def byteorder(self) -> Endian:
393
394
return Endian (sys .byteorder )
394
395
395
396
def reshape (self , newshape : Iterable [int ]) -> Self :
396
- return self .__class__ (self ._data .reshape (newshape ))
397
+ return self .__class__ (self ._data .reshape (tuple ( newshape ) ))
397
398
398
- def astype (self , dtype : np .DTypeLike , order : Literal ["K" , "A" , "C" , "F" ] = "K" ) -> Self :
399
+ def astype (self , dtype : npt .DTypeLike , order : Literal ["K" , "A" , "C" , "F" ] = "K" ) -> Self :
399
400
return self .__class__ (self ._data .astype (dtype = dtype , order = order ))
400
401
401
402
def __getitem__ (self , key : Any ) -> Self :
@@ -418,11 +419,11 @@ def fill(self, value: Any) -> None:
418
419
def copy (self ) -> Self :
419
420
return self .__class__ (self ._data .copy ())
420
421
421
- def transpose (self , * axes : np .SupportsIndex ) -> Self :
422
+ def transpose (self , * axes : np .SupportsIndex ) -> Self : # type: ignore[name-defined]
422
423
return self .__class__ (self ._data .transpose (* axes ))
423
424
424
425
425
- def as_numpy_array_wrapper (func : Callable [[np . ndarray ], bytes ], buf : Buffer ) -> Buffer :
426
+ def as_numpy_array_wrapper (func : Callable [[npt . NDArray [ Any ] ], bytes ], buf : Buffer ) -> Buffer :
426
427
"""Converts the input of `func` to a numpy array and the output back to `Buffer`.
427
428
428
429
This function is useful when calling a `func` that only support host memory such
0 commit comments