Skip to content

Commit 06f35f2

Browse files
committed
Consolidate code
1 parent c4bfb06 commit 06f35f2

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

src/zarr/storage/_fsspec.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,30 @@
3232

3333

3434
def _make_async(fs: AbstractFileSystem) -> AsyncFileSystem:
35+
"""Convert a sync FSSpec filesystem to an async FFSpec filesystem
36+
37+
If the filesystem class supports async operations, a new async instance is created
38+
from the existing instance.
39+
40+
If the filesystem class does not support async operations, the existing instance
41+
is wrapped with AsyncFileSystemWrapper.
42+
"""
43+
if fs.async_impl and fs.asynchronous:
44+
return fs
45+
if fs.async_impl:
46+
raise NotImplementedError(
47+
f"The filesystem '{fs}' is synchronous and wrapping synchronous filesystems using from_mapper has not been implemented. See https://github.com/zarr-developers/zarr-python/issues/2706 for more details."
48+
)
3549
try:
3650
from fsspec.implementations.asyn_wrapper import AsyncFileSystemWrapper
3751

38-
fs = AsyncFileSystemWrapper(fs)
52+
return AsyncFileSystemWrapper(fs)
3953
except ImportError as e:
4054
raise ImportError(
4155
f"The filesystem '{fs}' is synchronous, and the required "
4256
"AsyncFileSystemWrapper is not available. Upgrade fsspec to version "
4357
"2024.12.0 or later to enable this functionality."
4458
) from e
45-
return fs
4659

4760

4861
class FsspecStore(Store):
@@ -174,14 +187,8 @@ def from_mapper(
174187
-------
175188
FsspecStore
176189
"""
177-
if not fs_map.fs.async_impl:
178-
raise NotImplementedError(
179-
f"The filesystem '{fs_map.fs}' is synchronous and wrapping synchronous filesystems using from_mapper has not been implemented. See https://github.com/zarr-developers/zarr-python/issues/2706 for more details."
180-
)
181190
if not fs_map.fs.asynchronous:
182-
raise NotImplementedError(
183-
f"The filesystem '{fs_map.fs}' is synchronous and conversion to an async instance has not been implemented. See https://github.com/zarr-developers/zarr-python/issues/2706 for more details."
184-
)
191+
fs_map.fs = _make_async(fs_map.fs)
185192
return cls(
186193
fs=fs_map.fs,
187194
path=fs_map.root,

0 commit comments

Comments
 (0)