|
| 1 | +# pyright: reportIncompatibleMethodOverride=false |
| 2 | + |
1 | 3 | from collections.abc import Generator
|
2 | 4 | from types import EllipsisType
|
3 |
| -from typing import ( |
4 |
| - Any, |
5 |
| - TypeAlias, |
6 |
| - TypeVar, |
7 |
| - overload, |
8 |
| -) |
| 5 | +from typing import Any, Final, TypeAlias, overload |
| 6 | + |
| 7 | +from typing_extensions import TypeVar |
9 | 8 |
|
10 |
| -from numpy import ndarray, dtype, generic |
11 |
| -from numpy._typing import DTypeLike, NDArray, _Shape as _AnyShape |
| 9 | +import numpy as np |
12 | 10 |
|
13 | 11 | __all__ = ["Arrayterator"]
|
14 | 12 |
|
15 |
| -# TODO: Rename to ``_ShapeType`` |
16 |
| -_Shape = TypeVar("_Shape", bound=_AnyShape) |
17 |
| -_DType = TypeVar("_DType", bound=dtype[Any]) |
18 |
| -_ScalarType = TypeVar("_ScalarType", bound=generic) |
| 13 | +_ShapeT_co = TypeVar("_ShapeT_co", bound=tuple[int, ...], covariant=True) |
| 14 | +_DTypeT = TypeVar("_DTypeT", bound=np.dtype[Any]) |
| 15 | +_DTypeT_co = TypeVar("_DTypeT_co", bound=np.dtype[Any], covariant=True) |
| 16 | +_ScalarT = TypeVar("_ScalarT", bound=np.generic) |
19 | 17 |
|
20 |
| -_Index: TypeAlias = ( |
21 |
| - EllipsisType |
22 |
| - | int |
23 |
| - | slice |
24 |
| - | tuple[EllipsisType | int | slice, ...] |
25 |
| -) |
| 18 | +_AnyIndex: TypeAlias = EllipsisType | int | slice | tuple[EllipsisType | int | slice, ...] |
26 | 19 |
|
27 | 20 | # NOTE: In reality `Arrayterator` does not actually inherit from `ndarray`,
|
28 | 21 | # but its ``__getattr__` method does wrap around the former and thus has
|
29 | 22 | # access to all its methods
|
30 | 23 |
|
31 |
| -class Arrayterator(ndarray[_Shape, _DType]): |
32 |
| - var: ndarray[_Shape, _DType] # type: ignore[assignment] |
33 |
| - buf_size: None | int |
34 |
| - start: list[int] |
35 |
| - stop: list[int] |
36 |
| - step: list[int] |
| 24 | +class Arrayterator(np.ndarray[_ShapeT_co, _DTypeT_co]): |
| 25 | + var: np.ndarray[_ShapeT_co, _DTypeT_co] # type: ignore[assignment] |
| 26 | + buf_size: Final[int | None] |
| 27 | + start: Final[list[int]] |
| 28 | + stop: Final[list[int]] |
| 29 | + step: Final[list[int]] |
37 | 30 |
|
38 | 31 | @property # type: ignore[misc]
|
39 |
| - def shape(self) -> tuple[int, ...]: ... |
| 32 | + def shape(self) -> _ShapeT_co: ... |
40 | 33 | @property
|
41 |
| - def flat(self: NDArray[_ScalarType]) -> Generator[_ScalarType, None, None]: ... |
42 |
| - def __init__( |
43 |
| - self, var: ndarray[_Shape, _DType], buf_size: None | int = ... |
44 |
| - ) -> None: ... |
45 |
| - @overload |
46 |
| - def __array__(self, dtype: None = ..., copy: None | bool = ...) -> ndarray[_AnyShape, _DType]: ... |
| 34 | + def flat(self: Arrayterator[Any, np.dtype[_ScalarT]]) -> Generator[_ScalarT]: ... # type: ignore[override] |
| 35 | + |
| 36 | + # |
| 37 | + def __init__(self, /, var: np.ndarray[_ShapeT_co, _DTypeT_co], buf_size: int | None = None) -> None: ... |
| 38 | + def __getitem__(self, index: _AnyIndex, /) -> Arrayterator[tuple[int, ...], _DTypeT_co]: ... # type: ignore[override] |
| 39 | + def __iter__(self) -> Generator[np.ndarray[tuple[int, ...], _DTypeT_co]]: ... |
| 40 | + |
| 41 | + # |
| 42 | + @overload # type: ignore[override] |
| 43 | + def __array__(self, /, dtype: None = None, copy: bool | None = None) -> np.ndarray[_ShapeT_co, _DTypeT_co]: ... |
47 | 44 | @overload
|
48 |
| - def __array__(self, dtype: DTypeLike, copy: None | bool = ...) -> NDArray[Any]: ... |
49 |
| - def __getitem__(self, index: _Index) -> Arrayterator[_AnyShape, _DType]: ... |
50 |
| - def __iter__(self) -> Generator[ndarray[_AnyShape, _DType], None, None]: ... |
| 45 | + def __array__(self, /, dtype: _DTypeT, copy: bool | None = None) -> np.ndarray[_ShapeT_co, _DTypeT]: ... |
0 commit comments