Skip to content

Make sure fs exceptions are raised if not Missing #1237

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

Closed
wants to merge 10 commits into from
7 changes: 6 additions & 1 deletion zarr/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -1417,9 +1417,14 @@ def getitems(
) -> Mapping[str, Any]:

keys_transformed = [self._normalize_key(key) for key in keys]
results = self.map.getitems(keys_transformed, on_error="omit")
results = self.map.getitems(keys_transformed, on_error="return")
# The function calling this method may not recognize the transformed keys
# So we send the values returned by self.map.getitems back into the original key space.
for k, v in results.copy().items():
if isinstance(v, self.exceptions):
results.pop(k)
elif isinstance(v, Exception):
raise v
return {keys[keys_transformed.index(rk)]: rv for rk, rv in results.items()}

def __getitem__(self, key):
Expand Down
16 changes: 16 additions & 0 deletions zarr/tests/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -1341,6 +1341,22 @@ def test_s3_complex(self):
)
assert (a[:] == -np.ones((8, 8, 8))).all()

def test_exceptions(self):
import fsspec
m = fsspec.filesystem("memory")
g = zarr.open_group("memory://test/out.zarr", mode='w')
arr = g.create_dataset("data", data=[1, 2, 3, 4],
dtype="i4", compression=None, chunks=[2])
Comment on lines +1348 to +1349
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
arr = g.create_dataset("data", data=[1, 2, 3, 4],
dtype="i4", compression=None, chunks=[2])
arr = g.create_dataset("data", data=[1, 2, 3, 4], dtype="i4", compression=None, chunks=[2])

black/ruff line length is 100, this will fix one red x

m.store["/test/out.zarr/data/0"] = None
del m.store["/test/out.zarr/data/1"]
assert g.store.getitems(["data/1"]) == {} # not found
with pytest.raises(Exception):
# None is bad data, as opposed to missing
g.store.getitems(["data/0", "data/1"])
Comment on lines +1352 to +1355
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
assert g.store.getitems(["data/1"]) == {} # not found
with pytest.raises(Exception):
# None is bad data, as opposed to missing
g.store.getitems(["data/0", "data/1"])
assert g.store.getitems(["data/1"], contexts={}) == {} # not found
with pytest.raises(Exception):
# None is bad data, as opposed to missing
g.store.getitems(["data/0", "data/1"], contexts={})

missing required contexts kwarg is causing one (of two) test failures

with pytest.raises(Exception):
# None is bad data, as opposed to missing
arr[:]


@pytest.mark.skipif(have_fsspec is False, reason="needs fsspec")
class TestFSStoreWithKeySeparator(StoreTests):
Expand Down