Skip to content

Commit be67b6a

Browse files
committed
fix(zarr-metadata): a model shares nothing with the document it read
`parse_*` returns a canonical document as it was given, and the models copied one level of what they parsed, so a frozen model held the caller's own dictionaries: codec configurations, attributes, extension fields, v2 codecs, and those of the nodes a group's inline consolidated metadata holds. Every node model's `from_json` now reads a deep copy, as every `to_json` already writes one; the v2 consolidated reader already refines its entries into fresh containers. The copy replaces the cast that restated `ZarrV3NamedConfig`'s configuration, and the `arrays_to_tuples` pass that the `parse_*` inside repeated. Assisted-by: ClaudeCode:claude-opus-5-5
1 parent b3045a2 commit be67b6a

5 files changed

Lines changed: 47 additions & 11 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
A model read by `from_json` or `from_key_value` shares no mutable state
2+
with the document it was read from, as a document from `to_json` already
3+
shares none with its model. A frozen model held the caller's own
4+
dictionaries, so editing the input after reading it changed the model,
5+
and could make `to_key_value` fail.

‎packages/zarr-metadata/src/zarr_metadata/model/_array.py‎

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
MetadataValidationError,
1717
StoreKey,
1818
ValidationProblem,
19-
arrays_to_tuples,
2019
dump_store_json,
2120
load_store_json,
2221
parse_array_metadata_v2,
@@ -79,12 +78,8 @@ def from_json(cls, data: object) -> ZarrV3NamedConfig:
7978
field = parse_metadata_field_v3(data)
8079
if isinstance(field, str):
8180
return cls(name=field, configuration={}, must_understand=True)
82-
# Sound cast: parse_metadata_field_v3 checked the configuration is a
83-
# string-keyed mapping of JSON values; arrays_to_tuples only converts
84-
# lists to tuples within that shape.
85-
configuration = cast(
86-
"dict[str, JSONValue]", arrays_to_tuples(dict(field.get("configuration", {})))
87-
)
81+
# A read model shares no mutable state with what it read.
82+
configuration = copy.deepcopy(dict(field.get("configuration", {})))
8883
return cls(
8984
name=field["name"],
9085
configuration=configuration,
@@ -287,7 +282,8 @@ def to_json(self) -> ZarrV3ArrayMetadataJSON:
287282

288283
@classmethod
289284
def from_json(cls, data: object) -> ZarrV3ArrayMetadata:
290-
parsed = parse_array_metadata_v3(arrays_to_tuples(data))
285+
# A read model shares no mutable state with what it read.
286+
parsed = copy.deepcopy(parse_array_metadata_v3(data))
291287
# Sound cast: the TypedDict types all non-standard keys as its
292288
# `extra_items` (`ZarrV3ExtensionField`); the comprehension's inferred value
293289
# type is the union over ALL keys because the key filter cannot narrow it.
@@ -458,7 +454,8 @@ def to_json(self) -> ZarrV2ArrayMetadataJSON:
458454

459455
@classmethod
460456
def from_json(cls, data: object) -> ZarrV2ArrayMetadata:
461-
parsed = parse_array_metadata_v2(arrays_to_tuples(data))
457+
# A read model shares no mutable state with what it read.
458+
parsed = copy.deepcopy(parse_array_metadata_v2(data))
462459
return cls(
463460
shape=parsed["shape"],
464461
dtype=parsed["dtype"],

‎packages/zarr-metadata/src/zarr_metadata/model/_group.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ def to_json(self) -> ZarrV3GroupMetadataJSON:
136136

137137
@classmethod
138138
def from_json(cls, data: object) -> ZarrV3GroupMetadata:
139-
parsed = parse_group_metadata_v3(arrays_to_tuples(data))
139+
# A read model shares no mutable state with what it read.
140+
parsed = copy.deepcopy(parse_group_metadata_v3(data))
140141
# Cast for narrowing across standard and arbitrary extra TypedDict items.
141142
consolidated_raw = cast("object", parsed.get(ZARR_V3_CONSOLIDATED_METADATA_KEY, UNSET))
142143
consolidated: ZarrV3ConsolidatedMetadata | UNSET
@@ -315,7 +316,8 @@ def to_json(self) -> ZarrV2GroupMetadataJSON:
315316

316317
@classmethod
317318
def from_json(cls, data: object) -> ZarrV2GroupMetadata:
318-
parsed = parse_group_metadata_v2(arrays_to_tuples(data))
319+
# A read model shares no mutable state with what it read.
320+
parsed = copy.deepcopy(parse_group_metadata_v2(data))
319321
return cls(attributes=(dict(parsed["attributes"]) if "attributes" in parsed else UNSET))
320322

321323
@classmethod

‎packages/zarr-metadata/tests/model/test_array.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,20 @@ def test_to_json_shares_no_mutable_state_with_model(
736736
assert model.to_json() == baseline
737737

738738

739+
@pytest.mark.parametrize("model", TO_JSON_NO_ALIASING_PARAMS)
740+
def test_from_json_shares_no_mutable_state_with_its_input(
741+
model: ZarrV3ArrayMetadata | ZarrV2ArrayMetadata,
742+
) -> None:
743+
"""Mutating the document a model was read from leaves the model unchanged."""
744+
# Arrays as tuples: the reader has nothing to rebuild, so only a copy
745+
# keeps the model apart from its input.
746+
document = arrays_to_tuples(model.to_json())
747+
read = type(model).from_json(document)
748+
baseline = copy.deepcopy(read.to_json())
749+
mutate_nested_containers(document)
750+
assert read.to_json() == baseline
751+
752+
739753
def test_v3_parser_accepts_bare_string_data_type() -> None:
740754
"""V3 from_json accepts a bare-string data_type and re-serializes it canonically."""
741755
doc = ZarrV3ArrayMetadata.create_default().to_json()

‎packages/zarr-metadata/tests/model/test_group.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from zarr_metadata.model._validation import (
2323
MetadataValidationError,
2424
ValidationProblem,
25+
arrays_to_tuples,
2526
is_group_metadata_v2,
2627
is_group_metadata_v3,
2728
parse_group_metadata_v2,
@@ -573,3 +574,20 @@ def test_to_json_shares_no_mutable_state_with_model(
573574
baseline = copy.deepcopy(model.to_json())
574575
mutate_nested_containers(model.to_json())
575576
assert model.to_json() == baseline
577+
578+
579+
@pytest.mark.parametrize("model", TO_JSON_NO_ALIASING_PARAMS)
580+
def test_from_json_shares_no_mutable_state_with_its_input(
581+
model: ZarrV3GroupMetadata
582+
| ZarrV2GroupMetadata
583+
| ZarrV3ConsolidatedMetadata
584+
| ZarrV2ConsolidatedMetadata,
585+
) -> None:
586+
"""Mutating the document a model was read from leaves the model unchanged."""
587+
# Arrays as tuples: the reader has nothing to rebuild, so only a copy
588+
# keeps the model apart from its input.
589+
document = arrays_to_tuples(model.to_json())
590+
read = type(model).from_json(document)
591+
baseline = copy.deepcopy(read.to_json())
592+
mutate_nested_containers(document)
593+
assert read.to_json() == baseline

0 commit comments

Comments
 (0)