Skip to content

Commit cddb877

Browse files
d-v-bclaude
andcommitted
fix(metadata): raise the rectilinear flag error when refreshing a consolidated member
A group write reads each upgraded consolidated member again from its own document, and kept the consolidated copy when that document could not be read. That also swallowed the rectilinear chunks flag error, so a member whose document now declares a rectilinear grid had its stale copy stored as valid. The flag error is now `RectilinearChunksDisabledError`, a `ValueError`, and the refresh lets it propagate: the group write raises and stores nothing. Unreadable documents keep their consolidated copy as before. Assisted-by: ClaudeCode:claude-opus-5-5 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent dc50ec6 commit cddb877

3 files changed

Lines changed: 26 additions & 1 deletion

File tree

‎src/zarr/core/metadata/io.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from zarr.core.buffer.cpu import buffer_prototype as cpu_buffer_prototype
1313
from zarr.core.common import ZARR_JSON, ZARRAY_JSON, ZATTRS_JSON
1414
from zarr.core.metadata.upgrades import mark_upgraded, upgrade_array_document
15+
from zarr.core.metadata.v3 import RectilinearChunksDisabledError
1516
from zarr.errors import ArrayNotFoundError, ContainsArrayError
1617
from zarr.storage._common import StorePath, ensure_no_existing_node
1718

@@ -144,6 +145,8 @@ async def _refresh_array(store_path: StorePath, member: ArrayMetadata) -> ArrayM
144145
documents = await read_documents(store_path, ARRAY_DOCUMENTS[member.zarr_format])
145146
try:
146147
current = parse_stored_array(documents, member.zarr_format)
148+
except RectilinearChunksDisabledError:
149+
raise # A document that can be read, but only with the flag.
147150
except (KeyError, TypeError, ValueError):
148151
return member
149152
if current._stored_document_upgraded:

‎src/zarr/core/metadata/v3.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,10 @@ def from_dict(cls, data: RegularChunkGridMetadataJSON) -> Self: # type: ignore[
270270
return cls(chunk_shape=parse_chunk_shape(configuration["chunk_shape"]))
271271

272272

273+
class RectilinearChunksDisabledError(ValueError):
274+
"""Rectilinear chunk grids are used while the `array.rectilinear_chunks` flag is off."""
275+
276+
273277
@dataclass(frozen=True, kw_only=True)
274278
class RectilinearChunkGridMetadata(Metadata):
275279
"""Metadata-only description of a rectilinear chunk grid.
@@ -290,7 +294,7 @@ class RectilinearChunkGridMetadata(Metadata):
290294

291295
def __post_init__(self) -> None:
292296
if not config.get("array.rectilinear_chunks"):
293-
raise ValueError(
297+
raise RectilinearChunksDisabledError(
294298
"Rectilinear chunk grids are experimental and disabled by default. "
295299
"Enable them with: zarr.config.set({'array.rectilinear_chunks': True}) "
296300
"or set the environment variable ZARR_ARRAY__RECTILINEAR_CHUNKS=True"

‎tests/test_metadata/test_upgrades.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,24 @@ def test_group_write_keeps_consolidated_metadata_shared_with_subgroups(
947947
assert sorted(dict(reopened.members(max_depth=None))) == ["g", "g/a"]
948948

949949

950+
@pytest.mark.filterwarnings("ignore:Consolidated metadata is currently not part:UserWarning")
951+
def test_group_write_refuses_upgraded_member_read_as_rectilinear(tmp_path: Path) -> None:
952+
"""A member of consolidated metadata read from a document that had to be upgraded,
953+
whose own document now declares a rectilinear chunk grid, is read again only with the
954+
rectilinear chunks flag: without it, a group write raises and stores nothing."""
955+
path = tmp_path / "group.zarr"
956+
_flagged_consolidated_group(path, 3, "a")
957+
with pytest.warns(ZarrUserWarning, match="is read as"):
958+
group = zarr.open_group(path, mode="r+", use_consolidated=True)
959+
(path / "a" / "zarr.json").write_text(json.dumps(_rectilinear_doc([3], [[1, 2]])))
960+
documents = {p: p.read_bytes() for p in path.rglob("*") if p.is_file()}
961+
962+
with pytest.raises(ValueError, match="Rectilinear chunk grids are experimental"):
963+
group.attrs["x"] = 1
964+
965+
assert {p: p.read_bytes() for p in path.rglob("*") if p.is_file()} == documents
966+
967+
950968
@pytest.mark.filterwarnings("ignore:Consolidated metadata is currently not part:UserWarning")
951969
@pytest.mark.parametrize("zarr_format", [2, 3])
952970
@pytest.mark.parametrize("replacement", ["group", "invalid", "none"])

0 commit comments

Comments
 (0)