Skip to content

Commit 86951b8

Browse files
committed
Don't raise an exception on set_if_not_exists
1 parent f310260 commit 86951b8

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

src/zarr/storage/object_store.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import asyncio
4+
import contextlib
45
from collections import defaultdict
56
from collections.abc import Iterable
67
from typing import TYPE_CHECKING, Any, TypedDict
@@ -111,7 +112,8 @@ async def set(self, key: str, value: Buffer) -> None:
111112
async def set_if_not_exists(self, key: str, value: Buffer) -> None:
112113
self._check_writable()
113114
buf = value.to_bytes()
114-
await obs.put_async(self.store, key, buf, mode="create")
115+
with contextlib.suppress(obs.exceptions.AlreadyExistsError):
116+
await obs.put_async(self.store, key, buf, mode="create")
115117

116118
@property
117119
def supports_deletes(self) -> bool:

src/zarr/testing/store.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,9 +308,8 @@ async def test_set_if_not_exists(self, store: S) -> None:
308308
data_buf = self.buffer_cls.from_bytes(b"0000")
309309
await self.set(store, key, data_buf)
310310

311-
with pytest.raises(Exception):
312-
new = self.buffer_cls.from_bytes(b"1111")
313-
await store.set_if_not_exists(key, new)
311+
new = self.buffer_cls.from_bytes(b"1111")
312+
await store.set_if_not_exists("k", new) # no error
314313

315314
result = await store.get(key, default_buffer_prototype())
316315
assert result == data_buf

0 commit comments

Comments
 (0)