Skip to content

Commit 6adeec7

Browse files
committed
Add numpy to mypy pre-commit check env
1 parent 50b3fb1 commit 6adeec7

File tree

8 files changed

+28
-21
lines changed

8 files changed

+28
-21
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ repos:
2626
hooks:
2727
- id: mypy
2828
files: src
29-
args: []
3029
additional_dependencies:
3130
- types-redis
3231
- types-setuptools
3332
- pytest
33+
- numpy

src/zarr/array.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ async def setitem(
417417
# We accept any ndarray like object from the user and convert it
418418
# to a NDBuffer (or subclass). From this point onwards, we only pass
419419
# Buffer and NDBuffer between components.
420-
value = factory(value)
420+
value_buffer = factory(value)
421421

422422
# merging with existing data and encoding chunks
423423
await self.metadata.codec_pipeline.write(
@@ -430,7 +430,7 @@ async def setitem(
430430
)
431431
for chunk_coords, chunk_selection, out_selection in indexer
432432
],
433-
value,
433+
value_buffer,
434434
)
435435

436436
async def resize(

src/zarr/buffer.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
)
1212

1313
import numpy as np
14+
import numpy.typing as npt
1415

1516
if TYPE_CHECKING:
1617
from typing_extensions import Self
@@ -20,8 +21,8 @@
2021

2122
# TODO: create a protocol for the attributes we need, for now we alias Numpy's ndarray
2223
# both for the array-like and ndarray-like
23-
ArrayLike: TypeAlias = np.ndarray
24-
NDArrayLike: TypeAlias = np.ndarray
24+
ArrayLike: TypeAlias = npt.NDArray[Any]
25+
NDArrayLike: TypeAlias = npt.NDArray[Any]
2526

2627

