Skip to content

Commit

Permalink
Only catch validation errors
Browse files Browse the repository at this point in the history
  • Loading branch information
dstansby committed Jan 7, 2025
1 parent 7623a49 commit c39899a
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 20 deletions.
3 changes: 2 additions & 1 deletion src/ome_zarr_models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from importlib.metadata import PackageNotFoundError, version

import zarr
from pydantic import ValidationError

import ome_zarr_models.v04.hcs
import ome_zarr_models.v04.image
Expand Down Expand Up @@ -37,7 +38,7 @@ def load_ome_zarr_group(group: zarr.Group) -> BaseGroup:
for group_cls in _V04_groups:
try:
return group_cls.from_zarr(group)
except Exception:
except ValidationError:
continue

raise RuntimeError(f"Could not find any matches for group {group}")
14 changes: 1 addition & 13 deletions src/ome_zarr_models/v04/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from pydantic import Field, model_validator
from pydantic_zarr.v2 import ArraySpec, GroupSpec

from ome_zarr_models._utils import get_store_path
from ome_zarr_models.base import BaseAttrs
from ome_zarr_models.v04.base import BaseGroupv04
from ome_zarr_models.v04.labels import Labels
Expand Down Expand Up @@ -95,18 +94,7 @@ def from_zarr(cls, group: zarr.Group) -> Self:
# on unlistable storage backends, the members of this group will be {}
guess = GroupSpec.from_zarr(group, depth=0)

try:
multi_meta_maybe = guess.attributes["multiscales"]
except KeyError as e:
store_path = get_store_path(group.store)
msg = (
"Failed to find mandatory `multiscales` key in the attributes of the "
"Zarr group at "
f"{group.store}://{store_path}://{group.path}."
)
raise KeyError(msg) from e

multi_meta = ImageAttrs(multiscales=multi_meta_maybe)
multi_meta = ImageAttrs.model_validate(guess.attributes)
members_tree_flat = {}
for multiscale in multi_meta.multiscales:
for dataset in multiscale.datasets:
Expand Down
8 changes: 2 additions & 6 deletions tests/v04/test_multiscales.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,13 +492,9 @@ def test_from_zarr_missing_metadata(
) -> None:
group_model = GroupSpec()
group = group_model.to_zarr(store, path="test")
store_path = ""
# store_path = store.path if hasattr(store, "path") else ""
match = (
"Failed to find mandatory `multiscales` key in the attributes of the "
f"Zarr group at {store}://{store_path}://{group.path}."
)
with pytest.raises(KeyError, match=match):
match = "multiscales\n Field required"
with pytest.raises(ValidationError, match=match):
Image.from_zarr(group)


Expand Down

0 comments on commit c39899a

Please sign in to comment.