Skip to content

Commit a4b2509

Browse files
authored
Fix/empty listdir (#2225)
* fix(store): report store is empty if no files are present in root directory * parse dtype again
1 parent ee2a3c6 commit a4b2509

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

src/zarr/core/array_spec.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
from dataclasses import dataclass
44
from typing import TYPE_CHECKING, Any, Literal
55

6+
import numpy as np
7+
68
from zarr.core.common import parse_fill_value, parse_order, parse_shapelike
79

810
if TYPE_CHECKING:
9-
import numpy as np
10-
1111
from zarr.core.buffer import BufferPrototype
1212
from zarr.core.common import ChunkCoords
1313

@@ -29,11 +29,12 @@ def __init__(
2929
prototype: BufferPrototype,
3030
) -> None:
3131
shape_parsed = parse_shapelike(shape)
32+
dtype_parsed = np.dtype(dtype)
3233
fill_value_parsed = parse_fill_value(fill_value)
3334
order_parsed = parse_order(order)
3435

3536
object.__setattr__(self, "shape", shape_parsed)
36-
object.__setattr__(self, "dtype", dtype)
37+
object.__setattr__(self, "dtype", dtype_parsed)
3738
object.__setattr__(self, "fill_value", fill_value_parsed)
3839
object.__setattr__(self, "order", order_parsed)
3940
object.__setattr__(self, "prototype", prototype)

src/zarr/store/local.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,15 @@ async def clear(self) -> None:
9393

9494
async def empty(self) -> bool:
9595
try:
96-
subpaths = os.listdir(self.root)
96+
with os.scandir(self.root) as it:
97+
for entry in it:
98+
if entry.is_file():
99+
# stop once a file is found
100+
return False
97101
except FileNotFoundError:
98102
return True
99103
else:
100-
return not subpaths
104+
return True
101105

102106
def __str__(self) -> str:
103107
return f"file://{self.root}"

tests/v3/test_store/test_local.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,8 @@ def test_store_supports_partial_writes(self, store: LocalStore) -> None:
3535

3636
def test_store_supports_listing(self, store: LocalStore) -> None:
3737
assert store.supports_listing
38+
39+
async def test_empty_with_empty_subdir(self, store: LocalStore) -> None:
40+
assert await store.empty()
41+
(store.root / "foo/bar").mkdir(parents=True)
42+
assert await store.empty()

0 commit comments

Comments
 (0)