Skip to content

Commit d7d4e02

Browse files
committed
bring in update codec abc
1 parent 1e23a91 commit d7d4e02

File tree

1 file changed

+45
-12
lines changed

1 file changed

+45
-12
lines changed

src/zarr/abc/codec.py

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,20 @@
22

33
from abc import abstractmethod
44
from collections.abc import Mapping
5-
from typing import TYPE_CHECKING, Generic, TypeGuard, TypeVar
5+
from typing import (
6+
TYPE_CHECKING,
7+
Generic,
8+
Literal,
9+
TypedDict,
10+
TypeVar,
11+
overload,
12+
)
613

7-
from typing_extensions import ReadOnly, TypedDict
14+
from typing_extensions import ReadOnly
815

916
from zarr.abc.metadata import Metadata
1017
from zarr.core.buffer import Buffer, NDBuffer
11-
from zarr.core.common import ChunkCoords, NamedConfig, concurrent_map
18+
from zarr.core.common import ChunkCoords, NamedConfig, ZarrFormat, concurrent_map
1219
from zarr.core.config import config
1320

1421
if TYPE_CHECKING:
@@ -41,22 +48,16 @@
4148

4249

4350
class CodecJSON_V2(TypedDict, Generic[TName]):
44-
"""The JSON representation of a codec for Zarr V2"""
45-
4651
id: ReadOnly[TName]
4752

4853

49-
def _check_codecjson_v2(data: object) -> TypeGuard[CodecJSON_V2[str]]:
50-
return isinstance(data, Mapping) and "id" in data and isinstance(data["id"], str)
51-
54+
CodecConfig_V3 = NamedConfig[str, Mapping[str, object]]
5255

53-
CodecJSON_V3 = str | NamedConfig[str, Mapping[str, object]]
54-
"""The JSON representation of a codec for Zarr V3."""
56+
CodecJSON_V3 = str | CodecConfig_V3
5557

56-
# The widest type we will *accept* for a codec JSON
58+
# The widest type we will accept for a codec JSON
5759
# This covers v2 and v3
5860
CodecJSON = str | Mapping[str, object]
59-
"""The widest type of JSON-like input that could specify a codec."""
6061

6162

6263
class BaseCodec(Metadata, Generic[CodecInput, CodecOutput]):
@@ -181,6 +182,34 @@ async def encode(
181182
"""
182183
return await _batching_helper(self._encode_single, chunks_and_specs)
183184

185+
@overload
186+
def to_json(self, zarr_format: Literal[2]) -> CodecJSON_V2[str]: ...
187+
@overload
188+
def to_json(self, zarr_format: Literal[3]) -> NamedConfig[str, Mapping[str, object]]: ...
189+
190+
def to_json(
191+
self, zarr_format: ZarrFormat
192+
) -> CodecJSON_V2[str] | NamedConfig[str, Mapping[str, object]]:
193+
raise NotImplementedError
194+
195+
@classmethod
196+
def _from_json_v2(cls, data: CodecJSON) -> Self:
197+
raise NotImplementedError
198+
199+
@classmethod
200+
def _from_json_v3(cls, data: CodecJSON) -> Self:
201+
raise NotImplementedError
202+
203+
@classmethod
204+
def from_json(cls, data: CodecJSON, zarr_format: ZarrFormat) -> Self:
205+
if zarr_format == 2:
206+
return cls._from_json_v2(data)
207+
elif zarr_format == 3:
208+
return cls._from_json_v3(data)
209+
raise ValueError(
210+
f"Unsupported Zarr format {zarr_format}. Expected 2 or 3."
211+
) # pragma: no cover
212+
184213

185214
class ArrayArrayCodec(BaseCodec[NDBuffer, NDBuffer]):
186215
"""Base class for array-to-array codecs."""
@@ -471,3 +500,7 @@ async def wrap(chunk: CodecInput | None, chunk_spec: ArraySpec) -> CodecOutput |
471500
return await func(chunk, chunk_spec)
472501

473502
return wrap
503+
504+
505+
# Raised when a codec JSON data is invalid
506+
class CodecValidationError(ValueError): ...

0 commit comments

Comments
 (0)