Skip to content

Commit 60160e9

Browse files
committed
fix(zarr-metadata): the v2 group document types are closed
`.zgroup` "Other keys MUST NOT be present", the v2 group validator refuses them, and the pydantic schema was closed, but the TypedDicts were open, and the Partial's docstring said it inherited a closed shape that nothing declared. `ZarrV2ZGroupJSON`, `ZarrV2GroupMetadataJSON` and its Partial now declare `closed=True`. Assisted-by: ClaudeCode:claude-opus-5-5
1 parent 4aeac75 commit 60160e9

3 files changed

Lines changed: 30 additions & 6 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
`ZarrV2ZGroupJSON`, `ZarrV2GroupMetadataJSON` and its Partial are closed
2+
(PEP 728), as the spec's "Other keys MUST NOT be present" and the v2 group
3+
validator already said, and as its pydantic schema already was. A type
4+
checker now refuses a v2 group document with a key it does not declare.

‎packages/zarr-metadata/src/zarr_metadata/v2/group.py‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from zarr_metadata._common import JSONValue
1212

1313

14-
class ZarrV2ZGroupJSON(TypedDict):
14+
class ZarrV2ZGroupJSON(TypedDict, closed=True):
1515
"""
1616
On-disk `.zgroup` file content.
1717
@@ -27,7 +27,7 @@ class ZarrV2ZGroupJSON(TypedDict):
2727
zarr_format: Literal[2]
2828

2929

30-
class ZarrV2GroupMetadataJSON(TypedDict):
30+
class ZarrV2GroupMetadataJSON(TypedDict, closed=True):
3131
"""
3232
Zarr v2 group metadata document, in-memory merged form.
3333
@@ -38,14 +38,18 @@ class ZarrV2GroupMetadataJSON(TypedDict):
3838
or write the real on-disk files should use `ZarrV2ZGroupJSON` (strict
3939
`.zgroup`) plus `ZarrV2ZAttrsJSON` directly.
4040
41+
Closed: `.zgroup` holds no other keys ("Other keys MUST NOT be present",
42+
https://github.com/zarr-developers/zarr-specs/blob/fc7dd9c9beb5a50b87f9b08b00bf50fc0048482f/docs/v2/v2.0.rst#L313),
43+
and `attributes` is the one member folded in.
44+
4145
See https://zarr-specs.readthedocs.io/en/latest/v2/v2.0.html
4246
"""
4347

4448
zarr_format: Literal[2]
4549
attributes: NotRequired[Mapping[str, JSONValue]]
4650

4751

48-
class ZarrV2GroupMetadataJSONPartial(TypedDict, total=False):
52+
class ZarrV2GroupMetadataJSONPartial(TypedDict, total=False, closed=True):
4953
"""
5054
Partial form of `ZarrV2GroupMetadataJSON`: every field is `NotRequired`.
5155
@@ -64,9 +68,9 @@ class ZarrV2GroupMetadataJSONPartial(TypedDict, total=False):
6468
special-casing that field (PEP 655 explicitly permits `NotRequired` inside
6569
`total=False`).
6670
67-
Note: v2 group metadata has no `extra_items` setting (the v2 spec has no
68-
extension-field concept, and `.zgroup` forbids other keys outright:
69-
https://github.com/zarr-developers/zarr-specs/blob/fc7dd9c9beb5a50b87f9b08b00bf50fc0048482f/docs/v2/v2.0.rst#L313), so this partial inherits the same closed shape.
71+
Note: v2 group metadata is closed (the v2 spec has no extension-field
72+
concept, and `.zgroup` forbids other keys outright:
73+
https://github.com/zarr-developers/zarr-specs/blob/fc7dd9c9beb5a50b87f9b08b00bf50fc0048482f/docs/v2/v2.0.rst#L313), and so is this partial.
7074
7175
Drift between this type and `ZarrV2GroupMetadataJSON` is prevented by
7276
`tests/test_partial_equivalence.py`.

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
validate_group_metadata_v2,
3030
validate_group_metadata_v3,
3131
)
32+
from zarr_metadata.v2.group import (
33+
ZarrV2GroupMetadataJSON,
34+
ZarrV2GroupMetadataJSONPartial,
35+
ZarrV2ZGroupJSON,
36+
)
3237

3338
# --- ZarrV3GroupMetadata ---------------------------------------------------
3439

@@ -134,6 +139,17 @@ def test_group_v2_rejects_unknown_document_member() -> None:
134139
]
135140

136141

142+
@pytest.mark.parametrize(
143+
"document_type",
144+
[ZarrV2ZGroupJSON, ZarrV2GroupMetadataJSON, ZarrV2GroupMetadataJSONPartial],
145+
ids=lambda document_type: document_type.__name__,
146+
)
147+
def test_v2_group_document_types_are_closed(document_type: type) -> None:
148+
"""`.zgroup` "Other keys MUST NOT be present": the validator refuses them, and
149+
the types say so."""
150+
assert getattr(document_type, "__closed__", None) is True
151+
152+
137153
@pytest.mark.parametrize(
138154
("parse", "document"),
139155
[

0 commit comments

Comments
 (0)