Skip to content

Commit bc86a61

Browse files
authored
don't serialize consolidated metadata if it's None (#3535)
* don't serialize consolidated metadata if it's unset (for zarr v3 groups) * changelog
1 parent 950066b commit bc86a61

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

changes/3535.bugfix.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixed a bug where the `"consolidated_metadata"` key was written to metadata documents even when
2+
consolidated metadata was not used, resulting in invalid metadata documents.

src/zarr/core/group.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,8 +432,11 @@ def from_dict(cls, data: dict[str, Any]) -> GroupMetadata:
432432

433433
def to_dict(self) -> dict[str, Any]:
434434
result = asdict(replace(self, consolidated_metadata=None))
435-
if self.consolidated_metadata:
435+
if self.consolidated_metadata is not None:
436436
result["consolidated_metadata"] = self.consolidated_metadata.to_dict()
437+
else:
438+
# Leave consolidated metadata unset if it's None
439+
result.pop("consolidated_metadata")
437440
return result
438441

439442

tests/test_metadata/test_v3.py

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import numpy as np
88
import pytest
99

10+
from zarr import consolidate_metadata, create_group
1011
from zarr.codecs.bytes import BytesCodec
1112
from zarr.core.buffer import default_buffer_prototype
1213
from zarr.core.chunk_key_encodings import DefaultChunkKeyEncoding, V2ChunkKeyEncoding
@@ -21,7 +22,12 @@
2122
parse_dimension_names,
2223
parse_zarr_format,
2324
)
24-
from zarr.errors import MetadataValidationError, NodeTypeValidationError, UnknownCodecError
25+
from zarr.errors import (
26+
MetadataValidationError,
27+
NodeTypeValidationError,
28+
UnknownCodecError,
29+
ZarrUserWarning,
30+
)
2531

2632
if TYPE_CHECKING:
2733
from collections.abc import Sequence
@@ -338,3 +344,52 @@ def test_parse_codecs_unknown_codec_raises(monkeypatch: pytest.MonkeyPatch) -> N
338344
codecs = [{"name": "unknown"}]
339345
with pytest.raises(UnknownCodecError):
340346
parse_codecs(codecs)
347+
348+
349+
@pytest.mark.parametrize("use_consolidated", [True, False])
350+
@pytest.mark.parametrize("attributes", [None, {"foo": "bar"}])
351+
def test_group_to_dict(use_consolidated: bool, attributes: None | dict[str, Any]) -> None:
352+
"""
353+
Test that the output of GroupMetadata.to_dict() is what we expect
354+
"""
355+
store: dict[str, object] = {}
356+
if attributes is None:
357+
expect_attributes = {}
358+
else:
359+
expect_attributes = attributes
360+
361+
group = create_group(store, attributes=attributes, zarr_format=3)
362+
group.create_group("foo")
363+
if use_consolidated:
364+
with pytest.warns(
365+
ZarrUserWarning,
366+
match="Consolidated metadata is currently not part in the Zarr format 3 specification.",
367+
):
368+
group = consolidate_metadata(store)
369+
meta = group.metadata
370+
expect = {
371+
"node_type": "group",
372+
"zarr_format": 3,
373+
"consolidated_metadata": {
374+
"kind": "inline",
375+
"must_understand": False,
376+
"metadata": {
377+
"foo": {
378+
"attributes": {},
379+
"zarr_format": 3,
380+
"node_type": "group",
381+
"consolidated_metadata": {
382+
"kind": "inline",
383+
"metadata": {},
384+
"must_understand": False,
385+
},
386+
}
387+
},
388+
},
389+
"attributes": expect_attributes,
390+
}
391+
else:
392+
meta = group.metadata
393+
expect = {"node_type": "group", "zarr_format": 3, "attributes": expect_attributes}
394+
395+
assert meta.to_dict() == expect

0 commit comments

Comments
 (0)