Skip to content

Commit b748783

Browse files
committed
TYP: Align arrayproxy.ArrayLike to satisfy np.ndarray
1 parent 3686e03 commit b748783

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

nibabel/arrayproxy.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@
5959
if ty.TYPE_CHECKING: # pragma: no cover
6060
import numpy.typing as npt
6161

62+
# Taken from numpy/__init__.pyi
63+
_DType = ty.TypeVar('_DType', bound=np.dtype[ty.Any])
64+
6265

6366
class ArrayLike(ty.Protocol):
6467
"""Protocol for numpy ndarray-like objects
@@ -68,9 +71,19 @@ class ArrayLike(ty.Protocol):
6871
"""
6972

7073
shape: tuple[int, ...]
71-
ndim: int
7274

73-
def __array__(self, dtype: npt.DTypeLike | None = None, /) -> npt.NDArray:
75+
@property
76+
def ndim(self) -> int:
77+
... # pragma: no cover
78+
79+
# If no dtype is passed, any dtype might be returned, depending on the array-like
80+
@ty.overload
81+
def __array__(self, dtype: None = ..., /) -> np.ndarray[ty.Any, np.dtype[ty.Any]]:
82+
... # pragma: no cover
83+
84+
# Any dtype might be passed, and *that* dtype must be returned
85+
@ty.overload
86+
def __array__(self, dtype: _DType, /) -> np.ndarray[ty.Any, _DType]:
7487
... # pragma: no cover
7588

7689
def __getitem__(self, key, /) -> npt.NDArray:

0 commit comments

Comments
 (0)