Skip to content

Commit 47d4b4e

Browse files
committed
make proper ChunkKeyEncodingLike type alias, and use it
1 parent ad99a67 commit 47d4b4e

File tree

3 files changed

+15
-14
lines changed

3 files changed

+15
-14
lines changed

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: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from abc import abstractmethod
44
from dataclasses import dataclass
5-
from typing import Literal, TypedDict, cast
5+
from typing import Literal, TypeAlias, TypedDict, cast
66

77
from zarr.abc.metadata import Metadata
88
from zarr.core.common import (
@@ -20,7 +20,7 @@ def parse_separator(data: JSON) -> SeparatorLiteral:
2020
return cast(SeparatorLiteral, data)
2121

2222

23-
class ChunkKeyEncodingLike(TypedDict):
23+
class ChunkKeyEncodingParams(TypedDict):
2424
name: Literal["v2", "default"]
2525
separator: SeparatorLiteral
2626

@@ -36,9 +36,7 @@ def __init__(self, *, separator: SeparatorLiteral) -> None:
3636
object.__setattr__(self, "separator", separator_parsed)
3737

3838
@classmethod
39-
def from_dict(
40-
cls, data: dict[str, JSON] | ChunkKeyEncoding | ChunkKeyEncodingLike
41-
) -> ChunkKeyEncoding:
39+
def from_dict(cls, data: dict[str, JSON] | ChunkKeyEncodingLike) -> ChunkKeyEncoding:
4240
if isinstance(data, ChunkKeyEncoding):
4341
return data
4442

@@ -73,6 +71,9 @@ def encode_chunk_key(self, chunk_coords: ChunkCoords) -> str:
7371
pass
7472

7573

74+
ChunkKeyEncodingLike: TypeAlias = ChunkKeyEncodingParams | ChunkKeyEncoding
75+
76+
7677
@dataclass(frozen=True)
7778
class DefaultChunkKeyEncoding(ChunkKeyEncoding):
7879
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)