Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/3576.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed `FsspecStore.from_url` passing `asynchronous=True` to synchronous `fsspec` filesystems (such as `ssh://` / `SFTPFileSystem`), which do not have `async_impl = True` and are subsequently wrapped with `_make_async`.
13 changes: 11 additions & 2 deletions src/zarr/storage/_fsspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,17 @@ def from_url(
# before fsspec==2024.3.1
from fsspec.core import url_to_fs

opts = storage_options or {}
opts = {"asynchronous": True, **opts}
from fsspec.core import split_protocol
from fsspec.registry import get_filesystem_class

opts = dict(storage_options) if storage_options else {}
if "::" in url:
head = url.split("::", 1)[0]
protocol = split_protocol(head)[0] or head
else:
protocol = opts.get("protocol") or split_protocol(url)[0] or "file"
if getattr(get_filesystem_class(protocol), "async_impl", False):
opts.setdefault("asynchronous", True)

fs, path = url_to_fs(url, **opts)
if not fs.async_impl:
Expand Down
99 changes: 99 additions & 0 deletions tests/test_store/test_fsspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -783,3 +783,102 @@ async def test_memory_scheme() -> None:
store = await make_store("memory://test")
assert isinstance(store, FsspecStore)
assert store.fs.protocol == "memory"


@pytest.mark.skipif(
parse_version(fsspec.__version__) < parse_version("2024.12.0"),
reason="No AsyncFileSystemWrapper",
)
def test_from_url_ssh_does_not_pass_asynchronous(monkeypatch: pytest.MonkeyPatch) -> None:
"""Regression test for https://github.com/zarr-developers/zarr-python/issues/3576 ."""
import inspect

paramiko = pytest.importorskip("paramiko")
from fsspec.implementations.asyn_wrapper import AsyncFileSystemWrapper
from fsspec.implementations.sftp import SFTPFileSystem

orig_connect = paramiko.SSHClient.connect
connect_sig = inspect.signature(orig_connect)

def fake_connect(client: Any, hostname: str, *args: Any, **kwargs: Any) -> None:
connect_sig.bind(client, hostname, *args, **kwargs)

monkeypatch.setattr(paramiko.SSHClient, "connect", fake_connect)
monkeypatch.setattr(paramiko.SSHClient, "open_sftp", lambda _self: object())

storage_options: dict[str, Any] = {"username": "tester"}
store = FsspecStore.from_url(
"ssh://example.invalid/tmp/test.zarr", storage_options=storage_options
)

assert storage_options == {"username": "tester"}
assert isinstance(store.fs, AsyncFileSystemWrapper)
assert isinstance(store.fs.sync_fs, SFTPFileSystem)
assert store.fs.asynchronous
assert store.path == "/tmp/test.zarr"


@pytest.mark.skipif(
parse_version(fsspec.__version__) < parse_version("2024.12.0"),
reason="No AsyncFileSystemWrapper",
)
def test_from_url_sync_and_async_protocols(monkeypatch: pytest.MonkeyPatch) -> None:
"""``from_url`` only defaults ``asynchronous=True`` for async-capable filesystems."""
import importlib

from fsspec.asyn import AsyncFileSystem
from fsspec.implementations.asyn_wrapper import AsyncFileSystemWrapper
from fsspec.spec import AbstractFileSystem

reg_mod = importlib.import_module("fsspec.registry")
reg_dict = getattr(reg_mod, "_registry", reg_mod.registry)

sync_proto = "_test_sync_proto"
async_proto = "_test_async_proto"

class _SyncStrictFS(AbstractFileSystem): # type: ignore[misc]
protocol = sync_proto
async_impl = False

def __init__(self, **kwargs: Any) -> None:
if "asynchronous" in kwargs:
raise TypeError("unexpected keyword argument 'asynchronous'")
super().__init__(**kwargs)

class _AsyncFS(AsyncFileSystem): # type: ignore[misc]
protocol = async_proto
async_impl = True

monkeypatch.setitem(reg_dict, sync_proto, _SyncStrictFS)
monkeypatch.setitem(reg_dict, async_proto, _AsyncFS)

store_direct = FsspecStore.from_url(f"{sync_proto}://bucket/data.zarr")
assert isinstance(store_direct.fs, AsyncFileSystemWrapper)
assert isinstance(store_direct.fs.sync_fs, _SyncStrictFS)
assert store_direct.fs.asynchronous

store_chained = FsspecStore.from_url(f"simplecache::{sync_proto}://bucket/data.zarr")
assert isinstance(store_chained.fs, AsyncFileSystemWrapper)
assert store_chained.fs.asynchronous

opts_proto: dict[str, Any] = {"protocol": sync_proto}
store_by_option = FsspecStore.from_url("bucket/data.zarr", storage_options=opts_proto)
assert opts_proto == {"protocol": sync_proto}
assert isinstance(store_by_option.fs, AsyncFileSystemWrapper)
assert isinstance(store_by_option.fs.sync_fs, _SyncStrictFS)

opts_async: dict[str, Any] = {}
store_async = FsspecStore.from_url(
f"{async_proto}://bucket/data.zarr", storage_options=opts_async
)
assert opts_async == {}
assert isinstance(store_async.fs, _AsyncFS)
assert store_async.fs.asynchronous

with pytest.warns(ZarrUserWarning, match=r".* was not created with `asynchronous=True`.*"):
store_explicit_sync = FsspecStore.from_url(
f"{async_proto}://bucket/data.zarr",
storage_options={"asynchronous": False},
)
assert isinstance(store_explicit_sync.fs, _AsyncFS)
assert not store_explicit_sync.fs.asynchronous