From 44f20ff25621fc6543f1d63cfb4022bbc4afd123 Mon Sep 17 00:00:00 2001 From: Joseph Hamman Date: Tue, 12 Nov 2024 15:28:15 -0800 Subject: [PATCH] is_empty --- src/zarr/abc/store.py | 2 +- src/zarr/storage/common.py | 6 +++--- src/zarr/storage/logging.py | 4 ++-- src/zarr/testing/store.py | 16 ++++++++-------- tests/test_store/test_local.py | 4 ++-- tests/test_store/test_remote.py | 2 +- tests/test_store/test_stateful_store.py | 16 ++++++++-------- 7 files changed, 25 insertions(+), 25 deletions(-) diff --git a/src/zarr/abc/store.py b/src/zarr/abc/store.py index 0b3cc4002..fa3c7f3bd 100644 --- a/src/zarr/abc/store.py +++ b/src/zarr/abc/store.py @@ -82,7 +82,7 @@ async def _ensure_open(self) -> None: if not self._is_open: await self._open() - async def empty_dir(self, prefix: str) -> bool: + async def is_empty(self, prefix: str) -> bool: """ Check if the directory is empty. diff --git a/src/zarr/storage/common.py b/src/zarr/storage/common.py index ecb664798..0485a3357 100644 --- a/src/zarr/storage/common.py +++ b/src/zarr/storage/common.py @@ -83,7 +83,7 @@ async def open( match mode: case "w-": - if not await self.empty_dir(): + if not await self.is_empty(): msg = ( f"{self} is not empty, but `mode` is set to 'w-'." "Either remove the existing objects in storage," @@ -187,7 +187,7 @@ async def exists(self) -> bool: """ return await self.store.exists(self.path) - async def empty_dir(self) -> bool: + async def is_empty(self) -> bool: """ Check if any keys exist in the store with the given prefix. @@ -196,7 +196,7 @@ async def empty_dir(self) -> bool: bool True if no keys exist in the store with the given prefix, False otherwise. """ - return await self.store.empty_dir(self.path) + return await self.store.is_empty(self.path) def __truediv__(self, other: str) -> StorePath: """Combine this store path with another path""" diff --git a/src/zarr/storage/logging.py b/src/zarr/storage/logging.py index 7888ebede..3b11ddbba 100644 --- a/src/zarr/storage/logging.py +++ b/src/zarr/storage/logging.py @@ -135,10 +135,10 @@ async def _ensure_open(self) -> None: with self.log(): return await self._store._ensure_open() - async def empty_dir(self, prefix: str = "") -> bool: + async def is_empty(self, prefix: str = "") -> bool: # docstring inherited with self.log(): - return await self._store.empty_dir(prefix=prefix) + return await self._store.is_empty(prefix=prefix) async def clear(self) -> None: # docstring inherited diff --git a/src/zarr/testing/store.py b/src/zarr/testing/store.py index b8a2016c6..b544bf87e 100644 --- a/src/zarr/testing/store.py +++ b/src/zarr/testing/store.py @@ -230,23 +230,23 @@ async def test_delete_dir(self, store: S) -> None: assert not await store.exists("foo/zarr.json") assert not await store.exists("foo/c/0") - async def test_empty_dir(self, store: S) -> None: - assert await store.empty_dir("") + async def test_is_empty(self, store: S) -> None: + assert await store.is_empty("") await self.set( store, "foo/bar", self.buffer_cls.from_bytes(bytes("something", encoding="utf-8")) ) - assert not await store.empty_dir("") - assert await store.empty_dir("fo") - assert not await store.empty_dir("foo/") - assert not await store.empty_dir("foo") - assert await store.empty_dir("spam/") + assert not await store.is_empty("") + assert await store.is_empty("fo") + assert not await store.is_empty("foo/") + assert not await store.is_empty("foo") + assert await store.is_empty("spam/") async def test_clear(self, store: S) -> None: await self.set( store, "key", self.buffer_cls.from_bytes(bytes("something", encoding="utf-8")) ) await store.clear() - assert await store.empty_dir("") + assert await store.is_empty("") async def test_list(self, store: S) -> None: assert await _collect_aiterator(store.list()) == () diff --git a/tests/test_store/test_local.py b/tests/test_store/test_local.py index 4b85708da..c614d32c2 100644 --- a/tests/test_store/test_local.py +++ b/tests/test_store/test_local.py @@ -43,9 +43,9 @@ def test_store_supports_listing(self, store: LocalStore) -> None: assert store.supports_listing async def test_empty_with_empty_subdir(self, store: LocalStore) -> None: - assert await store.empty_dir("") + assert await store.is_empty("") (store.root / "foo/bar").mkdir(parents=True) - assert await store.empty_dir("") + assert await store.is_empty("") def test_creates_new_directory(self, tmp_path: pathlib.Path): target = tmp_path.joinpath("a", "b", "c") diff --git a/tests/test_store/test_remote.py b/tests/test_store/test_remote.py index 5702e63bf..aee620796 100644 --- a/tests/test_store/test_remote.py +++ b/tests/test_store/test_remote.py @@ -213,4 +213,4 @@ async def test_empty_nonexistent_path(self, store_kwargs) -> None: # regression test for https://github.com/zarr-developers/zarr-python/pull/2343 store_kwargs["path"] += "/abc" store = await self.store_cls.open(**store_kwargs) - assert await store.empty_dir("") + assert await store.is_empty("") diff --git a/tests/test_store/test_stateful_store.py b/tests/test_store/test_stateful_store.py index 4514d630e..751c1ac74 100644 --- a/tests/test_store/test_stateful_store.py +++ b/tests/test_store/test_stateful_store.py @@ -55,8 +55,8 @@ def get_partial_values( def delete(self, path: str) -> None: return self._sync(self.store.delete(path)) - def empty_dir(self, prefix: str = "") -> bool: - return self._sync(self.store.empty_dir(prefix=prefix)) + def is_empty(self, prefix: str) -> bool: + return self._sync(self.store.is_empty(prefix=prefix)) def clear(self) -> None: return self._sync(self.store.clear()) @@ -184,18 +184,18 @@ def clear(self) -> None: self.store.clear() self.model.clear() - assert self.store.empty_dir() + assert self.store.is_empty("") assert len(self.model.keys()) == len(list(self.store.list())) == 0 @rule() # Local store can be non-empty when there are subdirectories but no files @precondition(lambda self: not isinstance(self.store.store, LocalStore)) - def empty_dir(self) -> None: - note("(empty_dir)") + def is_empty(self) -> None: + note("(is_empty)") # make sure they either both are or both aren't empty (same state) - assert self.store.empty_dir() == (not self.model) + assert self.store.is_empty("") == (not self.model) @rule(key=zarr_keys) def exists(self, key: str) -> None: @@ -228,10 +228,10 @@ def check_zarr_keys(self) -> None: keys = list(self.store.list()) if not keys: - assert self.store.empty_dir() is True + assert self.store.is_empty("") is True else: - assert self.store.empty_dir() is False + assert self.store.is_empty("") is False for key in keys: assert self.store.exists(key) is True