2728
def check_item_key_is_1d_contiguous(key: Any) -> None:
@@ -40,7 +41,7 @@ def __call__(
4041
self,
4142
*,
4243
shape: Iterable[int],
43-
dtype: np.DTypeLike,
44+
dtype: npt.DTypeLike,
4445
order: Literal["C", "F"],
4546
fill_value: Any | None,
4647
) -> NDBuffer:
@@ -163,7 +164,7 @@ def as_array_like(self) -> NDArrayLike:
163164
"""
164165
return self._data
165166

166-
def as_nd_buffer(self, *, dtype: np.DTypeLike) -> NDBuffer:
167+
def as_nd_buffer(self, *, dtype: npt.DTypeLike) -> NDBuffer:
167168
"""Create a new NDBuffer from this one.
168169
169170
This will never copy data.
@@ -179,7 +180,7 @@ def as_nd_buffer(self, *, dtype: np.DTypeLike) -> NDBuffer:
179180
"""
180181
return NDBuffer.from_ndarray_like(self._data.view(dtype=dtype))
181182

182-
def as_numpy_array(self) -> np.ndarray:
183+
def as_numpy_array(self) -> npt.NDArray[Any]:
183184
"""Return the buffer as a NumPy array (host memory).
184185
185186
Warning
@@ -271,7 +272,7 @@ def create(
271272
cls,
272273
*,
273274
shape: Iterable[int],
274-
dtype: np.DTypeLike,
275+
dtype: npt.DTypeLike,
275276
order: Literal["C", "F"] = "C",
276277
fill_value: Any | None = None,
277278
) -> Self:
@@ -298,7 +299,7 @@ def create(
298299
A subclass can overwrite this method to create a ndarray-like object
299300
other then the default Numpy array.
300301
"""
301-
ret = cls(np.empty(shape=shape, dtype=dtype, order=order))
302+
ret = cls(np.empty(shape=tuple(shape), dtype=dtype, order=order))
302303
if fill_value is not None:
303304
ret.fill(fill_value)
304305
return ret
@@ -319,7 +320,7 @@ def from_ndarray_like(cls, ndarray_like: NDArrayLike) -> Self:
319320
return cls(ndarray_like)
320321

321322
@classmethod
322-
def from_numpy_array(cls, array_like: np.ArrayLike) -> Self:
323+
def from_numpy_array(cls, array_like: npt.ArrayLike) -> Self:
323324
"""Create a new buffer of Numpy array-like object
324325
325326
Parameters
@@ -360,7 +361,7 @@ def as_buffer(self) -> Buffer:
360361
data = np.ascontiguousarray(self._data)
361362
return Buffer(data.reshape(-1).view(dtype="b")) # Flatten the array without copy
362363

363-
def as_numpy_array(self) -> np.ndarray:
364+
def as_numpy_array(self) -> npt.NDArray[Any]:
364365
"""Return the buffer as a NumPy array (host memory).
365366
366367
Warning
@@ -393,9 +394,9 @@ def byteorder(self) -> Endian:
393394
return Endian(sys.byteorder)
394395

395396
def reshape(self, newshape: Iterable[int]) -> Self:
396-
return self.__class__(self._data.reshape(newshape))
397+
return self.__class__(self._data.reshape(tuple(newshape)))
397398

398-
def astype(self, dtype: np.DTypeLike, order: Literal["K", "A", "C", "F"] = "K") -> Self:
399+
def astype(self, dtype: npt.DTypeLike, order: Literal["K", "A", "C", "F"] = "K") -> Self:
399400
return self.__class__(self._data.astype(dtype=dtype, order=order))
400401

401402
def __getitem__(self, key: Any) -> Self:
@@ -422,7 +423,7 @@ def transpose(self, *axes: np.SupportsIndex) -> Self:
422423
return self.__class__(self._data.transpose(*axes))
423424

424425

425-
def as_numpy_array_wrapper(func: Callable[[np.ndarray], bytes], buf: Buffer) -> Buffer:
426+
def as_numpy_array_wrapper(func: Callable[[npt.NDArray[Any]], bytes], buf: Buffer) -> Buffer:
426427
"""Converts the input of `func` to a numpy array and the output back to `Buffer`.
427428
428429
This function is useful when calling a `func` that only support host memory such

src/zarr/codecs/bytes.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ async def encode_single(
9292
assert isinstance(chunk_array, NDBuffer)
9393
if chunk_array.dtype.itemsize > 1:
9494
if self.endian is not None and self.endian != chunk_array.byteorder:
95-
new_dtype = chunk_array.dtype.newbyteorder(self.endian.name)
95+
# type-ignore is a numpy bug
96+
# see https://github.com/numpy/numpy/issues/26473
97+
new_dtype = chunk_array.dtype.newbyteorder(self.endian.name) # type: ignore[arg-type]
9698
chunk_array = chunk_array.astype(new_dtype)
9799
return chunk_array.as_buffer()
98100

src/zarr/codecs/sharding.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from typing import TYPE_CHECKING, NamedTuple
99

1010
import numpy as np
11+
import numpy.typing as npt
1112

1213
from zarr.abc.codec import ByteGetter, ByteSetter, Codec, CodecPipeline
1314
from zarr.buffer import Buffer, NDBuffer
@@ -82,7 +83,7 @@ async def delete(self) -> None:
8283

8384
class _ShardIndex(NamedTuple):
8485
# dtype uint64, shape (chunks_per_shard_0, chunks_per_shard_1, ..., 2)
85-
offsets_and_lengths: np.ndarray
86+
offsets_and_lengths: npt.NDArray[np.uint64]
8687

8788
@property
8889
def chunks_per_shard(self) -> ChunkCoords:
@@ -97,7 +98,7 @@ def _localize_chunk(self, chunk_coords: ChunkCoords) -> ChunkCoords:
9798
def is_all_empty(self) -> bool:
9899
return bool(np.array_equiv(self.offsets_and_lengths, MAX_UINT_64))
99100

100-
def get_full_chunk_map(self) -> np.ndarray:
101+
def get_full_chunk_map(self) -> npt.NDArray[np.bool_]:
101102
return self.offsets_and_lengths[..., 0] != MAX_UINT_64
102103

103104
def get_chunk_slice(self, chunk_coords: ChunkCoords) -> tuple[int, int] | None:

src/zarr/metadata.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ def to_dict(self) -> JSON:
399399
zarray_dict["chunks"] = self.chunk_grid.chunk_shape
400400

401401
_ = zarray_dict.pop("data_type")
402-
zarray_dict["dtype"] = self.data_type
402+
zarray_dict["dtype"] = self.data_type.str
403403

404404
return zarray_dict
405405

src/zarr/store/local.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ def _put(
5858
if start is not None:
5959
with path.open("r+b") as f:
6060
f.seek(start)
61-
f.write(value.as_numpy_array())
61+
f.write(value.as_numpy_array().tobytes())
6262
return None
6363
else:
64-
return path.write_bytes(value.as_numpy_array())
64+
return path.write_bytes(value.as_numpy_array().tobytes())
6565

6666

6767
class LocalStore(Store):

test.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import numpy as np
2+
a = np.array([1, 2, 3])
3+
a.dtype.newbyteorder("little")

0 commit comments

Comments
 (0)