From b005620a8597a8ca82f8744a3ca0fc5f9340da71 Mon Sep 17 00:00:00 2001 From: brokkoli71 Date: Wed, 12 Feb 2025 13:30:49 +0100 Subject: [PATCH] fix mypy --- src/zarr/core/array.py | 15 ++++++++++----- src/zarr/core/buffer/cpu.py | 4 ++-- src/zarr/core/buffer/gpu.py | 4 ++-- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/zarr/core/array.py b/src/zarr/core/array.py index bca3c51606..bfd69f5168 100644 --- a/src/zarr/core/array.py +++ b/src/zarr/core/array.py @@ -38,6 +38,7 @@ NDBuffer, default_buffer_prototype, ) +from zarr.core.buffer.core import NDArrayLike from zarr.core.chunk_grids import RegularChunkGrid, _auto_partition, normalize_chunks from zarr.core.chunk_key_encodings import ( ChunkKeyEncoding, @@ -1400,7 +1401,7 @@ async def _set_selection( value = value.astype(dtype=self.metadata.dtype, order="A") else: value = np.array(value, dtype=self.metadata.dtype, order="A") - value = cast(NDArrayOrScalarLike, value) + value = cast(NDArrayLike, value) # We accept any ndarray like object from the user and convert it # to a NDBuffer (or subclass). From this point onwards, we only pass # Buffer and NDBuffer between components. @@ -2260,7 +2261,7 @@ def _iter_chunk_regions( def __array__( self, dtype: npt.DTypeLike | None = None, copy: bool | None = None - ) -> NDArrayOrScalarLike: + ) -> NDArrayLike: """ This method is used by numpy when converting zarr.Array into a numpy array. For more information, see https://numpy.org/devdocs/user/basics.interoperability.html#the-array-method @@ -2269,9 +2270,13 @@ def __array__( msg = "`copy=False` is not supported. This method always creates a copy." raise ValueError(msg) - arr_np = self[...] - if self.ndim == 0: - arr_np = np.array(arr_np) + arr = self[...] + arr_np: NDArrayLike + + if not hasattr(arr, "astype"): + arr_np = np.array(arr, dtype=dtype) + else: + arr_np = arr if dtype is not None: arr_np = arr_np.astype(dtype) diff --git a/src/zarr/core/buffer/cpu.py b/src/zarr/core/buffer/cpu.py index dc54e4fe23..225adb6f5c 100644 --- a/src/zarr/core/buffer/cpu.py +++ b/src/zarr/core/buffer/cpu.py @@ -19,7 +19,7 @@ from collections.abc import Callable, Iterable from typing import Self - from zarr.core.buffer.core import ArrayLike, NDArrayOrScalarLike + from zarr.core.buffer.core import ArrayLike, NDArrayLike from zarr.core.common import BytesLike @@ -142,7 +142,7 @@ class NDBuffer(core.NDBuffer): ndarray-like object that is convertible to a regular Numpy array. """ - def __init__(self, array: NDArrayOrScalarLike) -> None: + def __init__(self, array: NDArrayLike) -> None: super().__init__(array) @classmethod diff --git a/src/zarr/core/buffer/gpu.py b/src/zarr/core/buffer/gpu.py index c810eca10c..6941c8897e 100644 --- a/src/zarr/core/buffer/gpu.py +++ b/src/zarr/core/buffer/gpu.py @@ -12,7 +12,7 @@ import numpy.typing as npt from zarr.core.buffer import core -from zarr.core.buffer.core import ArrayLike, BufferPrototype, NDArrayOrScalarLike +from zarr.core.buffer.core import ArrayLike, BufferPrototype, NDArrayLike if TYPE_CHECKING: from collections.abc import Iterable @@ -136,7 +136,7 @@ class NDBuffer(core.NDBuffer): ndarray-like object that is convertible to a regular Numpy array. """ - def __init__(self, array: NDArrayOrScalarLike) -> None: + def __init__(self, array: NDArrayLike) -> None: if cp is None: raise ImportError( "Cannot use zarr.buffer.gpu.NDBuffer without cupy. Please install cupy."