From 8665f299984de91850722c59075e9710220b765f Mon Sep 17 00:00:00 2001 From: David Stansby Date: Fri, 16 May 2025 18:06:32 +0100 Subject: [PATCH 1/4] Add a GroupNotFoundError --- src/zarr/api/asynchronous.py | 4 ++-- src/zarr/errors.py | 10 +++++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/zarr/api/asynchronous.py b/src/zarr/api/asynchronous.py index 4f3c9c3f8f..8445d18d6b 100644 --- a/src/zarr/api/asynchronous.py +++ b/src/zarr/api/asynchronous.py @@ -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: @@ -836,7 +836,7 @@ async def open_group( overwrite=overwrite, attributes=attributes, ) - raise FileNotFoundError(f"Unable to find group: {store_path}") + raise GroupNotFoundError(store, store_path.path) async def create( diff --git a/src/zarr/errors.py b/src/zarr/errors.py index 441cdab9a3..31071fff81 100644 --- a/src/zarr/errors.py +++ b/src/zarr/errors.py @@ -10,7 +10,7 @@ ] -class BaseZarrError(ValueError): +class BaseZarrError(Exception): """ Base error which all zarr errors are sub-classed from. """ @@ -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.""" From ac1c82d3689d0ad475cfe7f1e1b924a6c5e90053 Mon Sep 17 00:00:00 2001 From: David Stansby Date: Wed, 21 May 2025 12:39:14 +0100 Subject: [PATCH 2/4] Fixup tests --- tests/test_metadata/test_v3.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/test_metadata/test_v3.py b/tests/test_metadata/test_v3.py index a47cbf43bb..13549b10a4 100644 --- a/tests/test_metadata/test_v3.py +++ b/tests/test_metadata/test_v3.py @@ -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 @@ -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) @@ -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, + match=f"Invalid value for 'node_type'. Expected 'array'. Got '{data}'.", ): parse_node_type_array(data) From fedf099e0fcc8f8e8cc308bd026f9f21ff3280fe Mon Sep 17 00:00:00 2001 From: David Stansby Date: Wed, 21 May 2025 12:40:35 +0100 Subject: [PATCH 3/4] Add changelog entries --- changes/3066.bugfix.rst | 2 ++ changes/3066.feature.rst | 1 + 2 files changed, 3 insertions(+) create mode 100644 changes/3066.bugfix.rst create mode 100644 changes/3066.feature.rst diff --git a/changes/3066.bugfix.rst b/changes/3066.bugfix.rst new file mode 100644 index 0000000000..5499358f14 --- /dev/null +++ b/changes/3066.bugfix.rst @@ -0,0 +1,2 @@ +`~zarr.errors.BaseZarrError` now inherits from `Exception` instead of `ValueError`. +This fix also affects all other exceptions in `zarr.errors`. diff --git a/changes/3066.feature.rst b/changes/3066.feature.rst new file mode 100644 index 0000000000..89d5ddb1c6 --- /dev/null +++ b/changes/3066.feature.rst @@ -0,0 +1 @@ +Added `~zarr.errors.GroupNotFoundError`, which is raised when attempting to open a group that does not exist. From d1159559f2e3326fa99e696cbda899c382640aee Mon Sep 17 00:00:00 2001 From: David Stansby Date: Sun, 25 May 2025 10:42:12 +0100 Subject: [PATCH 4/4] Remove inheritance change --- changes/3066.bugfix.rst | 2 -- src/zarr/errors.py | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) delete mode 100644 changes/3066.bugfix.rst diff --git a/changes/3066.bugfix.rst b/changes/3066.bugfix.rst deleted file mode 100644 index 5499358f14..0000000000 --- a/changes/3066.bugfix.rst +++ /dev/null @@ -1,2 +0,0 @@ -`~zarr.errors.BaseZarrError` now inherits from `Exception` instead of `ValueError`. -This fix also affects all other exceptions in `zarr.errors`. diff --git a/src/zarr/errors.py b/src/zarr/errors.py index 31071fff81..4d3140a4a9 100644 --- a/src/zarr/errors.py +++ b/src/zarr/errors.py @@ -10,7 +10,7 @@ ] -class BaseZarrError(Exception): +class BaseZarrError(ValueError): """ Base error which all zarr errors are sub-classed from. """