Skip to content

Commit eca64c0

Browse files
author
Test User
committed
Update NDArrayLike typing for NumPy 2.0 compatibility
Methods that transform arrays (reshape, view, astype, copy, transpose, ravel) now correctly return NDArrayLike instead of Self to accurately represent shape and dtype changes in NumPy 2.0+ typing.
1 parent 420f11c commit eca64c0

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

changes/3780.bugfix.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Update `NDArrayLike` typing for NumPy 2.0 compatibility. Methods that transform arrays (reshape, view, astype, copy, transpose, ravel) now correctly return `NDArrayLike` instead of `Self` to accurately represent shape and dtype changes in NumPy 2.0+ typing.

src/zarr/core/buffer/core.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,14 @@ def __setitem__(self, key: slice, value: Any) -> None: ...
4848

4949
@runtime_checkable
5050
class NDArrayLike(Protocol):
51-
"""Protocol for the nd-array-like type that underlie NDBuffer"""
51+
"""Protocol for the nd-array-like type that underlie NDBuffer
52+
53+
Notes
54+
-----
55+
Methods that transform the array (reshape, view, astype, ravel, transpose)
56+
return NDArrayLike rather than Self to correctly represent NumPy 2.0+ typing,
57+
which is stricter about shape and dtype transformations.
58+
"""
5259

5360
@property
5461
def dtype(self) -> np.dtype[Any]: ...
@@ -72,25 +79,25 @@ def __array__(self) -> npt.NDArray[Any]: ...
7279

7380
def reshape(
7481
self, shape: tuple[int, ...] | Literal[-1], *, order: Literal["A", "C", "F"] = ...
75-
) -> Self: ...
82+
) -> NDArrayLike: ...
7683

77-
def view(self, dtype: npt.DTypeLike) -> Self: ...
84+
def view(self, dtype: npt.DTypeLike) -> NDArrayLike: ...
7885

7986
def astype(
8087
self,
8188
dtype: npt.DTypeLike,
8289
order: Literal["K", "A", "C", "F"] = ...,
8390
*,
8491
copy: bool = ...,
85-
) -> Self: ...
92+
) -> NDArrayLike: ...
8693

8794
def fill(self, value: Any) -> None: ...
8895

89-
def copy(self) -> Self: ...
96+
def copy(self) -> NDArrayLike: ...
9097

91-
def transpose(self, axes: SupportsIndex | Sequence[SupportsIndex] | None) -> Self: ...
98+
def transpose(self, axes: SupportsIndex | Sequence[SupportsIndex] | None) -> NDArrayLike: ...
9299

93-
def ravel(self, order: Literal["K", "A", "C", "F"] = ...) -> Self: ...
100+
def ravel(self, order: Literal["K", "A", "C", "F"] = ...) -> NDArrayLike: ...
94101

95102
def all(self) -> bool: ...
96103

0 commit comments

Comments
 (0)