Skip to content

Commit 172e01f

Browse files
committed
ensure forward compatibility for older versions of zarr python 3.x
1 parent e72f905 commit 172e01f

17 files changed

+159
-33
lines changed

src/zarr/codecs/numcodecs/_codecs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def to_json(self, zarr_format: ZarrFormat) -> CodecJSON_V2 | CodecJSON_V3:
121121
if zarr_format == 2:
122122
return {"id": codec_id, **codec_config} # type: ignore[return-value, typeddict-item]
123123
else:
124-
return {"name": codec_id, "configuration": codec_config}
124+
return {"name": self.codec_name, "configuration": codec_config}
125125

126126

127127
class _NumcodecsBytesBytesCodec(_NumcodecsCodec, BytesBytesCodec):

src/zarr/codecs/numcodecs/astype.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,19 @@ class AsType(_NumcodecsArrayArrayCodec):
100100
@overload
101101
def to_json(self, zarr_format: Literal[2]) -> AsTypeJSON_V2: ...
102102
@overload
103-
def to_json(self, zarr_format: Literal[3]) -> AsTypeJSON_V3: ...
104-
def to_json(self, zarr_format: ZarrFormat) -> AsTypeJSON_V2 | AsTypeJSON_V3:
103+
def to_json(self, zarr_format: Literal[3]) -> AsTypeJSON_V3_Legacy: ...
104+
def to_json(self, zarr_format: ZarrFormat) -> AsTypeJSON_V2 | AsTypeJSON_V3_Legacy:
105105
_warn_unstable_specification(self)
106-
return super().to_json(zarr_format) # type: ignore[return-value]
106+
if zarr_format == 2:
107+
return super().to_json(zarr_format) # type: ignore[return-value]
108+
# For v3, we need to convert dtype format
109+
conf = self.codec_config
110+
encode_dtype_v3 = parse_dtype(conf["encode_dtype"], zarr_format=2).to_json(zarr_format=3)
111+
decode_dtype_v3 = parse_dtype(conf["decode_dtype"], zarr_format=2).to_json(zarr_format=3)
112+
return {
113+
"name": "numcodecs.astype",
114+
"configuration": {"encode_dtype": encode_dtype_v3, "decode_dtype": decode_dtype_v3},
115+
}
107116

108117
def resolve_metadata(self, chunk_spec: ArraySpec) -> ArraySpec:
109118
dtype = parse_dtype(self.codec_config["encode_dtype"], zarr_format=3)

