Skip to content

Commit c3d6548

Browse files
authored
Merge pull request numpy#28506 from jorenham/numtype/165
TYP: fix typing errors in ``numpy.lib._arrayterator_impl``
2 parents 5b663bd + 35a6c4a commit c3d6548

File tree

1 file changed

+29
-34
lines changed

1 file changed

+29
-34
lines changed

numpy/lib/_arrayterator_impl.pyi

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,45 @@
1+
# pyright: reportIncompatibleMethodOverride=false
2+
13
from collections.abc import Generator
24
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
98

10-
from numpy import ndarray, dtype, generic
11-
from numpy._typing import DTypeLike, NDArray, _Shape as _AnyShape
9+
import numpy as np
1210

1311
__all__ = ["Arrayterator"]
1412

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)
1917

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, ...]
2619

2720
# NOTE: In reality `Arrayterator` does not actually inherit from `ndarray`,
2821
# but its ``__getattr__` method does wrap around the former and thus has
2922
# access to all its methods
3023

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]]
3730

3831
@property # type: ignore[misc]
39-
def shape(self) -> tuple[int, ...]: ...
32+
def shape(self) -> _ShapeT_co: ...
4033
@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]: ...
4744
@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

Comments
 (0)