Skip to content

Invalid "selection" values in "read_zarr()" should raise an error #1172

Description

@AlbertoFabbri93

spatialdata.read_zarr() currently appears to silently ignore invalid values passed to selection.

For example:

sdata = spatialdata.read_zarr(
    path,
    selection=("table",),
)

returns a SpatialData object with no loaded tables instead of indicating that "table" is not a valid selector.

It would be safer if read_zarr() validated selection against the supported values:

{"images", "labels", "points", "shapes", "tables"}

and raised a clear ValueError for unknown entries. Silent failure can otherwise make a valid Zarr store look empty and is difficult to diagnose.

Activity

  1. Tomaz-Vieira commented on Aug 5, 2026

    @Tomaz-Vieira
    Collaborator

    I actually think the right fix would be to tighten the type of selection to:

    Tuple[Literal["images", "labels", "shapes", "points", "tables"], ...] | None

    so we can catch these errors statically. If we want to be extremely nice towards people not using a type checker, we could also assert that, since passing anything other than Literal["images", "labels", ...] is a violation of the preconditions of the function, and therefore a bug, not a recoverable exception.

  2. LucaMarconato commented on Sep 13, 2026

    @LucaMarconato
    Member

    This whole message is AI-generated. The issue was automatically discovered and reported by an AI agent (Claude) during an autonomous bug hunt on the spatialdata code base. It has not been verified or triaged by a human yet; the needs: triage label is set so that a maintainer can confirm it. The reproduction script below was executed by the agent in an isolated environment (see Environment) and its output is pasted verbatim.

    I re-attempted to reproduce this issue on the current main branch (commit ccf1ea048d054b6624214bf618008a9f9ae223e0) as part of a triage pass over open issues, and confirmed it reproduces.

    Reproduction script (PEP 723 inline metadata; run with uv run repro.py, no other setup needed):

    # /// script
    # requires-python = ">=3.12"
    # dependencies = [
    #     "spatialdata @ git+https://github.com/scverse/spatialdata.git@ccf1ea048d054b6624214bf618008a9f9ae223e0",
    # ]
    # ///
    """Issue #1172: invalid `selection` values passed to `read_zarr()` are silently ignored."""
    import tempfile
    from pathlib import Path
    
    import numpy as np
    
    from spatialdata import SpatialData, read_zarr
    from spatialdata.models import Image2DModel
    
    path = Path(tempfile.mkdtemp()) / "s.zarr"
    SpatialData(images={"img": Image2DModel.parse(np.zeros((1, 8, 8), dtype=np.uint8))}).write(path)
    for sel in (("table",), ("imagez",), "images"):
        try:
            sd = read_zarr(path, selection=sel)
            print(f"selection={sel!r:12} -> loaded elements: {[n for _, n, _ in sd.gen_elements()]}")
        except Exception as e:  # noqa: BLE001
            print(f"selection={sel!r:12} -> {type(e).__name__}: {str(e)[:100]}")
    sd = read_zarr(path, selection=("table",))
    if not list(sd.gen_elements()):
        print("VERDICT: REPRODUCED (invalid selection values are silently ignored and an empty object is returned)")
    else:
        print("VERDICT: NOT REPRODUCED")

    Environment: uv run --no-project --python 3.13, which resolves spatialdata from git+https://github.com/scverse/spatialdata.git@ccf1ea048d054b6624214bf618008a9f9ae223e0 (extra dependencies, if any, are pinned in the script's inline metadata above).

    Output (verbatim, only uv package-resolution/install log lines removed):

    selection=('table',)   -> loaded elements: []
    selection=('imagez',)  -> loaded elements: []
    selection='images'     -> loaded elements: []
    VERDICT: REPRODUCED (invalid selection values are silently ignored and an empty object is returned)
    exit=0
    

    Agent's severity assessment: low (see the priority: * label added to this issue).


    Automatically generated; discovered by an AI agent (Claude) and not yet reviewed by a human.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions