Skip to content

Commit fe981bf

Browse files
committed
Fix overwrite modes
1 parent 8826415 commit fe981bf

2 files changed

Lines changed: 21 additions & 10 deletions

File tree

src/zarr/api/asynchronous.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888

8989
_READ_MODES: tuple[AccessModeLiteral, ...] = ("r", "r+", "a")
9090
_CREATE_MODES: tuple[AccessModeLiteral, ...] = ("a", "w", "w-")
91-
_OVERWRITE_MODES: tuple[AccessModeLiteral, ...] = ("a", "r+", "w")
91+
_OVERWRITE_MODES: tuple[AccessModeLiteral, ...] = ("w",)
9292

9393

9494
def _infer_overwrite(mode: AccessModeLiteral) -> bool:
@@ -817,7 +817,6 @@ async def open_group(
817817
warnings.warn("chunk_store is not yet implemented", RuntimeWarning, stacklevel=2)
818818

819819
store_path = await make_store_path(store, mode=mode, storage_options=storage_options, path=path)
820-
821820
if attributes is None:
822821
attributes = {}
823822

tests/test_api.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
create_group,
3030
group,
3131
load,
32-
open,
3332
open_group,
3433
save,
3534
save_array,
@@ -41,6 +40,9 @@
4140
from zarr.storage._utils import normalize_path
4241
from zarr.testing.utils import gpu_test
4342

43+
if TYPE_CHECKING:
44+
from pathlib import Path
45+
4446

4547
def test_create(memory_store: Store) -> None:
4648
store = memory_store
@@ -135,28 +137,28 @@ async def test_open_array(memory_store: MemoryStore, zarr_format: ZarrFormat) ->
135137
store = memory_store
136138

137139
# open array, create if doesn't exist
138-
z = open(store=store, shape=100, zarr_format=zarr_format)
140+
z = zarr.api.synchronous.open(store=store, shape=100, zarr_format=zarr_format)
139141
assert isinstance(z, Array)
140142
assert z.shape == (100,)
141143

142144
# open array, overwrite
143145
# store._store_dict = {}
144146
store = MemoryStore()
145-
z = open(store=store, shape=200, zarr_format=zarr_format)
147+
z = zarr.api.synchronous.open(store=store, shape=200, zarr_format=zarr_format)
146148
assert isinstance(z, Array)
147149
assert z.shape == (200,)
148150

149151
# open array, read-only
150152
store_cls = type(store)
151153
ro_store = await store_cls.open(store_dict=store._store_dict, read_only=True)
152-
z = open(store=ro_store, mode="r")
154+
z = zarr.api.synchronous.open(store=ro_store, mode="r")
153155
assert isinstance(z, Array)
154156
assert z.shape == (200,)
155157
assert z.read_only
156158

157159
# path not found
158160
with pytest.raises(FileNotFoundError):
159-
open(store="doesnotexist", mode="r", zarr_format=zarr_format)
161+
zarr.api.synchronous.open(store="doesnotexist", mode="r", zarr_format=zarr_format)
160162

161163

162164
@pytest.mark.parametrize("store", ["memory", "local", "zip"], indirect=True)
@@ -233,12 +235,12 @@ def test_save(store: Store, n_args: int, n_kwargs: int) -> None:
233235
save(store)
234236
elif n_args == 1 and n_kwargs == 0:
235237
save(store, *args)
236-
array = open(store)
238+
array = zarr.api.synchronous.open(store)
237239
assert isinstance(array, Array)
238240
assert_array_equal(array[:], data)
239241
else:
240242
save(store, *args, **kwargs) # type: ignore [arg-type]
241-
group = open(store)
243+
group = zarr.api.synchronous.open(store)
242244
assert isinstance(group, Group)
243245
for array in group.array_values():
244246
assert_array_equal(array[:], data)
@@ -1077,7 +1079,7 @@ def test_tree() -> None:
10771079
def test_open_positional_args_deprecated() -> None:
10781080
store = MemoryStore()
10791081
with pytest.warns(FutureWarning, match="pass"):
1080-
open(store, "w", shape=(1,))
1082+
zarr.api.synchronous.open(store, "w", shape=(1,))
10811083

10821084

10831085
def test_save_array_positional_args_deprecated() -> None:
@@ -1236,3 +1238,13 @@ def test_v2_with_v3_compressor() -> None:
12361238
zarr.create(
12371239
store={}, shape=(1), dtype="uint8", zarr_format=2, compressor=zarr.codecs.BloscCodec()
12381240
)
1241+
1242+
1243+
def test_create_no_delete_file(tmp_path: Path) -> None:
1244+
with open(tmp_path / "file.txt", "w") as f:
1245+
f.write("abc")
1246+
1247+
with pytest.raises(NotImplementedError, match="loading groups not yet supported"):
1248+
zarr.load("./tmp")
1249+
# Even though above fails, it shouldn't delete existing file
1250+
assert (tmp_path / "file.txt").exists()

0 commit comments

Comments
 (0)