Skip to content

Commit faa239c

Browse files
committed
Finish typing zarr.metadata
1 parent 67b07fb commit faa239c

File tree

5 files changed

+26
-24
lines changed

5 files changed

+26
-24
lines changed

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,6 @@ module = [
238238
"zarr.array",
239239
"zarr.common",
240240
"zarr.group",
241-
"zarr.metadata"
242241
]
243242
disallow_untyped_defs = false
244243

src/zarr/chunk_grids.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from __future__ import annotations
2-
from typing import TYPE_CHECKING, Any, Dict
2+
from typing import TYPE_CHECKING, Dict
33
from dataclasses import dataclass
44
from zarr.abc.metadata import Metadata
55

@@ -18,13 +18,13 @@
1818
@dataclass(frozen=True)
1919
class ChunkGrid(Metadata):
2020
@classmethod
21-
def from_dict(cls, data: Dict[str, JSON]) -> ChunkGrid:
21+
def from_dict(cls, data: Dict[str, JSON] | ChunkGrid) -> ChunkGrid:
2222
if isinstance(data, ChunkGrid):
2323
return data
2424

2525
name_parsed, _ = parse_named_configuration(data)
2626
if name_parsed == "regular":
27-
return RegularChunkGrid.from_dict(data)
27+
return RegularChunkGrid._from_dict(data)
2828
raise ValueError(f"Unknown chunk grid. Got {name_parsed}.")
2929

3030

@@ -38,7 +38,7 @@ def __init__(self, *, chunk_shape: ChunkCoordsLike) -> None:
3838
object.__setattr__(self, "chunk_shape", chunk_shape_parsed)
3939

4040
@classmethod
41-
def from_dict(cls, data: Dict[str, Any]) -> Self:
41+
def _from_dict(cls, data: Dict[str, JSON]) -> Self:
4242
_, configuration_parsed = parse_named_configuration(data, "regular")
4343

4444
return cls(**configuration_parsed) # type: ignore[arg-type]

src/zarr/chunk_key_encodings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def __init__(self, *, separator: SeparatorLiteral) -> None:
3333
object.__setattr__(self, "separator", separator_parsed)
3434

3535
@classmethod
36-
def from_dict(cls, data: Dict[str, JSON]) -> ChunkKeyEncoding:
36+
def from_dict(cls, data: Dict[str, JSON] | ChunkKeyEncoding) -> ChunkKeyEncoding:
3737
if isinstance(data, ChunkKeyEncoding):
3838
return data
3939

src/zarr/common.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from typing import Awaitable, Callable, Iterator, Optional, Type
2020

2121
import numpy as np
22+
import numpy.typing as npt
2223

2324
ZARR_JSON = "zarr.json"
2425
ZARRAY_JSON = ".zarray"
@@ -152,7 +153,7 @@ def parse_named_configuration(
152153
return name_parsed, configuration_parsed
153154

154155

155-
def parse_shapelike(data: Any) -> tuple[int, ...]:
156+
def parse_shapelike(data: Iterable[int]) -> tuple[int, ...]:
156157
if not isinstance(data, Iterable):
157158
raise TypeError(f"Expected an iterable. Got {data} instead.")
158159
data_tuple = tuple(data)
@@ -166,7 +167,7 @@ def parse_shapelike(data: Any) -> tuple[int, ...]:
166167
return data_tuple
167168

168169

169-
def parse_dtype(data: Any) -> np.dtype[Any]:
170+
def parse_dtype(data: npt.DTypeLike) -> np.dtype[Any]:
170171
# todo: real validation
171172
return np.dtype(data)
172173

src/zarr/metadata.py

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

1818
from zarr.abc.codec import Codec
1919
from zarr.abc.metadata import Metadata
20+
import numcodecs.abc
2021

2122
from zarr.common import (
2223
JSON,
@@ -119,15 +120,15 @@ class ArrayMetadata(Metadata):
119120
def __init__(
120121
self,
121122
*,
122-
shape,
123-
data_type,
124-
chunk_grid,
125-
chunk_key_encoding,
126-
fill_value,
127-
codecs,
128-
attributes,
129-
dimension_names,
130-
):
123+
shape: Iterable[int],
124+
data_type: npt.DTypeLike,
125+
chunk_grid: Dict[str, JSON] | ChunkGrid,
126+
chunk_key_encoding: Dict[str, JSON] | ChunkKeyEncoding,
127+
fill_value: Any,
128+
codecs: Iterable[Union[Codec, JSON]],
129+
attributes: None | Dict[str, JSON],
130+
dimension_names: None | Iterable[str],
131+
) -> None:
131132
"""
132133
Because the class is a frozen dataclass, we set attributes using object.__setattr__
133134
"""
@@ -193,14 +194,14 @@ def get_chunk_spec(self, _chunk_coords: ChunkCoords, order: Literal["C", "F"]) -
193194
)
194195

195196
def to_bytes(self) -> bytes:
196-
def _json_convert(o):
197+
def _json_convert(o: np.dtype[Any] | Enum | Codec) -> str | dict[str, Any]:
197198
if isinstance(o, np.dtype):
198199
return str(o)
199200
if isinstance(o, Enum):
200201
return o.name
201202
# this serializes numcodecs compressors
202203
# todo: implement to_dict for codecs
203-
elif hasattr(o, "get_config"):
204+
elif isinstance(o, numcodecs.abc.Codec):
204205
return o.get_config()
205206
raise TypeError
206207

@@ -290,7 +291,9 @@ def ndim(self) -> int:
290291
return len(self.shape)
291292

292293
def to_bytes(self) -> bytes:
293-
def _json_convert(o):
294+
def _json_convert(
295+
o: np.dtype[Any],
296+
) -> str | list[tuple[str, str] | tuple[str, str, tuple[int, ...]]]:
294297
if isinstance(o, np.dtype):
295298
if o.fields is None:
296299
return o.str
@@ -307,7 +310,7 @@ def from_dict(cls, data: Dict[str, Any]) -> ArrayV2Metadata:
307310
return cls(**data)
308311

309312

310-
def parse_dimension_names(data: Any) -> Tuple[str, ...] | None:
313+
def parse_dimension_names(data: None | Iterable[str]) -> Tuple[str, ...] | None:
311314
if data is None:
312315
return data
313316
if isinstance(data, Iterable) and all([isinstance(x, str) for x in data]):
@@ -317,12 +320,11 @@ def parse_dimension_names(data: Any) -> Tuple[str, ...] | None:
317320

318321

319322
# todo: real validation
320-
def parse_attributes(data: Any) -> Dict[str, JSON]:
323+
def parse_attributes(data: None | Dict[str, JSON]) -> Dict[str, JSON]:
321324
if data is None:
322325
return {}
323326

324-
data_json = cast(Dict[str, JSON], data)
325-
return data_json
327+
return data
326328

327329

328330
# todo: move to its own module and drop _v3 suffix

0 commit comments

Comments
 (0)