diff --git a/changes/3367.bugfix.rst b/changes/3367.bugfix.rst new file mode 100644 index 0000000000..75a3615e8d --- /dev/null +++ b/changes/3367.bugfix.rst @@ -0,0 +1 @@ +Added `~zarr.errors.ArrayNotFoundError`, which is raised when attempting to open a zarr array that does not exist. \ No newline at end of file diff --git a/src/zarr/api/asynchronous.py b/src/zarr/api/asynchronous.py index 78b68caf73..46a7d92158 100644 --- a/src/zarr/api/asynchronous.py +++ b/src/zarr/api/asynchronous.py @@ -40,6 +40,7 @@ ) from zarr.core.metadata import ArrayMetadataDict, ArrayV2Metadata, ArrayV3Metadata from zarr.errors import ( + ArrayNotFoundError, GroupNotFoundError, NodeTypeValidationError, ZarrDeprecationWarning, @@ -1257,7 +1258,7 @@ async def open_array( try: return await AsyncArray.open(store_path, zarr_format=zarr_format) - except FileNotFoundError: + except FileNotFoundError as err: if not store_path.read_only and mode in _CREATE_MODES: overwrite = _infer_overwrite(mode) _zarr_format = zarr_format or _default_zarr_format() @@ -1267,7 +1268,7 @@ async def open_array( overwrite=overwrite, **kwargs, ) - raise + raise ArrayNotFoundError(store_path.store, store_path.path) from err async def open_like( diff --git a/src/zarr/errors.py b/src/zarr/errors.py index 0055ea3c6c..d2cf98c869 100644 --- a/src/zarr/errors.py +++ b/src/zarr/errors.py @@ -1,6 +1,7 @@ from typing import Any __all__ = [ + "ArrayNotFoundError", "BaseZarrError", "ContainsArrayAndGroupError", "ContainsArrayError", @@ -34,6 +35,10 @@ class GroupNotFoundError(BaseZarrError, FileNotFoundError): _msg = "No group found in store {!r} at path {!r}" +class ArrayNotFoundError(BaseZarrError): + _msg = "array not found at path %r' {0!r}" + + class ContainsGroupError(BaseZarrError): """Raised when a group already exists at a certain path.""" diff --git a/tests/test_api.py b/tests/test_api.py index 12acf80589..18359fbf95 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -42,7 +42,12 @@ save_group, ) from zarr.core.buffer import NDArrayLike -from zarr.errors import MetadataValidationError, ZarrDeprecationWarning, ZarrUserWarning +from zarr.errors import ( + ArrayNotFoundError, + MetadataValidationError, + ZarrDeprecationWarning, + ZarrUserWarning, +) from zarr.storage import MemoryStore from zarr.storage._utils import normalize_path from zarr.testing.utils import gpu_test @@ -161,7 +166,7 @@ async def test_open_array(memory_store: MemoryStore, zarr_format: ZarrFormat) -> assert z.read_only # path not found - with pytest.raises(FileNotFoundError): + with pytest.raises(ArrayNotFoundError): zarr.api.synchronous.open(store="doesnotexist", mode="r", zarr_format=zarr_format) @@ -1200,7 +1205,7 @@ async def test_metadata_validation_error() -> None: ) def test_open_array_with_mode_r_plus(store: Store, zarr_format: ZarrFormat) -> None: # 'r+' means read/write (must exist) - with pytest.raises(FileNotFoundError): + with pytest.raises(ArrayNotFoundError): zarr.open_array(store=store, mode="r+", zarr_format=zarr_format) zarr.ones(store=store, shape=(3, 3), zarr_format=zarr_format) z2 = zarr.open_array(store=store, mode="r+")