Skip to content

Added ArrayNotFoundError #3367

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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/3367.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added `~zarr.errors.ArrayNotFoundError`, which is raised when attempting to open a zarr array that does not exist.
5 changes: 3 additions & 2 deletions src/zarr/api/asynchronous.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
)
from zarr.core.metadata import ArrayMetadataDict, ArrayV2Metadata, ArrayV3Metadata
from zarr.errors import (
ArrayNotFoundError,
GroupNotFoundError,
NodeTypeValidationError,
ZarrDeprecationWarning,
Expand Down Expand Up @@ -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()
Expand All @@ -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(
Expand Down
5 changes: 5 additions & 0 deletions src/zarr/errors.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Any

__all__ = [
"ArrayNotFoundError",
"BaseZarrError",
"ContainsArrayAndGroupError",
"ContainsArrayError",
Expand Down Expand Up @@ -34,6 +35,10 @@ class GroupNotFoundError(BaseZarrError, FileNotFoundError):
_msg = "No group found in store {!r} at path {!r}"


class ArrayNotFoundError(BaseZarrError):
Copy link
Contributor

Choose a reason for hiding this comment

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

This should also sub-class FileNotFoundError to make sure we retain backwards compatibility.

_msg = "array not found at path %r' {0!r}"


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

Expand Down
11 changes: 8 additions & 3 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Copy link
Contributor

Choose a reason for hiding this comment

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

This is currently failing the tests, because it actually raises a GroupNotFoundError now. I think the sensible thing to do here would be to create another new error, NodeNotFoundError, that is raised when a non-specific function is used (e.g., just open) to try and open either an array or a group.

zarr.api.synchronous.open(store="doesnotexist", mode="r", zarr_format=zarr_format)


Expand Down Expand Up @@ -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+")
Expand Down
Loading