Skip to content

Commit ee60792

Browse files
authored
Normalize paths when creating StorePath (#2850)
* add failing group name tests * normalize paths in storepath constructor * add asserts to hypothesis tests * test group creation too * code review * changelog
1 parent 73d5a6c commit ee60792

File tree

4 files changed

+44
-29
lines changed

4 files changed

+44
-29
lines changed

changes/2850.fix.rst

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixed a bug where ``StorePath`` creation would not apply standard path normalization to the ``path`` parameter,
2+
which led to the creation of arrays and groups with invalid keys.

src/zarr/storage/_common.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class StorePath:
4141

4242
def __init__(self, store: Store, path: str = "") -> None:
4343
self.store = store
44-
self.path = path
44+
self.path = normalize_path(path)
4545

4646
@property
4747
def read_only(self) -> bool:

src/zarr/testing/strategies.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from zarr.core.sync import sync
2020
from zarr.storage import MemoryStore, StoreLike
2121
from zarr.storage._common import _dereference_path
22+
from zarr.storage._utils import normalize_path
2223

2324
# Copied from Xarray
2425
_attr_keys = st.text(st.characters(), min_size=1)
@@ -277,11 +278,12 @@ def arrays(
277278
if a.metadata.zarr_format == 3:
278279
assert a.fill_value is not None
279280
assert a.name is not None
281+
assert a.path == normalize_path(array_path)
282+
assert a.name == "/" + a.path
280283
assert isinstance(root[array_path], Array)
281284
assert nparray.shape == a.shape
282285
assert chunk_shape == a.chunks
283286
assert shard_shape == a.shards
284-
assert array_path == a.path, (path, name, array_path, a.name, a.path)
285287
assert a.basename == name, (a.basename, name)
286288
assert dict(a.attrs) == expected_attrs
287289

tests/test_group.py

+38-27
Original file line numberDiff line numberDiff line change
@@ -130,24 +130,27 @@ async def test_create_creates_parents(store: Store, zarr_format: ZarrFormat) ->
130130
assert g.attrs == {}
131131

132132

133-
def test_group_name_properties(store: Store, zarr_format: ZarrFormat) -> None:
133+
@pytest.mark.parametrize("store", ["memory"], indirect=True)
134+
@pytest.mark.parametrize("root_name", ["", "/", "a", "/a"])
135+
@pytest.mark.parametrize("branch_name", ["foo", "/foo", "foo/bar", "/foo/bar"])
136+
def test_group_name_properties(
137+
store: Store, zarr_format: ZarrFormat, root_name: str, branch_name: str
138+
) -> None:
134139
"""
135-
Test basic properties of groups
140+
Test that the path, name, and basename attributes of a group and its subgroups are consistent
136141
"""
137-
root = Group.from_store(store=store, zarr_format=zarr_format)
138-
assert root.path == ""
139-
assert root.name == "/"
140-
assert root.basename == ""
142+
root = Group.from_store(store=StorePath(store=store, path=root_name), zarr_format=zarr_format)
143+
assert root.path == normalize_path(root_name)
144+
assert root.name == "/" + root.path
145+
assert root.basename == root.path
141146

142-
foo = root.create_group("foo")
143-
assert foo.path == "foo"
144-
assert foo.name == "/foo"
145-
assert foo.basename == "foo"
146-
147-
bar = root.create_group("foo/bar")
148-
assert bar.path == "foo/bar"
149-
assert bar.name == "/foo/bar"
150-
assert bar.basename == "bar"
147+
branch = root.create_group(branch_name)
148+
if root.path == "":
149+
assert branch.path == normalize_path(branch_name)
150+
else:
151+
assert branch.path == "/".join([root.path, normalize_path(branch_name)])
152+
assert branch.name == "/" + branch.path
153+
assert branch.basename == branch_name.split("/")[-1]
151154

152155

153156
@pytest.mark.parametrize("consolidated_metadata", [True, False])
@@ -623,11 +626,13 @@ async def test_group_update_attributes_async(store: Store, zarr_format: ZarrForm
623626

624627

625628
@pytest.mark.parametrize("method", ["create_array", "array"])
629+
@pytest.mark.parametrize("name", ["a", "/a"])
626630
def test_group_create_array(
627631
store: Store,
628632
zarr_format: ZarrFormat,
629633
overwrite: bool,
630634
method: Literal["create_array", "array"],
635+
name: str,
631636
) -> None:
632637
"""
633638
Test `Group.from_store`
@@ -638,23 +643,26 @@ def test_group_create_array(
638643
data = np.arange(np.prod(shape)).reshape(shape).astype(dtype)
639644

640645
if method == "create_array":
641-
array = group.create_array(name="array", shape=shape, dtype=dtype)
646+
array = group.create_array(name=name, shape=shape, dtype=dtype)
642647
array[:] = data
643648
elif method == "array":
644649
with pytest.warns(DeprecationWarning):
645-
array = group.array(name="array", data=data, shape=shape, dtype=dtype)
650+
array = group.array(name=name, data=data, shape=shape, dtype=dtype)
646651
else:
647652
raise AssertionError
648653

649654
if not overwrite:
650655
if method == "create_array":
651656
with pytest.raises(ContainsArrayError):
652-
a = group.create_array(name="array", shape=shape, dtype=dtype)
657+
a = group.create_array(name=name, shape=shape, dtype=dtype)
653658
a[:] = data
654659
elif method == "array":
655660
with pytest.raises(ContainsArrayError), pytest.warns(DeprecationWarning):
656-
a = group.array(name="array", shape=shape, dtype=dtype)
661+
a = group.array(name=name, shape=shape, dtype=dtype)
657662
a[:] = data
663+
664+
assert array.path == normalize_path(name)
665+
assert array.name == "/" + array.path
658666
assert array.shape == shape
659667
assert array.dtype == np.dtype(dtype)
660668
assert np.array_equal(array[:], data)
@@ -945,20 +953,23 @@ async def test_asyncgroup_delitem(store: Store, zarr_format: ZarrFormat) -> None
945953
raise AssertionError
946954

947955

956+
@pytest.mark.parametrize("name", ["a", "/a"])
948957
async def test_asyncgroup_create_group(
949958
store: Store,
959+
name: str,
950960
zarr_format: ZarrFormat,
951961
) -> None:
952962
agroup = await AsyncGroup.from_store(store=store, zarr_format=zarr_format)
953-
sub_node_path = "sub_group"
954963
attributes = {"foo": 999}
955-
subnode = await agroup.create_group(name=sub_node_path, attributes=attributes)
956-
957-
assert isinstance(subnode, AsyncGroup)
958-
assert subnode.attrs == attributes
959-
assert subnode.store_path.path == sub_node_path
960-
assert subnode.store_path.store == store
961-
assert subnode.metadata.zarr_format == zarr_format
964+
subgroup = await agroup.create_group(name=name, attributes=attributes)
965+
966+
assert isinstance(subgroup, AsyncGroup)
967+
assert subgroup.path == normalize_path(name)
968+
assert subgroup.name == "/" + subgroup.path
969+
assert subgroup.attrs == attributes
970+
assert subgroup.store_path.path == subgroup.path
971+
assert subgroup.store_path.store == store
972+
assert subgroup.metadata.zarr_format == zarr_format
962973

963974

964975
async def test_asyncgroup_create_array(

0 commit comments

Comments
 (0)