Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@tomwhite
Copy link
Contributor

This avoids the need to import zarrs, which allows zarrs-python to be enabled purely through configuration.

Copy link
Collaborator

@ilan-gold ilan-gold left a comment

Choose a reason for hiding this comment

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

@tomwhite Just out of curiosity, how did you find this? An extrapolation of https://zarr.readthedocs.io/en/stable/user-guide/extending.html#custom-codecs to pipelines?

I would also like to test this. I'm not sure how, but I think a good start would be changing the test_pipeline.py to not have the import zarrs as well as removing the imports from zarrs in conftest.py (which don't appear to be serving a purpose?). Does that sound reasonable? For me on main removing these causes things to fail as expected but not with your branch i.e., this mechanism is working.

@tomwhite
Copy link
Contributor Author

@tomwhite Just out of curiosity, how did you find this? An extrapolation of https://zarr.readthedocs.io/en/stable/user-guide/extending.html#custom-codecs to pipelines?

Yes, and I saw that it was supported in the zarr-python source at https://github.com/zarr-developers/zarr-python/blob/main/src/zarr/registry.py#L113.

I would also like to test this. I'm not sure how, but I think a good start would be changing the test_pipeline.py to not have the import zarrs as well as removing the imports from zarrs in conftest.py (which don't appear to be serving a purpose?). Does that sound reasonable? For me on main removing these causes things to fail as expected but not with your branch i.e., this mechanism is working.

Absolutely. I had manually tested it using another project that depends on zarrs-python, and removing import zarrs worked fine with this change (but not without it). I have updated this PR to remove the imports from zarrs in conftest.py, but I've left the one in test_pipeline.py as it is used here: https://github.com/zarrs/zarrs-python/blob/main/tests/test_pipeline.py#L279.

@flying-sheep
Copy link
Collaborator

flying-sheep commented Sep 30, 2025

The way we test critical stuff like this in scanpy is by precomputing the outcomes at import time of the tests, then testing the data, somthing like the following.

import sys

import zarr

# IMPORTED_MODULES must be determined first
IMPORTED_MODULES = list(sys.modules)
with zarr.config.set({"codec_pipeline.path": "zarrs.ZarrsCodecPipeline"}):
    try:
        zarr.array([])
    except zarr.core.config.BadConfigError:
        IS_REGISTERED = False
    else:
        IS_REGISTERED = True


def test_registerable_when_not_imported():
   assert "zarrs" not in IMPORTED_MODULES
   assert IS_REGISTERED

caveat: there are many ways one can accidentally import a module, so a clean subprocess-based approach could also work (if we do async testing here, it wouldn’t even slow us down as we could run things async):

data/check-registry.py

import json

# imported_modules must be determined first
imported_modules = list(sys.modules)
with zarr.config.set({"codec_pipeline.path": "zarrs.ZarrsCodecPipeline"}):
    try:
        zarr.array([])
    except zarr.core.config.BadConfigError:
        is_registered = False
    else:
        is_registered = True

print(json.dumps(dict(
    imported_modules=imported_modules,
    is_registered=is_registered,
)))

test_registry.py

import sys
import json
from subprocess import run
from pathlib import Path

HERE = Path(__file__).parent

def test_registerable_when_not_imported():
    proc = run([sys.executable, "-I", HERE / "data/check-registry.py"], check=True)
    results = json.loads(proc.stdout)
    assert "zarrs" not in results["imported_modules"]
    assert result["is_registered"]

@tomwhite
Copy link
Contributor Author

Thanks @flying-sheep! I've added the test you suggested.

@flying-sheep
Copy link
Collaborator

Thanks! Also neat, looks like I only missed a capture_output=True in the code I typed into the GitHub comment box haha.

@flying-sheep
Copy link
Collaborator

flying-sheep commented Oct 1, 2025

@LDeakin could you please install the pre-commit.ci GitHub app so we get autofixes?

I think it needs to be installed by an org admin, so only you can do it.

@LDeakin
Copy link
Member

LDeakin commented Oct 1, 2025

@tomwhite Nice find!

@flying-sheep - pre-commit CI is now enabled

@LDeakin LDeakin enabled auto-merge (squash) October 1, 2025 11:53
@LDeakin LDeakin merged commit a572afe into zarrs:main Oct 1, 2025
26 of 29 checks passed
tomwhite added a commit to cubed-dev/cubed that referenced this pull request Oct 27, 2025
tomwhite added a commit to cubed-dev/cubed that referenced this pull request Oct 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants