Skip to content

Commit b77e1c2

Browse files
committed
Wrap sync fs for xarray.to_zarr
1 parent b773b3e commit b77e1c2

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/zarr/storage/_fsspec.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,17 @@ def from_url(
172172
opts = {"asynchronous": True, **opts}
173173

174174
fs, path = url_to_fs(url, **opts)
175+
if not fs.async_impl:
176+
try:
177+
from fsspec.implementations.asyn_wrapper import AsyncFileSystemWrapper
178+
179+
fs = AsyncFileSystemWrapper(fs)
180+
except ImportError as e:
181+
raise ImportError(
182+
f"The filesystem for URL '{url}' is synchronous, and the required "
183+
"AsyncFileSystemWrapper is not available. Upgrade fsspec to version "
184+
"2024.12.0 or later to enable this functionality."
185+
) from e
175186

176187
# fsspec is not consistent about removing the scheme from the path, so check and strip it here
177188
# https://github.com/fsspec/filesystem_spec/issues/1722

tests/test_store/test_fsspec.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import pytest
88
from botocore.session import Session
9+
from fsspec.implementations.asyn_wrapper import AsyncFileSystemWrapper
910

1011
import zarr.api.asynchronous
1112
from zarr.abc.store import OffsetByteRequest
@@ -215,3 +216,19 @@ async def test_empty_nonexistent_path(self, store_kwargs) -> None:
215216
store_kwargs["path"] += "/abc"
216217
store = await self.store_cls.open(**store_kwargs)
217218
assert await store.is_empty("")
219+
220+
221+
def test_wrap_sync_filesystem():
222+
"""The local fs is not async so we should expect it to be wrapped automatically"""
223+
store = FsspecStore.from_url("local://test/path")
224+
225+
assert isinstance(store.fs, AsyncFileSystemWrapper)
226+
assert store.fs.async_impl
227+
228+
229+
def test_no_wrap_async_filesystem():
230+
"""An async fs should not be wrapped automatically; fsspec's https filesystem is such an fs"""
231+
store = FsspecStore.from_url("https://test/path")
232+
233+
assert not isinstance(store.fs, AsyncFileSystemWrapper)
234+
assert store.fs.async_impl

0 commit comments

Comments
 (0)