Skip to content

Commit

Permalink
robuster
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAugspurger committed Oct 8, 2024
1 parent 9be5ef1 commit 3878f24
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/zarr/api/asynchronous.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from zarr.core.group import AsyncGroup
from zarr.core.metadata.v2 import ArrayV2Metadata
from zarr.core.metadata.v3 import ArrayV3Metadata
from zarr.errors import NodeTypeValidationError
from zarr.storage import (
StoreLike,
StorePath,
Expand Down Expand Up @@ -247,9 +248,9 @@ async def open(

try:
return await open_array(store=store_path, zarr_format=zarr_format, **kwargs)
except (KeyError, ValueError):
except (KeyError, NodeTypeValidationError):
# KeyError for a missing key
# ValueError for failing to parse node metadata as an array when it's
# NodeTypeValidationError for failing to parse node metadata as an array when it's
# actually a group
return await open_group(store=store_path, zarr_format=zarr_format, **kwargs)

Expand Down
5 changes: 3 additions & 2 deletions src/zarr/core/metadata/v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from zarr.core.common import ZARR_JSON, parse_named_configuration, parse_shapelike
from zarr.core.config import config
from zarr.core.metadata.common import ArrayMetadata, parse_attributes
from zarr.errors import MetadataValidationError, NodeTypeValidationError
from zarr.registry import get_codec_class

DEFAULT_DTYPE = "float64"
Expand All @@ -36,13 +37,13 @@
def parse_zarr_format(data: object) -> Literal[3]:
if data == 3:
return 3
raise ValueError(f"Invalid value. Expected 3. Got {data}.")
raise MetadataValidationError(f"Invalid value. Expected 3. Got {data}.")


def parse_node_type_array(data: object) -> Literal["array"]:
if data == "array":
return "array"
raise ValueError(f"Invalid value. Expected 'array'. Got {data}.")
raise NodeTypeValidationError(f"Invalid value. Expected 'array'. Got {data}.")


def parse_codecs(data: object) -> tuple[Codec, ...]:
Expand Down
13 changes: 13 additions & 0 deletions src/zarr/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ class ContainsArrayAndGroupError(_BaseZarrError):
)


class MetadataValidationError(_BaseZarrError):
"""An exception raised when the Zarr metadata is invalid in some way"""


class NodeTypeValidationError(MetadataValidationError):
"""
Specialized exception when the node_type of the metadata document is incorrect..
This can be raised when the value is invalid or unexpected given the context,
for example an 'array' node when we expected a 'group'.
"""


__all__ = [
"ContainsArrayAndGroupError",
"ContainsArrayError",
Expand Down

0 comments on commit 3878f24

Please sign in to comment.