Skip to content

Commit e602aa1

Browse files
authored
Use ChunkKeyEncodingLike type alias (#2763)
* make proper ChunkKeyEncodingLike type alias, and use it * model the not-required-ness of the separator * add cast * changelog
1 parent 0c895d1 commit e602aa1

File tree

4 files changed

+25
-15
lines changed

4 files changed

+25
-15
lines changed

changes/2763.chore.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Created a type alias ``ChunkKeyEncodingLike`` to model the union of ``ChunkKeyEncoding`` instances and the dict form of the
2+
parameters of those instances. ``ChunkKeyEncodingLike`` should be used by high-level functions to provide a convenient
3+
way for creating ``ChunkKeyEncoding`` objects.

src/zarr/core/array.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ async def create(
412412
# v3 only
413413
chunk_shape: ShapeLike | None = None,
414414
chunk_key_encoding: (
415-
ChunkKeyEncoding
415+
ChunkKeyEncodingLike
416416
| tuple[Literal["default"], Literal[".", "/"]]
417417
| tuple[Literal["v2"], Literal[".", "/"]]
418418
| None
@@ -453,7 +453,7 @@ async def create(
453453
The shape of the array's chunks
454454
Zarr format 3 only. Zarr format 2 arrays should use `chunks` instead.
455455
If not specified, default are guessed based on the shape and dtype.
456-
chunk_key_encoding : ChunkKeyEncoding, optional
456+
chunk_key_encoding : ChunkKeyEncodingLike, optional
457457
A specification of how the chunk keys are represented in storage.
458458
Zarr format 3 only. Zarr format 2 arrays should use `dimension_separator` instead.
459459
Default is ``("default", "/")``.
@@ -553,7 +553,7 @@ async def _create(
553553
# v3 only
554554
chunk_shape: ShapeLike | None = None,
555555
chunk_key_encoding: (
556-
ChunkKeyEncoding
556+
ChunkKeyEncodingLike
557557
| tuple[Literal["default"], Literal[".", "/"]]
558558
| tuple[Literal["v2"], Literal[".", "/"]]
559559
| None
@@ -671,7 +671,7 @@ async def _create_v3(
671671
config: ArrayConfig,
672672
fill_value: Any | None = None,
673673
chunk_key_encoding: (
674-
ChunkKeyEncoding
674+
ChunkKeyEncodingLike
675675
| tuple[Literal["default"], Literal[".", "/"]]
676676
| tuple[Literal["v2"], Literal[".", "/"]]
677677
| None
@@ -1708,7 +1708,7 @@ def create(
17081708
The shape of the Array's chunks.
17091709
Zarr format 3 only. Zarr format 2 arrays should use `chunks` instead.
17101710
If not specified, default are guessed based on the shape and dtype.
1711-
chunk_key_encoding : ChunkKeyEncoding, optional
1711+
chunk_key_encoding : ChunkKeyEncodingLike, optional
17121712
A specification of how the chunk keys are represented in storage.
17131713
Zarr format 3 only. Zarr format 2 arrays should use `dimension_separator` instead.
17141714
Default is ``("default", "/")``.
@@ -3756,7 +3756,7 @@ async def create_array(
37563756
order: MemoryOrder | None = None,
37573757
zarr_format: ZarrFormat | None = 3,
37583758
attributes: dict[str, JSON] | None = None,
3759-
chunk_key_encoding: ChunkKeyEncoding | ChunkKeyEncodingLike | None = None,
3759+
chunk_key_encoding: ChunkKeyEncodingLike | None = None,
37603760
dimension_names: Iterable[str] | None = None,
37613761
storage_options: dict[str, Any] | None = None,
37623762
overwrite: bool = False,
@@ -3834,7 +3834,7 @@ async def create_array(
38343834
The zarr format to use when saving.
38353835
attributes : dict, optional
38363836
Attributes for the array.
3837-
chunk_key_encoding : ChunkKeyEncoding, optional
3837+
chunk_key_encoding : ChunkKeyEncodingLike, optional
38383838
A specification of how the chunk keys are represented in storage.
38393839
For Zarr format 3, the default is ``{"name": "default", "separator": "/"}}``.
38403840
For Zarr format 2, the default is ``{"name": "v2", "separator": "."}}``.

src/zarr/core/chunk_key_encodings.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
from abc import abstractmethod
44
from dataclasses import dataclass
5-
from typing import Literal, TypedDict, cast
5+
from typing import TYPE_CHECKING, Literal, TypeAlias, TypedDict, cast
6+
7+
if TYPE_CHECKING:
8+
from typing import NotRequired
69

710
from zarr.abc.metadata import Metadata
811
from zarr.core.common import (
@@ -20,9 +23,9 @@ def parse_separator(data: JSON) -> SeparatorLiteral:
2023
return cast(SeparatorLiteral, data)
2124

2225

23-
class ChunkKeyEncodingLike(TypedDict):
26+
class ChunkKeyEncodingParams(TypedDict):
2427
name: Literal["v2", "default"]
25-
separator: SeparatorLiteral
28+
separator: NotRequired[SeparatorLiteral]
2629

2730

2831
@dataclass(frozen=True)
@@ -36,16 +39,17 @@ def __init__(self, *, separator: SeparatorLiteral) -> None:
3639
object.__setattr__(self, "separator", separator_parsed)
3740

3841
@classmethod
39-
def from_dict(
40-
cls, data: dict[str, JSON] | ChunkKeyEncoding | ChunkKeyEncodingLike
41-
) -> ChunkKeyEncoding:
42+
def from_dict(cls, data: dict[str, JSON] | ChunkKeyEncodingLike) -> ChunkKeyEncoding:
4243
if isinstance(data, ChunkKeyEncoding):
4344
return data
4445

4546
# handle ChunkKeyEncodingParams
4647
if "name" in data and "separator" in data:
4748
data = {"name": data["name"], "configuration": {"separator": data["separator"]}}
4849

50+
# TODO: remove this cast when we are statically typing the JSON metadata completely.
51+
data = cast(dict[str, JSON], data)
52+
4953
# configuration is optional for chunk key encodings
5054
name_parsed, config_parsed = parse_named_configuration(data, require_configuration=False)
5155
if name_parsed == "default":
@@ -73,6 +77,9 @@ def encode_chunk_key(self, chunk_coords: ChunkCoords) -> str:
7377
pass
7478

7579

80+
ChunkKeyEncodingLike: TypeAlias = ChunkKeyEncodingParams | ChunkKeyEncoding
81+
82+
7683
@dataclass(frozen=True)
7784
class DefaultChunkKeyEncoding(ChunkKeyEncoding):
7885
name: Literal["default"] = "default"

src/zarr/core/metadata/v3.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from zarr.abc.codec import ArrayArrayCodec, ArrayBytesCodec, BytesBytesCodec, Codec
2828
from zarr.core.array_spec import ArrayConfig, ArraySpec
2929
from zarr.core.chunk_grids import ChunkGrid, RegularChunkGrid
30-
from zarr.core.chunk_key_encodings import ChunkKeyEncoding
30+
from zarr.core.chunk_key_encodings import ChunkKeyEncoding, ChunkKeyEncodingLike
3131
from zarr.core.common import (
3232
JSON,
3333
ZARR_JSON,
@@ -253,7 +253,7 @@ def __init__(
253253
shape: Iterable[int],
254254
data_type: npt.DTypeLike | DataType,
255255
chunk_grid: dict[str, JSON] | ChunkGrid,
256-
chunk_key_encoding: dict[str, JSON] | ChunkKeyEncoding,
256+
chunk_key_encoding: ChunkKeyEncodingLike,
257257
fill_value: Any,
258258
codecs: Iterable[Codec | dict[str, JSON]],
259259
attributes: dict[str, JSON] | None,

0 commit comments

Comments
 (0)