Skip to content

Commit bb11867

Browse files
committed
adjust type annotations
1 parent c506d09 commit bb11867

File tree

10 files changed

+33
-25
lines changed

10 files changed

+33
-25
lines changed

src/zarr/api/asynchronous.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -990,7 +990,7 @@ async def create(
990990
chunks = shape
991991
default_filters, default_compressor = _get_default_chunk_encoding_v2(dtype_wrapped)
992992
if filters is None:
993-
filters = default_filters
993+
filters = default_filters # type: ignore[assignment]
994994
if compressor is None:
995995
compressor = default_compressor
996996
elif zarr_format == 3 and chunk_shape is None: # type: ignore[redundant-expr]

src/zarr/codecs/_v2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ async def _decode_single(
4646
chunk = ensure_ndarray_like(chunk)
4747
# special case object dtype, because incorrect handling can lead to
4848
# segfaults and other bad things happening
49-
if chunk_spec.dtype != object:
49+
if chunk_spec.dtype.dtype_cls is not np.dtypes.ObjectDType:
5050
try:
5151
chunk = chunk.view(chunk_spec.dtype.to_dtype())
5252
except TypeError:

src/zarr/core/_info.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
import dataclasses
44
import textwrap
5-
from typing import TYPE_CHECKING, Literal
5+
from typing import TYPE_CHECKING, Any, Literal
66

77
if TYPE_CHECKING:
88
import numcodecs.abc
99

1010
from zarr.abc.codec import ArrayArrayCodec, ArrayBytesCodec, BytesBytesCodec
1111
from zarr.core.common import ZarrFormat
12-
from zarr.core.dtype.wrapper import ZDType, _BaseDType, _BaseScalar
12+
from zarr.core.dtype.wrapper import ZDType
1313

1414

1515
@dataclasses.dataclass(kw_only=True)
@@ -80,7 +80,7 @@ class ArrayInfo:
8080

8181
_type: Literal["Array"] = "Array"
8282
_zarr_format: ZarrFormat
83-
_data_type: ZDType[_BaseDType, _BaseScalar]
83+
_data_type: ZDType[Any, Any]
8484
_shape: tuple[int, ...]
8585
_shard_shape: tuple[int, ...] | None = None
8686
_chunk_shape: tuple[int, ...] | None = None

src/zarr/core/array.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,8 @@ def _create_metadata_v3(
680680
"""
681681
Create an instance of ArrayV3Metadata.
682682
"""
683+
filters: tuple[ArrayArrayCodec, ...]
684+
compressors: tuple[BytesBytesCodec, ...]
683685

684686
shape = parse_shapelike(shape)
685687
if codecs is None:
@@ -707,7 +709,7 @@ def _create_metadata_v3(
707709
chunk_grid=chunk_grid_parsed,
708710
chunk_key_encoding=chunk_key_encoding_parsed,
709711
fill_value=fill_value_parsed,
710-
codecs=codecs_parsed,
712+
codecs=codecs_parsed, # type: ignore[arg-type]
711713
dimension_names=tuple(dimension_names) if dimension_names else None,
712714
attributes=attributes or {},
713715
)
@@ -1712,7 +1714,7 @@ def _info(
17121714
) -> Any:
17131715
return ArrayInfo(
17141716
_zarr_format=self.metadata.zarr_format,
1715-
_data_type=self.dtype,
1717+
_data_type=self._zdtype,
17161718
_shape=self.shape,
17171719
_order=self.order,
17181720
_shard_shape=self.shards,

src/zarr/core/array_spec.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
from zarr.core.buffer import BufferPrototype
1919
from zarr.core.common import ChunkCoords
20-
from zarr.core.dtype.wrapper import ZDType, _BaseDType, _BaseScalar
20+
from zarr.core.dtype.wrapper import ZDType
2121

2222

2323
class ArrayConfigParams(TypedDict):
@@ -89,15 +89,15 @@ def parse_array_config(data: ArrayConfigLike | None) -> ArrayConfig:
8989
@dataclass(frozen=True)
9090
class ArraySpec:
9191
shape: ChunkCoords
92-
dtype: ZDType[_BaseDType, _BaseScalar]
92+
dtype: ZDType[Any, Any]
9393
fill_value: Any
9494
config: ArrayConfig
9595
prototype: BufferPrototype
9696

9797
def __init__(
9898
self,
9999
shape: ChunkCoords,
100-
dtype: ZDType[_BaseDType, _BaseScalar],
100+
dtype: ZDType[Any, Any],
101101
fill_value: Any,
102102
config: ArrayConfig,
103103
prototype: BufferPrototype,

src/zarr/core/dtype/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,11 @@
8484
data_type_registry.register(dtype._zarr_v3_name, dtype)
8585

8686

87+
# TODO: find a better name for this function
8788
def get_data_type_from_native_dtype(dtype: npt.DTypeLike) -> ZDType[_BaseDType, _BaseScalar]:
89+
"""
90+
Get a data type wrapper (an instance of ``ZDType``) from a native data type, e.g. a numpy dtype.
91+
"""
8892
data_type_registry.lazy_load()
8993
if not isinstance(dtype, np.dtype):
9094
if dtype in (str, "str"):

src/zarr/core/dtype/wrapper.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
TDType = TypeVar("TDType", bound=_BaseDType)
2323

2424

25-
@dataclass(frozen=True, kw_only=True)
25+
@dataclass(frozen=True, kw_only=True, slots=True)
2626
class ZDType(Generic[TDType, TScalar], ABC):
2727
"""
2828
Abstract base class for wrapping native array data types, e.g. numpy dtypes
@@ -62,7 +62,7 @@ def check_dtype(cls: type[Self], dtype: _BaseDType) -> TypeGuard[TDType]:
6262
return type(dtype) is cls.dtype_cls
6363

6464
@classmethod
65-
def from_dtype(cls: type[Self], dtype: TDType) -> Self:
65+
def from_dtype(cls: type[Self], dtype: _BaseDType) -> Self:
6666
"""
6767
Wrap a dtype object.
6868

src/zarr/core/metadata/v3.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ def validate_codecs(codecs: tuple[Codec, ...], dtype: ZDType[_BaseDType, _BaseSc
109109
# we need to have special codecs if we are decoding vlen strings or bytestrings
110110
# TODO: use codec ID instead of class name
111111
codec_class_name = abc.__class__.__name__
112-
if isinstance(dtype, VariableLengthString) and not codec_class_name == "VLenUTF8Codec":
112+
# TODO: Fix typing here
113+
if isinstance(dtype, VariableLengthString) and not codec_class_name == "VLenUTF8Codec": # type: ignore[unreachable]
113114
raise ValueError(
114115
f"For string dtype, ArrayBytesCodec must be `VLenUTF8Codec`, got `{codec_class_name}`."
115116
)
@@ -407,11 +408,11 @@ def to_dict(self) -> dict[str, JSON]:
407408

408409
# TODO: replace the `to_dict` / `from_dict` on the `Metadata`` class with
409410
# to_json, from_json, and have ZDType inherit from `Metadata`
410-
# until then, we have this hack here
411+
# until then, we have this hack here, which relies on the fact that to_dict will pass through
412+
# any non-`Metadata` fields as-is.
411413
dtype_meta = out_dict["data_type"]
412-
413414
if isinstance(dtype_meta, ZDType):
414-
out_dict["data_type"] = dtype_meta.to_json(zarr_format=3)
415+
out_dict["data_type"] = dtype_meta.to_json(zarr_format=3) # type: ignore[unreachable]
415416

416417
return out_dict
417418

tests/test_array.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
from zarr.core.chunk_grids import _auto_partition
4040
from zarr.core.common import JSON, MemoryOrder, ZarrFormat
4141
from zarr.core.dtype import get_data_type_from_native_dtype
42+
from zarr.core.dtype._numpy import Float64
4243
from zarr.core.group import AsyncGroup
4344
from zarr.core.indexing import BasicIndexer, ceildiv
4445
from zarr.core.sync import sync
@@ -448,7 +449,7 @@ def test_info_v2(self, chunks: tuple[int, int], shards: tuple[int, int] | None)
448449
result = arr.info
449450
expected = ArrayInfo(
450451
_zarr_format=2,
451-
_data_type=arr.dtype,
452+
_data_type=arr._async_array._zdtype,
452453
_shape=(8, 8),
453454
_chunk_shape=chunks,
454455
_shard_shape=None,
@@ -465,7 +466,7 @@ def test_info_v3(self, chunks: tuple[int, int], shards: tuple[int, int] | None)
465466
result = arr.info
466467
expected = ArrayInfo(
467468
_zarr_format=3,
468-
_data_type=arr.dtype,
469+
_data_type=arr._async_array._zdtype,
469470
_shape=(8, 8),
470471
_chunk_shape=chunks,
471472
_shard_shape=shards,
@@ -490,7 +491,7 @@ def test_info_complete(self, chunks: tuple[int, int], shards: tuple[int, int] |
490491
result = arr.info_complete()
491492
expected = ArrayInfo(
492493
_zarr_format=3,
493-
_data_type=arr.dtype,
494+
_data_type=arr._async_array._zdtype,
494495
_shape=(8, 8),
495496
_chunk_shape=chunks,
496497
_shard_shape=shards,
@@ -525,7 +526,7 @@ async def test_info_v2_async(
525526
result = arr.info
526527
expected = ArrayInfo(
527528
_zarr_format=2,
528-
_data_type=np.dtype("float64"),
529+
_data_type=Float64(),
529530
_shape=(8, 8),
530531
_chunk_shape=(2, 2),
531532
_shard_shape=None,
@@ -550,7 +551,7 @@ async def test_info_v3_async(
550551
result = arr.info
551552
expected = ArrayInfo(
552553
_zarr_format=3,
553-
_data_type=arr.dtype,
554+
_data_type=arr._zdtype,
554555
_shape=(8, 8),
555556
_chunk_shape=chunks,
556557
_shard_shape=shards,
@@ -577,7 +578,7 @@ async def test_info_complete_async(
577578
result = await arr.info_complete()
578579
expected = ArrayInfo(
579580
_zarr_format=3,
580-
_data_type=arr.dtype,
581+
_data_type=arr._zdtype,
581582
_shape=(8, 8),
582583
_chunk_shape=chunks,
583584
_shard_shape=shards,

tests/test_info.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import textwrap
22

3-
import numpy as np
43
import pytest
54

65
from zarr.codecs.bytes import BytesCodec
76
from zarr.core._info import ArrayInfo, GroupInfo, human_readable_size
87
from zarr.core.common import ZarrFormat
8+
from zarr.core.dtype._numpy import Int32
99

1010
ZARR_FORMATS = [2, 3]
1111

@@ -53,7 +53,7 @@ def test_group_info_complete(zarr_format: ZarrFormat) -> None:
5353
def test_array_info(zarr_format: ZarrFormat) -> None:
5454
info = ArrayInfo(
5555
_zarr_format=zarr_format,
56-
_data_type=np.dtype("int32"),
56+
_data_type=Int32(),
5757
_shape=(100, 100),
5858
_chunk_shape=(10, 100),
5959
_order="C",
@@ -91,7 +91,7 @@ def test_array_info_complete(
9191
) = bytes_things
9292
info = ArrayInfo(
9393
_zarr_format=zarr_format,
94-
_data_type=np.dtype("int32"),
94+
_data_type=Int32(),
9595
_shape=(100, 100),
9696
_chunk_shape=(10, 100),
9797
_order="C",

0 commit comments

Comments
 (0)