diff --git a/src/zarr/core/array_spec.py b/src/zarr/core/array_spec.py index 1a251a0a4b..e84a81cb05 100644 --- a/src/zarr/core/array_spec.py +++ b/src/zarr/core/array_spec.py @@ -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 @@ -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) diff --git a/src/zarr/store/local.py b/src/zarr/store/local.py index c78837586f..fd209cd7c3 100644 --- a/src/zarr/store/local.py +++ b/src/zarr/store/local.py @@ -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}" diff --git a/tests/v3/test_store/test_local.py b/tests/v3/test_store/test_local.py index 5f1dde3fcc..bdd909c285 100644 --- a/tests/v3/test_store/test_local.py +++ b/tests/v3/test_store/test_local.py @@ -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()