Skip to content

Commit

Permalink
Remove use of entrypoints (#442)
Browse files Browse the repository at this point in the history
Since Python 3.8, the standard library has included functionality to
query entry points directly using importlib.metadata. Since the API has
changed for the better with Python 3.10, we need to support both ways of
using it.
  • Loading branch information
s-t-e-v-e-n-k authored Aug 11, 2023
1 parent d667b31 commit cb15543
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
14 changes: 9 additions & 5 deletions numcodecs/registry.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
"""The registry module provides some simple convenience functions to enable
applications to dynamically register and look-up codec classes."""
from importlib.metadata import entry_points
import logging
from contextlib import suppress

logger = logging.getLogger("numcodecs")
codec_registry = {}
entries = {}


def run_entrypoints():
import entrypoints
entries.clear()
entries.update(entrypoints.get_group_named("numcodecs.codecs"))
eps = entry_points()
if hasattr(eps, 'select'):
# If entry_points() has a select method, use that. Python 3.10+
entries.update(eps.select(group="numcodecs.codecs"))
else:
# Otherwise, fallback to using get
entries.update(eps.get("numcodecs.codecs", []))


with suppress(ImportError):
run_entrypoints()
run_entrypoints()


def get_codec(config):
Expand Down
2 changes: 0 additions & 2 deletions numcodecs/tests/test_entrypoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@


here = os.path.abspath(os.path.dirname(__file__))
pytest.importorskip("entrypoints")


@pytest.fixture()
Expand All @@ -20,7 +19,6 @@ def set_path():
numcodecs.registry.codec_registry.pop("test")


@pytest.mark.xfail(reason="FIXME: not working in wheels build")
def test_entrypoint_codec(set_path):
cls = numcodecs.registry.get_codec({"id": "test"})
assert cls.codec_id == "test"
7 changes: 6 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ in data storage and communication applications.
"""
readme = "README.rst"
dependencies = [
"entrypoints",
"numpy>=1.7",
]
requires-python = ">=3.8"
Expand Down Expand Up @@ -71,6 +70,12 @@ package-dir = {"" = "."}
packages = ["numcodecs", "numcodecs.tests"]
zip-safe = false

[tool.setuptools.package-data]
numcodecs = [
"tests/package_with_entrypoint/__init__.py",
"tests/package_with_entrypoint-0.1.dist-info/entry_points.txt"
]

[tool.setuptools_scm]
version_scheme = "guess-next-dev"
local_scheme = "dirty-tag"
Expand Down

0 comments on commit cb15543

Please sign in to comment.