Skip to content

Fix default entrypoint handling on older Pythons #475

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 21 commits into from
Oct 17, 2023
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
2 changes: 1 addition & 1 deletion .github/workflows/ci-linux.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
run: |
conda activate env
export DISABLE_NUMCODECS_AVX2=""
python -m pip install -v -e .[test,msgpack,zfpy]
python -m pip install -v -e .[test,test_extras,msgpack,zfpy]

- name: List installed packages
shell: "bash -l {0}"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci-osx.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
run: |
conda activate env
export DISABLE_NUMCODECS_AVX2=""
python -m pip install -v -e .[test,msgpack,zfpy]
python -m pip install -v -e .[test,test_extras,msgpack,zfpy]

- name: List installed packages
shell: "bash -l {0}"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci-windows.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
shell: "bash -l {0}"
run: |
conda activate env
python -m pip install -v -e .[test,msgpack,zfpy]
python -m pip install -v -e .[test,test_extras,msgpack,zfpy]

- name: List installed packages
shell: "bash -l {0}"
Expand Down
2 changes: 2 additions & 0 deletions docs/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ Fix

* ``Codec`` is now derived from ``abc.ABC``
By :user:`Mads R. B. Kristensen <madsbk>`, :issue:`472`.
* Fix handling of entry points on older Python versions where ``importlib_metadata`` compatibility is concerned
By :user:`Vyas Ramasubramani <vyasr>`, :issue:`478`.
* Make shuffle pyx functions ``noexcept``
By :user:`Martin Durant <martindurant>`, :issue:`477`.

Expand Down
2 changes: 1 addition & 1 deletion numcodecs/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def run_entrypoints():
entries.update({e.name: e for e in eps.select(group="numcodecs.codecs")})
else:
# Otherwise, fallback to using get
entries.update(eps.get("numcodecs.codecs", []))
entries.update({e.name: e for e in eps.get("numcodecs.codecs", [])})


run_entrypoints()
Expand Down
3 changes: 2 additions & 1 deletion numcodecs/tests/test_entrypoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def set_path():
numcodecs.registry.codec_registry.pop("test")


def test_entrypoint_codec(set_path):
@pytest.mark.usefixtures("set_path")
def test_entrypoint_codec():
cls = numcodecs.registry.get_codec({"id": "test"})
assert cls.codec_id == "test"
32 changes: 32 additions & 0 deletions numcodecs/tests/test_entrypoints_backport.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import os.path
import pkgutil
import sys

import pytest

from multiprocessing import Process

import numcodecs.registry

if not pkgutil.find_loader("importlib_metadata"): # pragma: no cover
pytest.skip("This test module requires importlib_metadata to be installed")

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


def get_entrypoints_with_importlib_metadata_loaded():
# importlib_metadata patches importlib.metadata, which can lead to breaking changes
# to the APIs of EntryPoint objects used when registering entrypoints. Attempt to
# isolate those changes to just this test.
import importlib_metadata # noqa: F401
sys.path.append(here)
numcodecs.registry.run_entrypoints()
cls = numcodecs.registry.get_codec({"id": "test"})
assert cls.codec_id == "test"
Comment on lines +22 to +25
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to reuse set_path here somehow? Should cleanup sys.path and the numcodecs.registry after?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about reusing, but I think it'd be more work than it's worth. We can't use a fixture directly because it's not supposed to affect the actual test function, we need it to affect the helper function that's run in the subprocess and that function isn't a test so pytest won't patch in fixtures. We could try and split it into a helper function like I had initially, but it's not really necessary. Since the function is running in a subprocess we don't need to clean things up, it shouldn't affect the modules in the parent process.



def test_entrypoint_codec_with_importlib_metadata():
p = Process(target=get_entrypoints_with_importlib_metadata_loaded)
p.start()
p.join()
assert p.exitcode == 0
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ test = [
"pytest",
"pytest-cov",
]
test_extras = [
"importlib_metadata",
]
msgpack = [
"msgpack",
]
Expand Down