Skip to content

Commit

Permalink
is_empty
Browse files Browse the repository at this point in the history
  • Loading branch information
jhamman committed Nov 12, 2024
1 parent f9dbd5b commit 44f20ff
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/zarr/abc/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions src/zarr/storage/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,"
Expand Down Expand Up @@ -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.
Expand All @@ -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"""
Expand Down
4 changes: 2 additions & 2 deletions src/zarr/storage/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions src/zarr/testing/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()) == ()
Expand Down
4 changes: 2 additions & 2 deletions tests/test_store/test_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion tests/test_store/test_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("")
16 changes: 8 additions & 8 deletions tests/test_store/test_stateful_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 44f20ff

Please sign in to comment.