Skip to content

Ignore extra keys in v2 metadata #2297

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/zarr/core/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import asyncio
import json
import logging
from dataclasses import asdict, dataclass, field, replace
from dataclasses import asdict, dataclass, field, fields, replace
from typing import TYPE_CHECKING, Literal, cast, overload

import numpy as np
Expand Down Expand Up @@ -116,6 +116,15 @@ def __init__(
@classmethod
def from_dict(cls, data: dict[str, Any]) -> GroupMetadata:
assert data.pop("node_type", None) in ("group", None)

zarr_format = data.get("zarr_format")
if zarr_format == 2 or zarr_format is None:
# zarr v2 allowed arbitrary keys here.
# We don't want the GroupMetadata constructor to fail just because someone put an
# extra key in the metadata.
expected = {x.name for x in fields(cls)}
data = {k: v for k, v in data.items() if k in expected}

return cls(**data)

def to_dict(self) -> dict[str, Any]:
Expand Down
13 changes: 12 additions & 1 deletion src/zarr/core/metadata/v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from zarr.core.common import JSON, ChunkCoords

import json
from dataclasses import dataclass, field, replace
from dataclasses import dataclass, field, fields, replace

import numcodecs
import numpy as np
Expand Down Expand Up @@ -140,6 +140,17 @@ def from_dict(cls, data: dict[str, Any]) -> ArrayV2Metadata:
_data = data.copy()
# check that the zarr_format attribute is correct
_ = parse_zarr_format(_data.pop("zarr_format"))

# zarr v2 allowed arbitrary keys here.
# We don't want the ArrayV2Metadata constructor to fail just because someone put an
# extra key in the metadata.
expected = {x.name for x in fields(cls)}
# https://github.com/zarr-developers/zarr-python/issues/2269
# handle the renames
expected |= {"dtype", "chunks"}

_data = {k: v for k, v in _data.items() if k in expected}

return cls(**_data)

def to_dict(self) -> dict[str, JSON]:
Expand Down
1 change: 1 addition & 0 deletions tests/v3/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import pytest

import zarr.api.asynchronous
import zarr.storage
from zarr import Array, AsyncArray, Group
from zarr.codecs.bytes import BytesCodec
from zarr.core.array import chunks_initialized
Expand Down
12 changes: 12 additions & 0 deletions tests/v3/test_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -963,3 +963,15 @@ async def test_open_mutable_mapping():
def test_open_mutable_mapping_sync():
group = zarr.open_group(store={}, mode="w")
assert isinstance(group.store_path.store, MemoryStore)


class TestGroupMetadata:
def test_from_dict_extra_fields(self):
data = {
"attributes": {"key": "value"},
"_nczarr_superblock": {"version": "2.0.0"},
"zarr_format": 2,
}
result = GroupMetadata.from_dict(data)
expected = GroupMetadata(attributes={"key": "value"}, zarr_format=2)
assert result == expected
26 changes: 26 additions & 0 deletions tests/v3/test_metadata/test_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,29 @@ def test_metadata_to_dict(
observed.pop("dimension_separator")

assert observed == expected


def test_from_dict_extra_fields() -> None:
data = {
"_nczarr_array": {"dimrefs": ["/dim1", "/dim2"], "storage": "chunked"},
"attributes": {"key": "value"},
"chunks": [8],
"compressor": None,
"dtype": "<f8",
"fill_value": 0.0,
"filters": None,
"order": "C",
"shape": [8],
"zarr_format": 2,
}

result = ArrayV2Metadata.from_dict(data)
expected = ArrayV2Metadata(
attributes={"key": "value"},
shape=(8,),
dtype="float64",
chunks=(8,),
fill_value=0.0,
order="C",
)
assert result == expected