From 979b6f271bb6abaa55825bd12014807c727d3426 Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Fri, 11 Oct 2024 16:08:53 -0500 Subject: [PATCH] Ensure paths created (#2337) --- src/zarr/storage/local.py | 5 +++++ tests/v3/test_store/test_local.py | 13 +++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/zarr/storage/local.py b/src/zarr/storage/local.py index 1e1f3beec..da37cbfd5 100644 --- a/src/zarr/storage/local.py +++ b/src/zarr/storage/local.py @@ -94,6 +94,11 @@ def __init__(self, root: Path | str, *, mode: AccessModeLiteral = "r") -> None: assert isinstance(root, Path) self.root = root + async def _open(self) -> None: + if not self.mode.readonly: + self.root.mkdir(parents=True, exist_ok=True) + return await super()._open() + async def clear(self) -> None: self._check_writable() shutil.rmtree(self.root) diff --git a/tests/v3/test_store/test_local.py b/tests/v3/test_store/test_local.py index 8cdb4e874..5352e3520 100644 --- a/tests/v3/test_store/test_local.py +++ b/tests/v3/test_store/test_local.py @@ -1,11 +1,17 @@ from __future__ import annotations +from typing import TYPE_CHECKING + import pytest +import zarr from zarr.core.buffer import Buffer, cpu from zarr.storage.local import LocalStore from zarr.testing.store import StoreTests +if TYPE_CHECKING: + import pathlib + class TestLocalStore(StoreTests[LocalStore, cpu.Buffer]): store_cls = LocalStore @@ -40,3 +46,10 @@ 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() + + def test_creates_new_directory(self, tmp_path: pathlib.Path): + target = tmp_path.joinpath("a", "b", "c") + assert not target.exists() + + store = self.store_cls(root=target, mode="w") + zarr.group(store=store)