Skip to content

Test codec entrypoints #1835

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions src/zarr/codecs/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,11 @@
__lazy_load_codecs: Dict[str, EntryPoint] = {}


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


def register_codec(key: str, codec_cls: Type[Codec]) -> None:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[zarr.codecs]
test = package_with_entrypoint:TestCodec
27 changes: 27 additions & 0 deletions tests/v3/package_with_entrypoint/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from numpy import ndarray
from zarr.abc.codec import ArrayBytesCodec
from zarr.common import ArraySpec, BytesLike
from zarr.config import RuntimeConfiguration


class TestCodec(ArrayBytesCodec):
is_fixed_size = True

async def encode(
self,
chunk_array: ndarray,
chunk_spec: ArraySpec,
runtime_configuration: RuntimeConfiguration,
) -> BytesLike | None:
pass

async def decode(
self,
chunk_bytes: BytesLike,
chunk_spec: ArraySpec,
runtime_configuration: RuntimeConfiguration,
) -> ndarray:
pass

def compute_encoded_size(self, input_byte_length: int, chunk_spec: ArraySpec) -> int:
return input_byte_length
25 changes: 25 additions & 0 deletions tests/v3/test_codec_entrypoints.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import os.path
import sys

import pytest

import zarr.codecs.registry


here = os.path.abspath(os.path.dirname(__file__))


@pytest.fixture()
def set_path():
sys.path.append(here)
zarr.codecs.registry._collect_entrypoints()
yield
sys.path.remove(here)
entry_points = zarr.codecs.registry._collect_entrypoints()
entry_points.pop("test")


@pytest.mark.usefixtures("set_path")
def test_entrypoint_codec():
cls = zarr.codecs.registry.get_codec_class("test")
assert cls.__name__ == "TestCodec"
Loading