Skip to content

Commit

Permalink
Don't raise an exception on set_if_not_exists
Browse files Browse the repository at this point in the history
  • Loading branch information
maxrjones committed Dec 15, 2024
1 parent f310260 commit 86951b8
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/zarr/storage/object_store.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import asyncio
import contextlib
from collections import defaultdict
from collections.abc import Iterable
from typing import TYPE_CHECKING, Any, TypedDict
Expand Down Expand Up @@ -111,7 +112,8 @@ async def set(self, key: str, value: Buffer) -> None:
async def set_if_not_exists(self, key: str, value: Buffer) -> None:
self._check_writable()
buf = value.to_bytes()
await obs.put_async(self.store, key, buf, mode="create")
with contextlib.suppress(obs.exceptions.AlreadyExistsError):
await obs.put_async(self.store, key, buf, mode="create")

@property
def supports_deletes(self) -> bool:
Expand Down
5 changes: 2 additions & 3 deletions src/zarr/testing/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,8 @@ async def test_set_if_not_exists(self, store: S) -> None:
data_buf = self.buffer_cls.from_bytes(b"0000")
await self.set(store, key, data_buf)

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

result = await store.get(key, default_buffer_prototype())
assert result == data_buf
Expand Down

0 comments on commit 86951b8

Please sign in to comment.