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 10 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
3 changes: 3 additions & 0 deletions docs/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ Docs
Maintenance
~~~~~~~~~~~

* FSStore now raises rather than return bad data.
By :user:`Martin Durant <martindurant>` and :user:`Ian Carroll <itcarroll>` :issue:`1604`.

* Cache result of ``FSStore._fsspec_installed()``.
By :user:`Janick Martinez Esturo <ph03>` :issue:`1581`.

Expand Down
22 changes: 17 additions & 5 deletions zarr/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -1413,11 +1413,23 @@ def _normalize_key(self, key):
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.
return {keys[keys_transformed.index(rk)]: rv for rk, rv in results.items()}
keys_transformed = {self._normalize_key(key): key for key in keys}
results_transformed = self.map.getitems(list(keys_transformed), on_error="return")
results = {}
for k, v in results_transformed.items():
if isinstance(v, self.exceptions):
# Cause recognized exceptions to prompt a KeyError in the
# function calling this method
continue
elif isinstance(v, Exception):
# Raise any other exception
raise v
else:
# 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[keys_transformed[k]] = v
return results

def __getitem__(self, key):
key = self._normalize_key(key)
Expand Down
21 changes: 21 additions & 0 deletions zarr/tests/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -1098,6 +1098,12 @@ def mock_walker_no_slash(_path):

@pytest.mark.skipif(have_fsspec is False, reason="needs fsspec")
class TestFSStore(StoreTests):
@pytest.fixture
def memory_store(self):
store = FSStore("memory://test/out.zarr")
yield store
store.map.fs.store.clear()

def create_store(self, normalize_keys=False, dimension_separator=".", path=None, **kwargs):
if path is None:
path = tempfile.mkdtemp()
Expand Down Expand Up @@ -1337,6 +1343,21 @@ def test_s3_complex(self):
)
assert (a[:] == -np.ones((8, 8, 8))).all()

def test_exceptions(self, memory_store):
path = memory_store.path
m = memory_store.fs
g = zarr.open_group(memory_store, mode="w")
arr = g.create_dataset("data", data=[1, 2, 3, 4], dtype="i4", compression=None, chunks=[2])
m.store[path + "/data/0"] = None
itcarroll marked this conversation as resolved.
Show resolved Hide resolved
del m.store[path + "/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[:]


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