Skip to content

Commit 3ae719b

Browse files
committed
Use if on fsspec versions rather than try; else
1 parent 7517f72 commit 3ae719b

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed

src/zarr/storage/_fsspec.py

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
from contextlib import suppress
55
from typing import TYPE_CHECKING, Any
66

7-
from fsspec import AbstractFileSystem
8-
97
from zarr.abc.store import (
108
ByteRequest,
119
OffsetByteRequest,
@@ -19,6 +17,7 @@
1917
if TYPE_CHECKING:
2018
from collections.abc import AsyncIterator, Iterable
2119

20+
from fsspec import AbstractFileSystem
2221
from fsspec.asyn import AsyncFileSystem
2322
from fsspec.mapping import FSMap
2423

@@ -42,37 +41,37 @@ def _make_async(fs: AbstractFileSystem) -> AsyncFileSystem:
4241
If the filesystem class does not support async operations, the existing instance
4342
is wrapped with AsyncFileSystemWrapper.
4443
"""
44+
import fsspec
45+
from packaging.version import parse as parse_version
46+
47+
fsspec_version = parse_version(fsspec.__version__)
4548
if fs.async_impl and fs.asynchronous:
49+
# Already an async instance of an async filesystem, nothing to do
4650
return fs
4751
if fs.async_impl:
48-
try:
49-
fs_dict = fs.to_dict()
50-
fs_dict["asynchronous"] = True
51-
return AbstractFileSystem.from_dict(fs_dict)
52-
except AttributeError:
53-
# Older fsspec specification used to_json rather than to_dict
54-
import json
55-
56-
fs_dict = json.loads(fs.to_json())
57-
fs_dict["asynchronous"] = True
58-
return AbstractFileSystem.from_json(json.dumps(fs_dict))
52+
# Convert sync instance of an async fs to an async instance
53+
import json
5954

60-
from fsspec.implementations.local import LocalFileSystem
55+
fs_dict = json.loads(fs.to_json())
56+
fs_dict["asynchronous"] = True
57+
return fsspec.AbstractFileSystem.from_json(json.dumps(fs_dict))
6158

62-
if type(fs) is LocalFileSystem and not fs.auto_mkdir:
59+
# Wrap sync filesystems with the async wrapper
60+
if type(fs) is fsspec.implementations.local.LocalFileSystem and not fs.auto_mkdir:
6361
raise ValueError(
6462
f"LocalFilesystem {fs} was created with auto_mkdir=False but Zarr requires the filesystem to automatically create directories"
6563
)
66-
try:
67-
from fsspec.implementations.asyn_wrapper import AsyncFileSystemWrapper
68-
69-
return AsyncFileSystemWrapper(fs)
70-
except ImportError as e:
64+
if fsspec_version < parse_version("2024.12.0"):
7165
raise ImportError(
72-
f"The filesystem '{fs}' is synchronous, and the required "
66+
"The filesystem '{fs}' is synchronous, and the required "
7367
"AsyncFileSystemWrapper is not available. Upgrade fsspec to version "
7468
"2024.12.0 or later to enable this functionality."
75-
) from e
69+
)
70+
71+
if fsspec_version > parse_version("2025.2.0"):
72+
return fsspec.implementations.asyn_wrapper.AsyncFileSystemWrapper(fs, asynchronous=True)
73+
else:
74+
return fsspec.implementations.asyn_wrapper.AsyncFileSystemWrapper(fs)
7675

7776

7877
class FsspecStore(Store):

0 commit comments

Comments
 (0)