Skip to content
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

Make sure fs exceptions are raised if not MissingFs exceptions (clone) #1604

Merged
merged 17 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
12 changes: 9 additions & 3 deletions zarr/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -1414,9 +1414,15 @@ def getitems(
self, keys: Sequence[str], *, contexts: Mapping[str, Context]
) -> Mapping[str, Any]:
keys_transformed = [self._normalize_key(key) for key in keys]
results = self.map.getitems(keys_transformed, on_error="omit")
# 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.
results = self.map.getitems(keys_transformed, on_error="return")
# Only recognized errors will prompt KeyErrors in the function calling this method
for k, v in results.copy().items():
martindurant marked this conversation as resolved.
Show resolved Hide resolved
if isinstance(v, self.exceptions):
results.pop(k)
elif isinstance(v, Exception):
raise v
# 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.
return {keys[keys_transformed.index(rk)]: rv for rk, rv in results.items()}

def __getitem__(self, key):
Expand Down
18 changes: 18 additions & 0 deletions zarr/tests/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -1337,6 +1337,24 @@ 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])
m.store["/test/out.zarr/data/0"] = None
del m.store["/test/out.zarr/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={})
with pytest.raises(Exception):
# None is bad data, as opposed to missing
arr[:]
# clear the global memory filesystem's store for use in other tests
m.store.clear()
itcarroll marked this conversation as resolved.
Show resolved Hide resolved


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