Skip to content

Commit 6ea9997

Browse files
mypy fixes for NumPy 2.1.0 (#2139)
* mypy fixes for NumPy 2.1.0 This includes some fixes to get mypy type checks passing. * fixup * Update src/zarr/codecs/crc32c_.py Co-authored-by: David Stansby <[email protected]> --------- Co-authored-by: David Stansby <[email protected]>
1 parent b1f26c0 commit 6ea9997

File tree

4 files changed

+24
-8
lines changed

4 files changed

+24
-8
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ repos:
2222
hooks:
2323
- id: check-yaml
2424
- repo: https://github.com/pre-commit/mirrors-mypy
25-
rev: v1.11.1
25+
rev: v1.11.2
2626
hooks:
2727
- id: mypy
2828
files: src

src/zarr/codecs/crc32c_.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
from __future__ import annotations
22

33
from dataclasses import dataclass
4-
from typing import TYPE_CHECKING
4+
from typing import TYPE_CHECKING, cast
55

66
import numpy as np
7+
import typing_extensions
78
from crc32c import crc32c
89

910
from zarr.abc.codec import BytesBytesCodec
@@ -37,7 +38,8 @@ async def _decode_single(
3738
crc32_bytes = data[-4:]
3839
inner_bytes = data[:-4]
3940

40-
computed_checksum = np.uint32(crc32c(inner_bytes)).tobytes()
41+
# Need to do a manual cast until https://github.com/numpy/numpy/issues/26783 is resolved
42+
computed_checksum = np.uint32(crc32c(cast(typing_extensions.Buffer, inner_bytes))).tobytes()
4143
stored_checksum = bytes(crc32_bytes)
4244
if computed_checksum != stored_checksum:
4345
raise ValueError(
@@ -52,7 +54,7 @@ async def _encode_single(
5254
) -> Buffer | None:
5355
data = chunk_bytes.as_numpy_array()
5456
# Calculate the checksum and "cast" it to a numpy array
55-
checksum = np.array([crc32c(data)], dtype=np.uint32)
57+
checksum = np.array([crc32c(cast(typing_extensions.Buffer, data))], dtype=np.uint32)
5658
# Append the checksum (as bytes) to the data
5759
return chunk_spec.prototype.buffer.from_array_like(np.append(data, checksum.view("b")))
5860

src/zarr/codecs/sharding.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from enum import Enum
66
from functools import lru_cache
77
from operator import itemgetter
8-
from typing import TYPE_CHECKING, Any, NamedTuple
8+
from typing import TYPE_CHECKING, Any, NamedTuple, cast
99

1010
import numpy as np
1111
import numpy.typing as npt
@@ -101,7 +101,9 @@ class _ShardIndex(NamedTuple):
101101

102102
@property
103103
def chunks_per_shard(self) -> ChunkCoords:
104-
return self.offsets_and_lengths.shape[0:-1]
104+
result = tuple(self.offsets_and_lengths.shape[0:-1])
105+
# The cast is required until https://github.com/numpy/numpy/pull/27211 is merged
106+
return cast(ChunkCoords, result)
105107

106108
def _localize_chunk(self, chunk_coords: ChunkCoords) -> ChunkCoords:
107109
return tuple(

src/zarr/core/metadata.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -602,9 +602,21 @@ def parse_fill_value_v3(fill_value: Any, dtype: FLOAT_DTYPE) -> FLOAT: ...
602602
def parse_fill_value_v3(fill_value: Any, dtype: COMPLEX_DTYPE) -> COMPLEX: ...
603603

604604

605+
@overload
606+
def parse_fill_value_v3(fill_value: Any, dtype: np.dtype[Any]) -> Any:
607+
# This dtype[Any] is unfortunately necessary right now.
608+
# See https://github.com/zarr-developers/zarr-python/issues/2131#issuecomment-2318010899
609+
# for more details, but `dtype` here (which comes from `parse_dtype`)
610+
# is np.dtype[Any].
611+
#
612+
# If you want the specialized types rather than Any, you need to use `np.dtypes.<dtype>`
613+
# rather than np.dtypes(<type>)
614+
...
615+
616+
605617
def parse_fill_value_v3(
606-
fill_value: Any, dtype: BOOL_DTYPE | INTEGER_DTYPE | FLOAT_DTYPE | COMPLEX_DTYPE
607-
) -> BOOL | INTEGER | FLOAT | COMPLEX:
618+
fill_value: Any, dtype: BOOL_DTYPE | INTEGER_DTYPE | FLOAT_DTYPE | COMPLEX_DTYPE | np.dtype[Any]
619+
) -> BOOL | INTEGER | FLOAT | COMPLEX | Any:
608620
"""
609621
Parse `fill_value`, a potential fill value, into an instance of `dtype`, a data type.
610622
If `fill_value` is `None`, then this function will return the result of casting the value 0

0 commit comments

Comments
 (0)