|
4 | 4 | from typing import Any, Literal |
5 | 5 |
|
6 | 6 | import pytest |
| 7 | +from packaging.version import parse as parse_version |
7 | 8 |
|
8 | 9 | import zarr |
9 | 10 | from zarr import Group |
| 11 | +from zarr.abc.store import Store |
10 | 12 | from zarr.core.buffer import cpu |
11 | 13 | from zarr.core.common import ZARR_JSON, AccessModeLiteral, ZarrFormat |
12 | 14 | from zarr.storage import FsspecStore, LocalStore, MemoryStore, StoreLike, StorePath, ZipStore |
13 | 15 | from zarr.storage._common import ( |
14 | 16 | _contains_node_v3, |
15 | 17 | contains_array, |
16 | 18 | contains_group, |
| 19 | + make_store, |
17 | 20 | make_store_path, |
18 | 21 | ) |
19 | 22 | from zarr.storage._utils import ( |
@@ -248,6 +251,45 @@ async def test_make_store_path_storage_options_raises(store_like: StoreLike) -> |
248 | 251 | await make_store_path(store_like, storage_options={"foo": "bar"}) |
249 | 252 |
|
250 | 253 |
|
| 254 | +# universal-pathlib 0.2.x emits this from its own subclass registry when a local UPath is built. |
| 255 | +@pytest.mark.filterwarnings( |
| 256 | + "ignore:Detected a customized `__new__` method in subclass:DeprecationWarning" |
| 257 | +) |
| 258 | +@pytest.mark.parametrize( |
| 259 | + ("url", "expected"), |
| 260 | + [ |
| 261 | + ("memory://bucket/foo.zarr", FsspecStore), |
| 262 | + ("s3://bucket/foo.zarr", FsspecStore), |
| 263 | + ("file://{tmp}/foo.zarr", LocalStore), |
| 264 | + ("{tmp}/foo.zarr", LocalStore), |
| 265 | + ], |
| 266 | +) |
| 267 | +async def test_make_store_upath(url: str, expected: type[Store], tmp_path: Path) -> None: |
| 268 | + """ |
| 269 | + A remote UPath becomes an FsspecStore, and a local one becomes a LocalStore, so that |
| 270 | + UPath("/data") and Path("/data") agree. See https://github.com/zarr-developers/zarr-python/issues/4244. |
| 271 | + """ |
| 272 | + upath = pytest.importorskip("upath") |
| 273 | + fsspec = pytest.importorskip("fsspec") |
| 274 | + if url.startswith("s3://"): |
| 275 | + pytest.importorskip("s3fs") |
| 276 | + if url.startswith("memory://") and parse_version(fsspec.__version__) < parse_version( |
| 277 | + "2024.12.0" |
| 278 | + ): |
| 279 | + # MemoryFileSystem is synchronous, so it can only be used once fsspec is new enough to |
| 280 | + # supply AsyncFileSystemWrapper. |
| 281 | + pytest.skip("No AsyncFileSystemWrapper") |
| 282 | + store = await make_store(upath.UPath(url.format(tmp=tmp_path))) |
| 283 | + assert isinstance(store, expected) |
| 284 | + |
| 285 | + |
| 286 | +async def test_make_store_upath_storage_options_raises() -> None: |
| 287 | + """A UPath carries its own storage options, so a separate mapping is ambiguous.""" |
| 288 | + upath = pytest.importorskip("upath") |
| 289 | + with pytest.raises(TypeError, match="storage_options"): |
| 290 | + await make_store(upath.UPath("memory://bucket/foo.zarr"), storage_options={"foo": "bar"}) |
| 291 | + |
| 292 | + |
251 | 293 | async def test_unsupported() -> None: |
252 | 294 | with pytest.raises(TypeError, match="Unsupported type for store_like: 'int'"): |
253 | 295 | await make_store_path(1) |
|
0 commit comments