Skip to content

Commit 951560b

Browse files
committed
refactor codecjson types to avoid circular dependency
1 parent d53d153 commit 951560b

File tree

9 files changed

+61
-53
lines changed

9 files changed

+61
-53
lines changed

src/zarr/abc/codec.py

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
from __future__ import annotations
22

33
from abc import abstractmethod
4-
from collections.abc import Mapping
54
from typing import (
65
TYPE_CHECKING,
76
Generic,
87
Literal,
98
Self,
10-
TypedDict,
119
TypeVar,
1210
overload,
1311
)
1412

15-
from typing_extensions import ReadOnly, TypeIs
16-
1713
from zarr.abc.metadata import Metadata
1814
from zarr.core.buffer import Buffer, NDBuffer
19-
from zarr.core.common import NamedConfig, ZarrFormat, concurrent_map
15+
from zarr.core.common import (
16+
CodecJSON_V2,
17+
CodecJSON_V3,
18+
ZarrFormat,
19+
_check_codecjson_v2,
20+
concurrent_map,
21+
)
2022
from zarr.core.config import config
2123

2224
if TYPE_CHECKING:
@@ -46,37 +48,6 @@
4648
CodecOutput = TypeVar("CodecOutput", bound=NDBuffer | Buffer)
4749

4850

49-
class CodecJSON_V2(TypedDict):
50-
"""The JSON representation of a codec for Zarr V2"""
51-
52-
id: ReadOnly[str]
53-
54-
55-
def _check_codecjson_v2(data: object) -> TypeIs[CodecJSON_V2]:
56-
"""
57-
A type narrowing function for the CodecJSON_V2 type
58-
"""
59-
return isinstance(data, Mapping) and "id" in data and isinstance(data["id"], str)
60-
61-
62-
CodecJSON_V3 = str | NamedConfig[str, Mapping[str, object]]
63-
"""The JSON representation of a codec for Zarr V3."""
64-
65-
66-
def _check_codecjson_v3(data: object) -> TypeIs[CodecJSON_V3]:
67-
"""
68-
A type narrowing function for the CodecJSON_V3 type
69-
"""
70-
if isinstance(data, str):
71-
return True
72-
return (
73-
isinstance(data, Mapping)
74-
and "name" in data
75-
and isinstance(data["name"], str)
76-
and isinstance(data.get("configuration", {}), Mapping)
77-
)
78-
79-
8051
# The widest type we will *accept* for a codec JSON
8152
# This covers v2 and v3
8253
CodecJSON = CodecJSON_V2 | CodecJSON_V3

src/zarr/codecs/_v2.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
ArrayBytesCodec,
1313
BytesBytesCodec,
1414
CodecJSON,
15+
)
16+
from zarr.core.common import (
1517
CodecJSON_V2,
1618
CodecJSON_V3,
1719
_check_codecjson_v2,

src/zarr/codecs/numcodecs/_codecs.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,19 @@
4949
ArrayBytesCodec,
5050
BytesBytesCodec,
5151
CodecJSON,
52+
)
53+
from zarr.core.array_spec import ArraySpec
54+
from zarr.core.buffer.cpu import as_numpy_array_wrapper
55+
from zarr.core.common import (
56+
JSON,
5257
CodecJSON_V2,
5358
CodecJSON_V3,
59+
NamedConfig,
60+
NamedRequiredConfig,
61+
ZarrFormat,
5462
_check_codecjson_v2,
63+
product,
5564
)
56-
from zarr.core.array_spec import ArraySpec
57-
from zarr.core.buffer.cpu import as_numpy_array_wrapper
58-
from zarr.core.common import JSON, NamedConfig, NamedRequiredConfig, ZarrFormat, product
5965
from zarr.dtype import UInt8, ZDType, parse_dtype
6066
from zarr.errors import ZarrUserWarning
6167
from zarr.registry import get_numcodec