src/zarr/codecs/numcodecs/crc32c.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def check_json_v3(data: object) -> TypeGuard[Crc32cJSON_V3 | Crc32cJSON_V3_Legac
4141
"""
4242
A type guard for the Zarr V3 form of the CRC32C codec JSON
4343
"""
44-
if data == "crc32c":
44+
if data in ("crc32c", "numcodecs.crc32c"):
4545
return True
4646
return (
4747
_check_codecjson_v3(data)
@@ -77,7 +77,7 @@ def _from_json_v2(cls, data: CodecJSON_V2) -> Self:
7777
@classmethod
7878
def _from_json_v3(cls, data: CodecJSON_V3) -> Self:
7979
if check_json_v3(data):
80-
if data == "crc32c":
80+
if data in ("crc32c", "numcodecs.crc32c"):
8181
return cls()
8282
config = data.get("configuration", {})
8383
return cls(**config)

src/zarr/codecs/numcodecs/delta.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def check_json_v2(data: object) -> TypeGuard[DeltaJSON_V2 | DeltaJSON_V3_Legacy]
6565
)
6666

6767

68-
def check_json_v3(data: object) -> TypeGuard[DeltaJSON_V3]:
68+
def check_json_v3(data: object) -> TypeGuard[DeltaJSON_V3 | DeltaJSON_V3_Legacy]:
6969
"""
7070
A type guard for the Zarr V3 form of the Delta codec JSON
7171
"""
@@ -120,16 +120,16 @@ def from_json(cls, data: CodecJSON) -> Self:
120120
@overload
121121
def to_json(self, zarr_format: Literal[2]) -> DeltaJSON_V2: ...
122122
@overload
123-
def to_json(self, zarr_format: Literal[3]) -> DeltaJSON_V3: ...
124-
def to_json(self, zarr_format: ZarrFormat) -> DeltaJSON_V2 | DeltaJSON_V3:
123+
def to_json(self, zarr_format: Literal[3]) -> DeltaJSON_V3_Legacy: ...
124+
def to_json(self, zarr_format: ZarrFormat) -> DeltaJSON_V2 | DeltaJSON_V3_Legacy:
125125
_warn_unstable_specification(self)
126126
if zarr_format == 2:
127127
return self.codec_config
128128
conf = self.codec_config
129129
astype_v3 = parse_dtype(conf["astype"], zarr_format=2).to_json(zarr_format=3)
130130
dtype_v3 = parse_dtype(conf["dtype"], zarr_format=2).to_json(zarr_format=3)
131131
return {
132-
"name": "delta",
132+
"name": self.codec_name,
133133
"configuration": {"astype": astype_v3, "dtype": dtype_v3},
134134
}
135135

src/zarr/codecs/numcodecs/fixed_scale_offset.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class FixedScaleOffsetJSON_V3_Legacy(
5151

5252

5353
class FixedScaleOffsetJSON_V3(
54-
NamedRequiredConfig[Literal["fixedscaleoffset"], FixedScaleOffsetConfig_V3]
54+
NamedRequiredConfig[Literal["numcodecs.fixedscaleoffset"], FixedScaleOffsetConfig_V3]
5555
):
5656
"""JSON representation of FixedScaleOffset codec for Zarr V3."""
5757

@@ -118,7 +118,7 @@ def to_json(self, zarr_format: ZarrFormat) -> FixedScaleOffsetJSON_V2 | FixedSca
118118
if zarr_format == 2:
119119
return super().to_json(zarr_format) # type: ignore[return-value]
120120
return {
121-
"name": "fixedscaleoffset",
121+
"name": "numcodecs.fixedscaleoffset",
122122
"configuration": {
123123
"astype": parse_dtype(self.codec_config["astype"], zarr_format=2).to_json(
124124
zarr_format=3

src/zarr/codecs/numcodecs/fletcher32.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def check_json_v2(data: object) -> TypeGuard[Fletcher32JSON_V2]:
4747
return _check_codecjson_v2(data) and data["id"] == "fletcher32"
4848

4949

50-
def check_json_v3(data: object) -> TypeGuard[Fletcher32JSON_V3]:
50+
def check_json_v3(data: object) -> TypeGuard[Fletcher32JSON_V3 | Fletcher32JSON_V3_Legacy]:
5151
"""
5252
A type guard for the Zarr V3 form of the Fletcher32 codec JSON
5353
"""

src/zarr/codecs/numcodecs/pcodec.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ class PCodecJSON_V2(PCodecConfig):
3333
id: ReadOnly[Literal["pcodec"]]
3434

3535

36+
class PCodecJSON_V3_Legacy(NamedRequiredConfig[Literal["numcodecs.pcodec"], PCodecConfig]):
37+
"""Legacy JSON representation of PCodec codec for Zarr V3."""
38+
39+
3640
class PCodecJSON_V3(NamedRequiredConfig[Literal["pcodec"], PCodecConfig]):
3741
"""JSON representation of PCodec codec for Zarr V3."""
3842

@@ -49,14 +53,14 @@ def check_json_v2(data: object) -> TypeGuard[PCodecJSON_V2]:
4953
)
5054

5155

52-
def check_json_v3(data: object) -> TypeGuard[PCodecJSON_V3]:
56+
def check_json_v3(data: object) -> TypeGuard[PCodecJSON_V3 | PCodecJSON_V3_Legacy]:
5357
"""
5458
A type guard for the Zarr V3 form of the PCodec codec JSON
5559
"""
5660
return (
5761
_check_codecjson_v3(data)
5862
and isinstance(data, Mapping)
59-
and data["name"] == "pcodec"
63+
and data["name"] in ("pcodec", "numcodecs.pcodec")
6064
and (
6165
"configuration" not in data
6266
or (

src/zarr/codecs/numcodecs/quantize.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def check_json_v2(data: object) -> TypeGuard[QuantizeJSON_V2]:
5656
)
5757

5858

59-
def check_json_v3(data: object) -> TypeGuard[QuantizeJSON_V3]:
59+
def check_json_v3(data: object) -> TypeGuard[QuantizeJSON_V3 | QuantizeJSON_V3_Legacy]:
6060
"""
6161
A type guard for the Zarr V3 form of the Quantize codec JSON
6262
"""

src/zarr/codecs/numcodecs/shuffle.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def check_json_v2(data: object) -> TypeGuard[ShuffleJSON_V2]:
5454
)
5555

5656

57-
def check_json_v3(data: object) -> TypeGuard[ShuffleJSON_V3]:
57+
def check_json_v3(data: object) -> TypeGuard[ShuffleJSON_V3 | ShuffleJSON_V3_Legacy]:
5858
"""
5959
A type guard for the Zarr V3 form of the Shuffle codec JSON
6060
"""

src/zarr/codecs/numcodecs/zfpy.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ class ZFPYJSON_V2(ZFPYConfig):
3535
id: ReadOnly[Literal["zfpy"]]
3636

3737

38+
class ZFPYJSON_V3_Legacy(NamedRequiredConfig[Literal["numcodecs.zfpy"], ZFPYConfig]):
39+
"""Legacy JSON representation of ZFPY codec for Zarr V3."""
40+
41+
3842
class ZFPYJSON_V3(NamedRequiredConfig[Literal["zfpy"], ZFPYConfig]):
3943
"""JSON representation of ZFPY codec for Zarr V3."""
4044

@@ -53,14 +57,14 @@ def check_json_v2(data: object) -> TypeGuard[ZFPYJSON_V2]:
5357
)
5458

5559

56-
def check_json_v3(data: object) -> TypeGuard[ZFPYJSON_V3]:
60+
def check_json_v3(data: object) -> TypeGuard[ZFPYJSON_V3 | ZFPYJSON_V3_Legacy]:
5761
"""
5862
A type guard for the Zarr V3 form of the ZFPY codec JSON
5963
"""
6064
return (
6165
_check_codecjson_v3(data)
6266
and isinstance(data, Mapping)
63-
and data["name"] == "zfpy"
67+
and data["name"] in ("zfpy", "numcodecs.zfpy")
6468
and (
6569
"configuration" not in data
6670
or (

0 commit comments

Comments
 (0)