Skip to content

Commit 40da541

Browse files
committed
format, docstring, and typing
1 parent e2ff9c9 commit 40da541

2 files changed

Lines changed: 12 additions & 6 deletions

File tree

src/zarr/registry.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
import numcodecs
99

10-
from zarr.abc.codec import Codec
1110
from zarr.core.config import BadConfigError, config
1211

1312
if TYPE_CHECKING:
@@ -17,6 +16,7 @@
1716
ArrayArrayCodec,
1817
ArrayBytesCodec,
1918
BytesBytesCodec,
19+
Codec,
2020
CodecPipeline,
2121
)
2222
from zarr.core.buffer import Buffer, NDBuffer
@@ -168,17 +168,18 @@ def _resolve_codec(data: dict[str, JSON]) -> Codec:
168168
return get_codec_class(data["name"]).from_dict(data) # type: ignore[arg-type]
169169

170170

171-
def numcodec_to_zarr3_codec(codec: numcodecs.abc.Codec) -> Codec:
171+
def numcodec_to_zarr3_codec(codec: numcodecs.abc.Codec) -> numcodecs.zarr3._NumcodecsCodec & Codec:
172+
"""
173+
Convert a numcodecs codec to a zarr v3 compatible numcodecs.zarr3 codec instance.
174+
"""
172175
codec_config = codec.get_config()
173176
codec_name = codec_config.pop("id", None)
174177
if codec_name is None:
175178
raise ValueError(f"Codec configuration does not contain 'id': {codec_config}")
176179
codec_cls = get_codec_class(f"numcodecs.{codec_name}")
177180
if codec_cls is None:
178181
raise ValueError(f"Codec class for 'numcodecs.{codec_name}' not found.")
179-
codec_v3 = codec_cls.from_dict({"name": f"numcodecs.{codec_name}", "configuration": codec_config})
180-
assert isinstance(codec_v3, Codec)
181-
return codec_v3
182+
return codec_cls.from_dict({"name": f"numcodecs.{codec_name}", "configuration": codec_config})
182183

183184

184185
def _parse_bytes_bytes_codec(data: dict[str, JSON] | Codec) -> BytesBytesCodec:

tests/test_codecs/test_codecs.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from zarr.storage import StorePath
2727

2828
if TYPE_CHECKING:
29+
from zarr.abc.codec import Codec
2930
from zarr.abc.store import Store
3031
from zarr.core.array import CompressorsLike, FiltersLike, SerializerLike
3132
from zarr.core.buffer.core import NDArrayLikeOrScalar
@@ -452,7 +453,11 @@ async def test_resize(store: Store) -> None:
452453
(numcodecs.Zstd(), numcodecs.zarr3.Zstd),
453454
],
454455
)
455-
def test_numcodecs_in_v3(store: Store, codec_v2, expected_v3_cls) -> None:
456+
def test_numcodecs_in_v3(
457+
store: Store, codec_v2: numcodecs.abc.Codec, expected_v3_cls: Codec
458+
) -> None:
459+
import zarr.registry
460+
456461
result_v3 = zarr.registry.numcodec_to_zarr3_codec(codec_v2)
457462

458463
assert result_v3.__class__ == expected_v3_cls

0 commit comments

Comments
 (0)