diff --git a/src/zarr/api/asynchronous.py b/src/zarr/api/asynchronous.py index 6164cda957..adb4acffde 100644 --- a/src/zarr/api/asynchronous.py +++ b/src/zarr/api/asynchronous.py @@ -877,7 +877,10 @@ async def open_group( overwrite=overwrite, attributes=attributes, ) - msg = f"No group found in store {store!r} at path {store_path.path!r}" + if zarr_format is not None: + msg = f"No Zarr {zarr_format} group found in store {store!r} at path {store_path.path!r}" + else: + msg = f"No Zarr group found in store {store!r} at path {store_path.path!r}" raise GroupNotFoundError(msg) @@ -1290,7 +1293,10 @@ async def open_array( overwrite=overwrite, **kwargs, ) - msg = f"No array found in store {store_path.store} at path {store_path.path}" + if zarr_format is not None: + msg = f"No Zarr {zarr_format} array found in store {store_path.store} at path {store_path.path}" + else: + msg = f"No Zarr array found in store {store_path.store} at path {store_path.path}" raise ArrayNotFoundError(msg) from err diff --git a/tests/test_api.py b/tests/test_api.py index adea150ae1..e1ef3864ab 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -45,6 +45,7 @@ from zarr.core.buffer import NDArrayLike from zarr.errors import ( ArrayNotFoundError, + GroupNotFoundError, MetadataValidationError, ZarrDeprecationWarning, ZarrUserWarning, @@ -1547,3 +1548,33 @@ def test_unimplemented_kwarg_warnings(kwarg_name: str) -> None: kwargs = {kwarg_name: 1} with pytest.warns(RuntimeWarning, match=".* is not yet implemented"): zarr.create(shape=(1,), **kwargs) # type: ignore[arg-type] + + +def test_specific_fmt_error_message_group() -> None: + store = zarr.storage.MemoryStore() + # Create a group + zarr.create_group(store=store, path="my_group", zarr_format=3) + # Check that the new group exists + zarr.open_group(store=store, path="my_group", mode="r", zarr_format=3) + # Check error message when opening with wrong Zarr format + with pytest.raises(GroupNotFoundError, match="No Zarr 2 group found"): + zarr.open_group(store=store, path="my_group", mode="r", zarr_format=2) + + +def test_specific_fmt_error_message_array() -> None: + store = zarr.storage.MemoryStore() + # Create an array + zarr.create_array( + store=store, + zarr_format=3, + data=np.array( + [ + 1, + ] + ), + ) + # Check that the new array exists + zarr.open_array(store=store, mode="r", zarr_format=3) + # Check error message when opening with wrong Zarr format + with pytest.raises(ArrayNotFoundError, match="No Zarr 2 array found"): + zarr.open_array(store=store, mode="r", zarr_format=2)