Skip to content

Fix/empty listdir #2225

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/zarr/core/array_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
from dataclasses import dataclass
from typing import TYPE_CHECKING, Any, Literal

import numpy as np

from zarr.core.common import parse_fill_value, parse_order, parse_shapelike

if TYPE_CHECKING:
import numpy as np

from zarr.core.buffer import BufferPrototype
from zarr.core.common import ChunkCoords

Expand All @@ -29,11 +29,12 @@ def __init__(
prototype: BufferPrototype,
) -> None:
shape_parsed = parse_shapelike(shape)
dtype_parsed = np.dtype(dtype)
fill_value_parsed = parse_fill_value(fill_value)
order_parsed = parse_order(order)

object.__setattr__(self, "shape", shape_parsed)
object.__setattr__(self, "dtype", dtype)
object.__setattr__(self, "dtype", dtype_parsed)
object.__setattr__(self, "fill_value", fill_value_parsed)
object.__setattr__(self, "order", order_parsed)
object.__setattr__(self, "prototype", prototype)
Expand Down
8 changes: 6 additions & 2 deletions src/zarr/store/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,15 @@ async def clear(self) -> None:

async def empty(self) -> bool:
try:
subpaths = os.listdir(self.root)
with os.scandir(self.root) as it:
for entry in it:
if entry.is_file():
# stop once a file is found
return False
except FileNotFoundError:
return True
else:
return not subpaths
return True

def __str__(self) -> str:
return f"file://{self.root}"
Expand Down
5 changes: 5 additions & 0 deletions tests/v3/test_store/test_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,8 @@ def test_store_supports_partial_writes(self, store: LocalStore) -> None:

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()
(store.root / "foo/bar").mkdir(parents=True)
assert await store.empty()