|
4 | 4 | from typing import TYPE_CHECKING
|
5 | 5 |
|
6 | 6 | import zarr.codecs
|
| 7 | +import zarr.storage |
7 | 8 |
|
8 | 9 | if TYPE_CHECKING:
|
9 | 10 | import pathlib
|
10 | 11 |
|
11 | 12 | from zarr.abc.store import Store
|
12 | 13 | from zarr.core.common import JSON, MemoryOrder, ZarrFormat
|
13 | 14 |
|
| 15 | +import contextlib |
14 | 16 | import warnings
|
15 | 17 | from typing import Literal
|
16 | 18 |
|
|
27 | 29 | create,
|
28 | 30 | create_array,
|
29 | 31 | create_group,
|
| 32 | + from_array, |
30 | 33 | group,
|
31 | 34 | load,
|
32 |
| - open, |
33 | 35 | open_group,
|
34 | 36 | save,
|
35 | 37 | save_array,
|
|
41 | 43 | from zarr.storage._utils import normalize_path
|
42 | 44 | from zarr.testing.utils import gpu_test
|
43 | 45 |
|
| 46 | +if TYPE_CHECKING: |
| 47 | + from collections.abc import Callable |
| 48 | + from pathlib import Path |
| 49 | + |
44 | 50 |
|
45 | 51 | def test_create(memory_store: Store) -> None:
|
46 | 52 | store = memory_store
|
@@ -135,28 +141,28 @@ async def test_open_array(memory_store: MemoryStore, zarr_format: ZarrFormat) ->
|
135 | 141 | store = memory_store
|
136 | 142 |
|
137 | 143 | # open array, create if doesn't exist
|
138 |
| - z = open(store=store, shape=100, zarr_format=zarr_format) |
| 144 | + z = zarr.api.synchronous.open(store=store, shape=100, zarr_format=zarr_format) |
139 | 145 | assert isinstance(z, Array)
|
140 | 146 | assert z.shape == (100,)
|
141 | 147 |
|
142 | 148 | # open array, overwrite
|
143 | 149 | # store._store_dict = {}
|
144 | 150 | store = MemoryStore()
|
145 |
| - z = open(store=store, shape=200, zarr_format=zarr_format) |
| 151 | + z = zarr.api.synchronous.open(store=store, shape=200, zarr_format=zarr_format) |
146 | 152 | assert isinstance(z, Array)
|
147 | 153 | assert z.shape == (200,)
|
148 | 154 |
|
149 | 155 | # open array, read-only
|
150 | 156 | store_cls = type(store)
|
151 | 157 | ro_store = await store_cls.open(store_dict=store._store_dict, read_only=True)
|
152 |
| - z = open(store=ro_store, mode="r") |
| 158 | + z = zarr.api.synchronous.open(store=ro_store, mode="r") |
153 | 159 | assert isinstance(z, Array)
|
154 | 160 | assert z.shape == (200,)
|
155 | 161 | assert z.read_only
|
156 | 162 |
|
157 | 163 | # path not found
|
158 | 164 | with pytest.raises(FileNotFoundError):
|
159 |
| - open(store="doesnotexist", mode="r", zarr_format=zarr_format) |
| 165 | + zarr.api.synchronous.open(store="doesnotexist", mode="r", zarr_format=zarr_format) |
160 | 166 |
|
161 | 167 |
|
162 | 168 | @pytest.mark.parametrize("store", ["memory", "local", "zip"], indirect=True)
|
@@ -233,12 +239,12 @@ def test_save(store: Store, n_args: int, n_kwargs: int) -> None:
|
233 | 239 | save(store)
|
234 | 240 | elif n_args == 1 and n_kwargs == 0:
|
235 | 241 | save(store, *args)
|
236 |
| - array = open(store) |
| 242 | + array = zarr.api.synchronous.open(store) |
237 | 243 | assert isinstance(array, Array)
|
238 | 244 | assert_array_equal(array[:], data)
|
239 | 245 | else:
|
240 | 246 | save(store, *args, **kwargs) # type: ignore [arg-type]
|
241 |
| - group = open(store) |
| 247 | + group = zarr.api.synchronous.open(store) |
242 | 248 | assert isinstance(group, Group)
|
243 | 249 | for array in group.array_values():
|
244 | 250 | assert_array_equal(array[:], data)
|
@@ -1077,7 +1083,7 @@ def test_tree() -> None:
|
1077 | 1083 | def test_open_positional_args_deprecated() -> None:
|
1078 | 1084 | store = MemoryStore()
|
1079 | 1085 | with pytest.warns(FutureWarning, match="pass"):
|
1080 |
| - open(store, "w", shape=(1,)) |
| 1086 | + zarr.api.synchronous.open(store, "w", shape=(1,)) |
1081 | 1087 |
|
1082 | 1088 |
|
1083 | 1089 | def test_save_array_positional_args_deprecated() -> None:
|
@@ -1236,3 +1242,62 @@ def test_v2_with_v3_compressor() -> None:
|
1236 | 1242 | zarr.create(
|
1237 | 1243 | store={}, shape=(1), dtype="uint8", zarr_format=2, compressor=zarr.codecs.BloscCodec()
|
1238 | 1244 | )
|
| 1245 | + |
| 1246 | + |
| 1247 | +def add_empty_file(path: Path) -> Path: |
| 1248 | + fpath = path / "a.txt" |
| 1249 | + fpath.touch() |
| 1250 | + return fpath |
| 1251 | + |
| 1252 | + |
| 1253 | +@pytest.mark.parametrize("create_function", [create_array, from_array]) |
| 1254 | +@pytest.mark.parametrize("overwrite", [True, False]) |
| 1255 | +def test_no_overwrite_array(tmp_path: Path, create_function: Callable, overwrite: bool) -> None: # type:ignore[type-arg] |
| 1256 | + store = zarr.storage.LocalStore(tmp_path) |
| 1257 | + existing_fpath = add_empty_file(tmp_path) |
| 1258 | + |
| 1259 | + assert existing_fpath.exists() |
| 1260 | + create_function(store=store, data=np.ones(shape=(1,)), overwrite=overwrite) |
| 1261 | + if overwrite: |
| 1262 | + assert not existing_fpath.exists() |
| 1263 | + else: |
| 1264 | + assert existing_fpath.exists() |
| 1265 | + |
| 1266 | + |
| 1267 | +@pytest.mark.parametrize("create_function", [create_group, group]) |
| 1268 | +@pytest.mark.parametrize("overwrite", [True, False]) |
| 1269 | +def test_no_overwrite_group(tmp_path: Path, create_function: Callable, overwrite: bool) -> None: # type:ignore[type-arg] |
| 1270 | + store = zarr.storage.LocalStore(tmp_path) |
| 1271 | + existing_fpath = add_empty_file(tmp_path) |
| 1272 | + |
| 1273 | + assert existing_fpath.exists() |
| 1274 | + create_function(store=store, overwrite=overwrite) |
| 1275 | + if overwrite: |
| 1276 | + assert not existing_fpath.exists() |
| 1277 | + else: |
| 1278 | + assert existing_fpath.exists() |
| 1279 | + |
| 1280 | + |
| 1281 | +@pytest.mark.parametrize("open_func", [zarr.open, open_group]) |
| 1282 | +@pytest.mark.parametrize("mode", ["r", "r+", "a", "w", "w-"]) |
| 1283 | +def test_no_overwrite_open(tmp_path: Path, open_func: Callable, mode: str) -> None: # type:ignore[type-arg] |
| 1284 | + store = zarr.storage.LocalStore(tmp_path) |
| 1285 | + existing_fpath = add_empty_file(tmp_path) |
| 1286 | + |
| 1287 | + assert existing_fpath.exists() |
| 1288 | + with contextlib.suppress(FileExistsError, FileNotFoundError): |
| 1289 | + open_func(store=store, mode=mode) |
| 1290 | + if mode == "w": |
| 1291 | + assert not existing_fpath.exists() |
| 1292 | + else: |
| 1293 | + assert existing_fpath.exists() |
| 1294 | + |
| 1295 | + |
| 1296 | +def test_no_overwrite_load(tmp_path: Path) -> None: |
| 1297 | + store = zarr.storage.LocalStore(tmp_path) |
| 1298 | + existing_fpath = add_empty_file(tmp_path) |
| 1299 | + |
| 1300 | + assert existing_fpath.exists() |
| 1301 | + with contextlib.suppress(NotImplementedError): |
| 1302 | + zarr.load(store) |
| 1303 | + assert existing_fpath.exists() |
0 commit comments