Skip to content

Commit 5fde3a2

Browse files
itcarrollmartindurantjhammanjoshmooresanketverma1704
authored
Make sure fs exceptions are raised if not MissingFs exceptions (clone) (#1604)
* Make sure fs exceptions are raised if not Missing * lint * add missing argument in tests, lint * clear memory filesystem during test * improve commenting * add memory_store fixture, getitems performance * Update release.rst * improve FSStore.test_exception coverage --------- Co-authored-by: Martin Durant <martin.durant@alumni.utoronto.ca> Co-authored-by: Joe Hamman <joe@earthmover.io> Co-authored-by: Josh Moore <josh@openmicroscopy.org> Co-authored-by: Sanket Verma <svsanketverma5@gmail.com>
1 parent 62910bc commit 5fde3a2

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

docs/release.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ Maintenance
4141

4242
* Bump minimum supported NumPy version to 1.23 (per spec 0000)
4343
By :user:`Joe Hamman <jhamman>` :issue:`1719`.
44+
45+
* FSStore now raises rather than return bad data.
46+
By :user:`Martin Durant <martindurant>` and :user:`Ian Carroll <itcarroll>` :issue:`1604`.
4447

4548
.. _release_2.17.1:
4649

zarr/storage.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,11 +1417,23 @@ def _normalize_key(self, key):
14171417
def getitems(
14181418
self, keys: Sequence[str], *, contexts: Mapping[str, Context]
14191419
) -> Mapping[str, Any]:
1420-
keys_transformed = [self._normalize_key(key) for key in keys]
1421-
results = self.map.getitems(keys_transformed, on_error="omit")
1422-
# The function calling this method may not recognize the transformed keys
1423-
# So we send the values returned by self.map.getitems back into the original key space.
1424-
return {keys[keys_transformed.index(rk)]: rv for rk, rv in results.items()}
1420+
keys_transformed = {self._normalize_key(key): key for key in keys}
1421+
results_transformed = self.map.getitems(list(keys_transformed), on_error="return")
1422+
results = {}
1423+
for k, v in results_transformed.items():
1424+
if isinstance(v, self.exceptions):
1425+
# Cause recognized exceptions to prompt a KeyError in the
1426+
# function calling this method
1427+
continue
1428+
elif isinstance(v, Exception):
1429+
# Raise any other exception
1430+
raise v
1431+
else:
1432+
# The function calling this method may not recognize the transformed
1433+
# keys, so we send the values returned by self.map.getitems back into
1434+
# the original key space.
1435+
results[keys_transformed[k]] = v
1436+
return results
14251437

14261438
def __getitem__(self, key):
14271439
key = self._normalize_key(key)

zarr/tests/test_storage.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,6 +1098,12 @@ def mock_walker_no_slash(_path):
10981098

10991099
@pytest.mark.skipif(have_fsspec is False, reason="needs fsspec")
11001100
class TestFSStore(StoreTests):
1101+
@pytest.fixture
1102+
def memory_store(self):
1103+
store = FSStore("memory://")
1104+
yield store
1105+
store.fs.store.clear()
1106+
11011107
def create_store(self, normalize_keys=False, dimension_separator=".", path=None, **kwargs):
11021108
if path is None:
11031109
path = tempfile.mkdtemp()
@@ -1337,6 +1343,25 @@ def test_s3_complex(self):
13371343
)
13381344
assert (a[:] == -np.ones((8, 8, 8))).all()
13391345

1346+
def test_exceptions(self, memory_store):
1347+
fs = memory_store.fs
1348+
group = zarr.open(memory_store, mode="w")
1349+
x = group.create_dataset("x", data=[1, 2, 3])
1350+
y = group.create_dataset("y", data=1)
1351+
fs.store["/x/0"] = None
1352+
fs.store["/y/0"] = None
1353+
# no exception from FSStore.getitems getting KeyError
1354+
assert group.store.getitems(["foo"], contexts={}) == {}
1355+
# exception from FSStore.getitems getting AttributeError
1356+
with pytest.raises(Exception):
1357+
group.store.getitems(["x/0"], contexts={})
1358+
# exception from FSStore.getitems getting AttributeError
1359+
with pytest.raises(Exception):
1360+
x[...]
1361+
# exception from FSStore.__getitem__ getting AttributeError
1362+
with pytest.raises(Exception):
1363+
y[...]
1364+
13401365

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

0 commit comments

Comments
 (0)