Skip to content

Commit bcb7684

Browse files
dcheriand-v-b
andauthored
Avoid redundant __contains__ (#1739)
Let's try grabbing the array.json and group.json files, and check for `*NotFoundError`, instead of using contains first. Co-authored-by: Davis Bennett <[email protected]>
1 parent d0fb875 commit bcb7684

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

zarr/hierarchy.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from zarr.errors import (
2828
ContainsArrayError,
2929
ContainsGroupError,
30+
ArrayNotFoundError,
3031
GroupNotFoundError,
3132
ReadOnlyError,
3233
)
@@ -457,7 +458,7 @@ def __getitem__(self, item):
457458
458459
"""
459460
path = self._item_path(item)
460-
if contains_array(self._store, path):
461+
try:
461462
return Array(
462463
self._store,
463464
read_only=self._read_only,
@@ -468,7 +469,10 @@ def __getitem__(self, item):
468469
zarr_version=self._version,
469470
meta_array=self._meta_array,
470471
)
471-
elif contains_group(self._store, path, explicit_only=True):
472+
except ArrayNotFoundError:
473+
pass
474+
475+
try:
472476
return Group(
473477
self._store,
474478
read_only=self._read_only,
@@ -479,7 +483,10 @@ def __getitem__(self, item):
479483
zarr_version=self._version,
480484
meta_array=self._meta_array,
481485
)
482-
elif self._version == 3:
486+
except GroupNotFoundError:
487+
pass
488+
489+
if self._version == 3:
483490
implicit_group = meta_root + path + "/"
484491
# non-empty folder in the metadata path implies an implicit group
485492
if self._store.list_prefix(implicit_group):

0 commit comments

Comments
 (0)