Skip to content

Commit b005620

Browse files
committed
fix mypy
1 parent a9c0eab commit b005620

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

src/zarr/core/array.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
NDBuffer,
3939
default_buffer_prototype,
4040
)
41+
from zarr.core.buffer.core import NDArrayLike
4142
from zarr.core.chunk_grids import RegularChunkGrid, _auto_partition, normalize_chunks
4243
from zarr.core.chunk_key_encodings import (
4344
ChunkKeyEncoding,
@@ -1400,7 +1401,7 @@ async def _set_selection(
14001401
value = value.astype(dtype=self.metadata.dtype, order="A")
14011402
else:
14021403
value = np.array(value, dtype=self.metadata.dtype, order="A")
1403-
value = cast(NDArrayOrScalarLike, value)
1404+
value = cast(NDArrayLike, value)
14041405
# We accept any ndarray like object from the user and convert it
14051406
# to a NDBuffer (or subclass). From this point onwards, we only pass
14061407
# Buffer and NDBuffer between components.
@@ -2260,7 +2261,7 @@ def _iter_chunk_regions(
22602261

22612262
def __array__(
22622263
self, dtype: npt.DTypeLike | None = None, copy: bool | None = None
2263-
) -> NDArrayOrScalarLike:
2264+
) -> NDArrayLike:
22642265
"""
22652266
This method is used by numpy when converting zarr.Array into a numpy array.
22662267
For more information, see https://numpy.org/devdocs/user/basics.interoperability.html#the-array-method
@@ -2269,9 +2270,13 @@ def __array__(
22692270
msg = "`copy=False` is not supported. This method always creates a copy."
22702271
raise ValueError(msg)
22712272

2272-
arr_np = self[...]
2273-
if self.ndim == 0:
2274-
arr_np = np.array(arr_np)
2273+
arr = self[...]
2274+
arr_np: NDArrayLike
2275+
2276+
if not hasattr(arr, "astype"):
2277+
arr_np = np.array(arr, dtype=dtype)
2278+
else:
2279+
arr_np = arr
22752280

22762281
if dtype is not None:
22772282
arr_np = arr_np.astype(dtype)

src/zarr/core/buffer/cpu.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from collections.abc import Callable, Iterable
2020
from typing import Self
2121

22-
from zarr.core.buffer.core import ArrayLike, NDArrayOrScalarLike
22+
from zarr.core.buffer.core import ArrayLike, NDArrayLike
2323
from zarr.core.common import BytesLike
2424

2525

@@ -142,7 +142,7 @@ class NDBuffer(core.NDBuffer):
142142
ndarray-like object that is convertible to a regular Numpy array.
143143
"""
144144

145-
def __init__(self, array: NDArrayOrScalarLike) -> None:
145+
def __init__(self, array: NDArrayLike) -> None:
146146
super().__init__(array)
147147

148148
@classmethod

src/zarr/core/buffer/gpu.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import numpy.typing as npt
1313

1414
from zarr.core.buffer import core
15-
from zarr.core.buffer.core import ArrayLike, BufferPrototype, NDArrayOrScalarLike
15+
from zarr.core.buffer.core import ArrayLike, BufferPrototype, NDArrayLike
1616

1717
if TYPE_CHECKING:
1818
from collections.abc import Iterable
@@ -136,7 +136,7 @@ class NDBuffer(core.NDBuffer):
136136
ndarray-like object that is convertible to a regular Numpy array.
137137
"""
138138

139-
def __init__(self, array: NDArrayOrScalarLike) -> None:
139+
def __init__(self, array: NDArrayLike) -> None:
140140
if cp is None:
141141
raise ImportError(
142142
"Cannot use zarr.buffer.gpu.NDBuffer without cupy. Please install cupy."

0 commit comments

Comments
 (0)