Skip to content

Some misc typing fixes #1892

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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: 0 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ repos:
hooks:
- id: mypy
files: src
args: []
additional_dependencies:
- types-redis
- types-setuptools
Expand Down
4 changes: 2 additions & 2 deletions src/zarr/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ async def setitem(
# 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.
value = factory(value)
value_buffer = factory(value)

# merging with existing data and encoding chunks
await self.metadata.codec_pipeline.write(
Expand All @@ -430,7 +430,7 @@ async def setitem(
)
for chunk_coords, chunk_selection, out_selection in indexer
],
value,
value_buffer,
)

async def resize(
Expand Down
17 changes: 9 additions & 8 deletions src/zarr/buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
)

import numpy as np
import numpy.typing as npt

if TYPE_CHECKING:
from typing_extensions import Self
Expand Down Expand Up @@ -40,7 +41,7 @@ def __call__(
self,
*,
shape: Iterable[int],
dtype: np.DTypeLike,
dtype: npt.DTypeLike,
order: Literal["C", "F"],
fill_value: Any | None,
) -> NDBuffer:
Expand Down Expand Up @@ -163,7 +164,7 @@ def as_array_like(self) -> NDArrayLike:
"""
return self._data

def as_nd_buffer(self, *, dtype: np.DTypeLike) -> NDBuffer:
def as_nd_buffer(self, *, dtype: npt.DTypeLike) -> NDBuffer:
"""Create a new NDBuffer from this one.

This will never copy data.
Expand All @@ -179,7 +180,7 @@ def as_nd_buffer(self, *, dtype: np.DTypeLike) -> NDBuffer:
"""
return NDBuffer.from_ndarray_like(self._data.view(dtype=dtype))

def as_numpy_array(self) -> np.ndarray:
def as_numpy_array(self) -> npt.NDArray[Any]:
"""Return the buffer as a NumPy array (host memory).

Warning
Expand Down Expand Up @@ -271,7 +272,7 @@ def create(
cls,
*,
shape: Iterable[int],
dtype: np.DTypeLike,
dtype: npt.DTypeLike,
order: Literal["C", "F"] = "C",
fill_value: Any | None = None,
) -> Self:
Expand Down Expand Up @@ -319,7 +320,7 @@ def from_ndarray_like(cls, ndarray_like: NDArrayLike) -> Self:
return cls(ndarray_like)

@classmethod
def from_numpy_array(cls, array_like: np.ArrayLike) -> Self:
def from_numpy_array(cls, array_like: npt.ArrayLike) -> Self:
"""Create a new buffer of Numpy array-like object

Parameters
Expand Down Expand Up @@ -360,7 +361,7 @@ def as_buffer(self) -> Buffer:
data = np.ascontiguousarray(self._data)
return Buffer(data.reshape(-1).view(dtype="b")) # Flatten the array without copy

def as_numpy_array(self) -> np.ndarray:
def as_numpy_array(self) -> npt.NDArray[Any]:
"""Return the buffer as a NumPy array (host memory).

Warning
Expand Down Expand Up @@ -395,7 +396,7 @@ def byteorder(self) -> Endian:
def reshape(self, newshape: Iterable[int]) -> Self:
return self.__class__(self._data.reshape(newshape))

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

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


def as_numpy_array_wrapper(func: Callable[[np.ndarray], bytes], buf: Buffer) -> Buffer:
def as_numpy_array_wrapper(func: Callable[[npt.NDArray[Any]], bytes], buf: Buffer) -> Buffer:
"""Converts the input of `func` to a numpy array and the output back to `Buffer`.

This function is useful when calling a `func` that only support host memory such
Expand Down
5 changes: 3 additions & 2 deletions src/zarr/codecs/sharding.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from typing import TYPE_CHECKING, NamedTuple

import numpy as np
import numpy.typing as npt

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

class _ShardIndex(NamedTuple):
# dtype uint64, shape (chunks_per_shard_0, chunks_per_shard_1, ..., 2)
offsets_and_lengths: np.ndarray
offsets_and_lengths: npt.NDArray[np.uint64]

@property
def chunks_per_shard(self) -> ChunkCoords:
Expand All @@ -97,7 +98,7 @@ def _localize_chunk(self, chunk_coords: ChunkCoords) -> ChunkCoords:
def is_all_empty(self) -> bool:
return bool(np.array_equiv(self.offsets_and_lengths, MAX_UINT_64))

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

def get_chunk_slice(self, chunk_coords: ChunkCoords) -> tuple[int, int] | None:
Expand Down
2 changes: 1 addition & 1 deletion src/zarr/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ def to_dict(self) -> JSON:
zarray_dict["chunks"] = self.chunk_grid.chunk_shape

_ = zarray_dict.pop("data_type")
zarray_dict["dtype"] = self.data_type
zarray_dict["dtype"] = self.data_type.str

return zarray_dict

Expand Down
Loading