Skip to content

Commit 6ecd146

Browse files
committed
Refactor make_store_path for clarity
1 parent a0c56fb commit 6ecd146

File tree

1 file changed

+54
-44
lines changed

1 file changed

+54
-44
lines changed

src/zarr/storage/_common.py

Lines changed: 54 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -323,54 +323,64 @@ async def make_store_path(
323323
"""
324324
from zarr.storage._fsspec import FsspecStore # circular import
325325

326-
used_storage_options = False
327326
path_normalized = normalize_path(path)
328-
if isinstance(store_like, StorePath):
329-
result = store_like / path_normalized
330-
else:
331-
assert mode in (None, "r", "r+", "a", "w", "w-")
332-
# if mode 'r' was provided, we'll open any new stores as read-only
333-
_read_only = mode == "r"
334-
if isinstance(store_like, Store):
335-
store = store_like
336-
elif store_like is None:
337-
store = await MemoryStore.open(read_only=_read_only)
338-
elif isinstance(store_like, Path):
339-
store = await LocalStore.open(root=store_like, read_only=_read_only)
340-
elif isinstance(store_like, str):
341-
storage_options = storage_options or {}
342-
343-
if _is_fsspec_uri(store_like):
344-
used_storage_options = True
345-
store = FsspecStore.from_url(
346-
store_like, storage_options=storage_options, read_only=_read_only
347-
)
348-
else:
349-
store = await LocalStore.open(root=Path(store_like), read_only=_read_only)
350-
elif isinstance(store_like, dict):
351-
# We deliberate only consider dict[str, Buffer] here, and not arbitrary mutable mappings.
352-
# By only allowing dictionaries, which are in-memory, we know that MemoryStore appropriate.
353-
store = await MemoryStore.open(store_dict=store_like, read_only=_read_only)
354-
elif _has_fsspec and isinstance(store_like, FSMap):
355-
if path:
356-
raise ValueError(
357-
"'path' was provided but is not used for FSMap store_like objects. Specify the path when creating the FSMap instance instead."
358-
)
359-
if storage_options:
360-
raise ValueError(
361-
"'storage_options was provided but is not used for FSMap store_like objects. Specify the storage options when creating the FSMap instance instead."
362-
)
363-
store = FsspecStore.from_mapper(store_like, read_only=_read_only)
364-
else:
365-
raise TypeError(f"Unsupported type for store_like: '{type(store_like).__name__}'")
366327

367-
result = await StorePath.open(store, path=path_normalized, mode=mode)
328+
if (
329+
not (isinstance(store_like, str) and _is_fsspec_uri(store_like))
330+
and storage_options is not None
331+
):
332+
raise TypeError(
333+
"'storage_options' was provided but unused. "
334+
"'storage_options' is only used when the store is passed as a FSSpec URI string.",
335+
)
368336

369-
if storage_options and not used_storage_options:
370-
msg = "'storage_options' was provided but unused. 'storage_options' is only used for fsspec filesystem stores."
371-
raise TypeError(msg)
337+
assert mode in (None, "r", "r+", "a", "w", "w-")
338+
_read_only = mode == "r"
372339

373-
return result
340+
if isinstance(store_like, StorePath):
341+
# Already a StorePath
342+
return store_like / path_normalized
343+
344+
elif isinstance(store_like, Store):
345+
# Already a Store
346+
store = store_like
347+
348+
elif isinstance(store_like, dict):
349+
# Already a dictionary that can be a MemoryStore
350+
#
351+
# We deliberate only consider dict[str, Buffer] here, and not arbitrary mutable mappings.
352+
# By only allowing dictionaries, which are in-memory, we know that MemoryStore appropriate.
353+
store = await MemoryStore.open(store_dict=store_like, read_only=_read_only)
354+
355+
elif store_like is None:
356+
# Create a new in-memory store
357+
return await make_store_path({}, mode=mode)
358+
359+
elif isinstance(store_like, Path):
360+
# Create a new LocalStore
361+
store = await LocalStore.open(root=store_like, read_only=_read_only)
362+
363+
elif isinstance(store_like, str):
364+
# Either a FSSpec URI or a local filesystem path
365+
storage_options = storage_options or {}
366+
if _is_fsspec_uri(store_like):
367+
store = FsspecStore.from_url(
368+
store_like, storage_options=storage_options, read_only=_read_only
369+
)
370+
else:
371+
# Assume a filesystem path
372+
return await make_store_path(Path(store_like), mode=mode)
373+
374+
elif _has_fsspec and isinstance(store_like, FSMap):
375+
if path:
376+
raise ValueError(
377+
"'path' was provided but is not used for FSMap store_like objects. Specify the path when creating the FSMap instance instead."
378+
)
379+
store = FsspecStore.from_mapper(store_like, read_only=_read_only)
380+
else:
381+
raise TypeError(f"Unsupported type for store_like: '{type(store_like).__name__}'")
382+
383+
return await StorePath.open(store, path=path_normalized, mode=mode)
374384

375385

376386
def _is_fsspec_uri(uri: str) -> bool:

0 commit comments

Comments
 (0)