Skip to content

Add a GroupNotFoundError #3066

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/3066.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added `~zarr.errors.GroupNotFoundError`, which is raised when attempting to open a group that does not exist.
4 changes: 2 additions & 2 deletions src/zarr/api/asynchronous.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
)
from zarr.core.metadata import ArrayMetadataDict, ArrayV2Metadata, ArrayV3Metadata
from zarr.core.metadata.v2 import _default_compressor, _default_filters
from zarr.errors import NodeTypeValidationError
from zarr.errors import GroupNotFoundError, NodeTypeValidationError
from zarr.storage._common import make_store_path

if TYPE_CHECKING:
Expand Down Expand Up @@ -836,7 +836,7 @@
overwrite=overwrite,
attributes=attributes,
)
raise FileNotFoundError(f"Unable to find group: {store_path}")
raise GroupNotFoundError(store, store_path.path)

Check warning on line 839 in src/zarr/api/asynchronous.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/api/asynchronous.py#L839

Added line #L839 was not covered by tests


async def create(
Expand Down
8 changes: 8 additions & 0 deletions src/zarr/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ def __init__(self, *args: Any) -> None:
super().__init__(self._msg.format(*args))


class GroupNotFoundError(BaseZarrError, FileNotFoundError):
"""
Raised when a group isn't found at a certain path.
"""

_msg = "No group found in store {!r} at path {!r}"


class ContainsGroupError(BaseZarrError):
"""Raised when a group already exists at a certain path."""

Expand Down
8 changes: 5 additions & 3 deletions tests/test_metadata/test_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
parse_fill_value,
parse_zarr_format,
)
from zarr.errors import MetadataValidationError
from zarr.errors import MetadataValidationError, NodeTypeValidationError

if TYPE_CHECKING:
from collections.abc import Sequence
Expand Down Expand Up @@ -62,7 +62,8 @@
@pytest.mark.parametrize("data", [None, 1, 2, 4, 5, "3"])
def test_parse_zarr_format_invalid(data: Any) -> None:
with pytest.raises(
ValueError, match=f"Invalid value for 'zarr_format'. Expected '3'. Got '{data}'."
MetadataValidationError,
match=f"Invalid value for 'zarr_format'. Expected '3'. Got '{data}'.",
):
parse_zarr_format(data)

Expand All @@ -88,7 +89,8 @@ def test_parse_node_type_invalid(node_type: Any) -> None:
@pytest.mark.parametrize("data", [None, "group"])
def test_parse_node_type_array_invalid(data: Any) -> None:
with pytest.raises(
ValueError, match=f"Invalid value for 'node_type'. Expected 'array'. Got '{data}'."
NodeTypeValidationError,
Copy link
Contributor

Choose a reason for hiding this comment

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

I am wondering now why we have a special exception for this case...

match=f"Invalid value for 'node_type'. Expected 'array'. Got '{data}'.",
):
parse_node_type_array(data)

Expand Down