From 8eff8815606dca3c81544cc62a7cabb239dfa099 Mon Sep 17 00:00:00 2001 From: Joseph Hamman Date: Mon, 23 Sep 2024 21:21:00 -0600 Subject: [PATCH 1/2] fix(store): report store is empty if no files are present in root directory --- src/zarr/store/local.py | 8 ++++++-- tests/v3/test_store/test_local.py | 5 +++++ 2 files changed, 11 insertions(+), 2 deletions(-) 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() From d52c6ea46a3536c49c3ad8d5860573d9366b6b8f Mon Sep 17 00:00:00 2001 From: Joseph Hamman Date: Mon, 23 Sep 2024 21:27:37 -0600 Subject: [PATCH 2/2] parse dtype again --- src/zarr/core/array_spec.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) 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)