src/zarr/codecs/sharding.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
ArrayBytesCodecPartialEncodeMixin,
2929
Codec,
3030
CodecJSON,
31-
CodecJSON_V2,
32-
CodecJSON_V3,
3331
CodecPipeline,
3432
)
3533
from zarr.abc.store import (
@@ -52,6 +50,8 @@
5250
from zarr.core.chunk_grids import ChunkGrid, RegularChunkGrid
5351
from zarr.core.common import (
5452
JSON,
53+
CodecJSON_V2,
54+
CodecJSON_V3,
5555
NamedRequiredConfig,
5656
ShapeLike,
5757
ZarrFormat,

src/zarr/core/common.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
)
2323

2424
import numpy as np
25-
from typing_extensions import ReadOnly
25+
from typing_extensions import ReadOnly, TypeIs
2626

2727
from zarr.core.config import config as zarr_config
2828
from zarr.errors import ZarrRuntimeWarning
@@ -88,6 +88,37 @@ class NamedRequiredConfig(TypedDict, Generic[TName, TConfig]):
8888
"""The configuration of the object."""
8989

9090

91+
class CodecJSON_V2(TypedDict):
92+
"""The JSON representation of a codec for Zarr V2"""
93+
94+
id: ReadOnly[str]
95+
96+
97+
def _check_codecjson_v2(data: object) -> TypeIs[CodecJSON_V2]:
98+
"""
99+
A type narrowing function for the CodecJSON_V2 type
100+
"""
101+
return isinstance(data, Mapping) and "id" in data and isinstance(data["id"], str)
102+
103+
104+
CodecJSON_V3 = str | NamedConfig[str, Mapping[str, object]]
105+
"""The JSON representation of a codec for Zarr V3."""
106+
107+
108+
def _check_codecjson_v3(data: object) -> TypeIs[CodecJSON_V3]:
109+
"""
110+
A type narrowing function for the CodecJSON_V3 type
111+
"""
112+
if isinstance(data, str):
113+
return True
114+
return (
115+
isinstance(data, Mapping)
116+
and "name" in data
117+
and isinstance(data["name"], str)
118+
and isinstance(data.get("configuration", {}), Mapping)
119+
)
120+
121+
91122
def product(tup: tuple[int, ...]) -> int:
92123
return functools.reduce(operator.mul, tup, 1)
93124

src/zarr/core/metadata/v2.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@
55
from functools import cached_property
66
from typing import TYPE_CHECKING, Any, TypeAlias, TypedDict, cast
77

8-
from zarr.abc.codec import (
9-
Codec,
10-
CodecJSON_V2,
11-
_check_codecjson_v2,
12-
_check_codecjson_v3,
13-
)
8+
from zarr.abc.codec import Codec
149
from zarr.abc.metadata import Metadata
1510
from zarr.abc.numcodec import Numcodec
1611
from zarr.codecs._v2 import NumcodecWrapper
1712
from zarr.codecs.blosc import BloscCodec
1813
from zarr.core.buffer.core import default_buffer_prototype
1914
from zarr.core.chunk_grids import RegularChunkGrid
15+
from zarr.core.common import (
16+
CodecJSON_V2,
17+
_check_codecjson_v2,
18+
_check_codecjson_v3,
19+
)
2020
from zarr.core.dtype import get_data_type_from_json
2121
from zarr.core.dtype.common import OBJECT_CODEC_IDS
2222
from zarr.errors import ZarrUserWarning

src/zarr/registry.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
from importlib.metadata import entry_points as get_entry_points
77
from typing import TYPE_CHECKING, Any, Generic, TypeVar
88

9-
from zarr.abc.codec import _check_codecjson_v2
109
from zarr.abc.numcodec import Numcodec
10+
from zarr.core.common import CodecJSON_V2, CodecJSON_V3, _check_codecjson_v2
1111
from zarr.core.config import BadConfigError, config
1212
from zarr.core.dtype import data_type_registry
1313
from zarr.errors import ZarrUserWarning
@@ -20,8 +20,6 @@
2020
ArrayBytesCodec,
2121
BytesBytesCodec,
2222
Codec,
23-
CodecJSON_V2,
24-
CodecJSON_V3,
2523
CodecPipeline,
2624
)
2725
from zarr.core.buffer import Buffer, NDBuffer

tests/test_abc/test_codec.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
from zarr.abc.codec import _check_codecjson_v2
3+
from zarr.core.common import _check_codecjson_v2
44

55

66
def test_check_codecjson_v2_valid() -> None:

tests/test_array.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
from .test_dtype.conftest import zdtype_examples
8080

8181
if TYPE_CHECKING:
82-
from zarr.abc.codec import CodecJSON_V3
82+
from zarr.core.common import CodecJSON_V3
8383
from zarr.core.metadata.v3 import ArrayV3Metadata
8484

8585

0 commit comments

Comments
 (0)