Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/3780.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +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.
21 changes: 14 additions & 7 deletions src/zarr/core/buffer/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,14 @@ def __setitem__(self, key: slice, value: Any) -> None: ...

@runtime_checkable
class NDArrayLike(Protocol):
"""Protocol for the nd-array-like type that underlie NDBuffer"""
"""Protocol for the nd-array-like type that underlie NDBuffer

Notes
-----
Methods that transform the array (reshape, view, astype, ravel, transpose)
return NDArrayLike rather than Self to correctly represent NumPy 2.0+ typing,
which is stricter about shape and dtype transformations.
"""

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

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

def view(self, dtype: npt.DTypeLike) -> Self: ...
def view(self, dtype: npt.DTypeLike) -> NDArrayLike: ...

def astype(
self,
dtype: npt.DTypeLike,
order: Literal["K", "A", "C", "F"] = ...,
*,
copy: bool = ...,
) -> Self: ...
) -> NDArrayLike: ...

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

def copy(self) -> Self: ...
def copy(self) -> NDArrayLike: ...

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

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

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

Expand Down
Loading