Skip to content

Commit fcab650

Browse files
authored
adds test for codec entrypoint (#1835)
1 parent 3499acb commit fcab650

File tree

4 files changed

+58
-9
lines changed

4 files changed

+58
-9
lines changed

src/zarr/codecs/registry.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,11 @@
1212
__lazy_load_codecs: Dict[str, EntryPoint] = {}
1313

1414

15-
def _collect_entrypoints() -> None:
15+
def _collect_entrypoints() -> Dict[str, EntryPoint]:
1616
entry_points = get_entry_points()
17-
if hasattr(entry_points, "select"):
18-
# If entry_points() has a select method, use that. Python 3.10+
19-
for e in entry_points.select(group="zarr.codecs"):
20-
__lazy_load_codecs[e.name] = e
21-
else:
22-
# Otherwise, fallback to using get
23-
for e in entry_points.get("zarr.codecs", []):
24-
__lazy_load_codecs[e.name] = e
17+
for e in entry_points.select(group="zarr.codecs"):
18+
__lazy_load_codecs[e.name] = e
19+
return __lazy_load_codecs
2520

2621

2722
def register_codec(key: str, codec_cls: Type[Codec]) -> None:
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[zarr.codecs]
2+
test = package_with_entrypoint:TestCodec
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from numpy import ndarray
2+
from zarr.abc.codec import ArrayBytesCodec
3+
from zarr.common import ArraySpec, BytesLike
4+
from zarr.config import RuntimeConfiguration
5+
6+
7+
class TestCodec(ArrayBytesCodec):
8+
is_fixed_size = True
9+
10+
async def encode(
11+
self,
12+
chunk_array: ndarray,
13+
chunk_spec: ArraySpec,
14+
runtime_configuration: RuntimeConfiguration,
15+
) -> BytesLike | None:
16+
pass
17+
18+
async def decode(
19+
self,
20+
chunk_bytes: BytesLike,
21+
chunk_spec: ArraySpec,
22+
runtime_configuration: RuntimeConfiguration,
23+
) -> ndarray:
24+
pass
25+
26+
def compute_encoded_size(self, input_byte_length: int, chunk_spec: ArraySpec) -> int:
27+
return input_byte_length

tests/v3/test_codec_entrypoints.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import os.path
2+
import sys
3+
4+
import pytest
5+
6+
import zarr.codecs.registry
7+
8+
9+
here = os.path.abspath(os.path.dirname(__file__))
10+
11+
12+
@pytest.fixture()
13+
def set_path():
14+
sys.path.append(here)
15+
zarr.codecs.registry._collect_entrypoints()
16+
yield
17+
sys.path.remove(here)
18+
entry_points = zarr.codecs.registry._collect_entrypoints()
19+
entry_points.pop("test")
20+
21+
22+
@pytest.mark.usefixtures("set_path")
23+
def test_entrypoint_codec():
24+
cls = zarr.codecs.registry.get_codec_class("test")
25+
assert cls.__name__ == "TestCodec"

0 commit comments

Comments
 (0)