Repository navigation
Invalid "selection" values in "read_zarr()" should raise an error #1172
Description
Activity
I actually think the right fix would be to tighten the type of
selectionto: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
assertthat, since passing anything other thanLiteral["images", "labels", ...]is a violation of the preconditions of the function, and therefore a bug, not a recoverable exception.Reacted by Alberto Fabbri and LucaMarconatoThis whole message is AI-generated. The issue was automatically discovered and reported by an AI agent (Claude) during an autonomous bug hunt on the
spatialdatacode base. It has not been verified or triaged by a human yet; theneeds: triagelabel 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
mainbranch (commitccf1ea048d054b6624214bf618008a9f9ae223e0) 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 resolvesspatialdatafromgit+https://github.com/scverse/spatialdata.git@ccf1ea048d054b6624214bf618008a9f9ae223e0(extra dependencies, if any, are pinned in the script's inline metadata above).Output (verbatim, only
uvpackage-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=0Agent'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.
spatialdata.read_zarr()currently appears to silently ignore invalid values passed to selection.For example:
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
ValueErrorfor unknown entries. Silent failure can otherwise make a valid Zarr store look empty and is difficult to diagnose.