Skip to content

Commit c9aedcd

Browse files
committed
Fixed ArrayV2Metadata parameter names
For things like dataclasses.repalce to work, we need the parameter names to match the attribute names. All the name change between what we have in memory and what the specs requires should happen during serialization / deserialization. Closes #2269
1 parent 6454c69 commit c9aedcd

File tree

4 files changed

+29
-7
lines changed

4 files changed

+29
-7
lines changed

src/zarr/core/array.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,8 +372,8 @@ async def _create_v2(
372372

373373
metadata = ArrayV2Metadata(
374374
shape=shape,
375-
dtype=np.dtype(dtype),
376-
chunks=chunks,
375+
data_type=np.dtype(dtype),
376+
chunk_grid=chunks,
377377
order=order,
378378
dimension_separator=dimension_separator,
379379
fill_value=0 if fill_value is None else fill_value,

src/zarr/core/common.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
if TYPE_CHECKING:
2020
from collections.abc import Awaitable, Callable, Iterator
2121

22+
from zarr.core.chunk_grids import RegularChunkGrid
23+
2224

2325
ZARR_JSON = "zarr.json"
2426
ZARRAY_JSON = ".zarray"
@@ -133,11 +135,15 @@ def parse_named_configuration(
133135
return name_parsed, configuration_parsed
134136

135137

136-
def parse_shapelike(data: int | Iterable[int]) -> tuple[int, ...]:
138+
def parse_shapelike(data: int | Iterable[int] | RegularChunkGrid) -> tuple[int, ...]:
139+
from zarr.core.chunk_grids import RegularChunkGrid
140+
137141
if isinstance(data, int):
138142
if data < 0:
139143
raise ValueError(f"Expected a non-negative integer. Got {data} instead")
140144
return (data,)
145+
elif isinstance(data, RegularChunkGrid):
146+
return data.chunk_shape
141147
try:
142148
data_tuple = tuple(data)
143149
except TypeError as e:

src/zarr/core/metadata/v2.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ def __init__(
4343
self,
4444
*,
4545
shape: ChunkCoords,
46-
dtype: npt.DTypeLike,
47-
chunks: ChunkCoords,
46+
data_type: npt.DTypeLike,
47+
chunk_grid: ChunkCoords,
4848
fill_value: Any,
4949
order: Literal["C", "F"],
5050
dimension_separator: Literal[".", "/"] = ".",
@@ -56,8 +56,8 @@ def __init__(
5656
Metadata for a Zarr version 2 array.
5757
"""
5858
shape_parsed = parse_shapelike(shape)
59-
data_type_parsed = parse_dtype(dtype)
60-
chunks_parsed = parse_shapelike(chunks)
59+
data_type_parsed = parse_dtype(data_type)
60+
chunks_parsed = parse_shapelike(chunk_grid)
6161
compressor_parsed = parse_compressor(compressor)
6262
order_parsed = parse_indexing_order(order)
6363
dimension_separator_parsed = parse_separator(dimension_separator)
@@ -140,6 +140,9 @@ def from_dict(cls, data: dict[str, Any]) -> ArrayV2Metadata:
140140
_data = data.copy()
141141
# check that the zarr_format attribute is correct
142142
_ = parse_zarr_format(_data.pop("zarr_format"))
143+
144+
_data["chunk_grid"] = _data.pop("chunks")
145+
_data["data_type"] = _data.pop("dtype")
143146
return cls(**_data)
144147

145148
def to_dict(self) -> dict[str, JSON]:

tests/v3/test_v2.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,16 @@ def test_codec_pipeline() -> None:
4646
result = array[:]
4747
expected = np.ones(1)
4848
np.testing.assert_array_equal(result, expected)
49+
50+
51+
def test_attrs(store: StorePath) -> None:
52+
data = np.arange(0, 8, dtype="uint16")
53+
a = Array.create(
54+
store / "simple_v2",
55+
zarr_format=2,
56+
shape=data.shape,
57+
chunks=(4,),
58+
dtype=data.dtype,
59+
fill_value=0,
60+
)
61+
a.attrs.put({"key": 0})

0 commit comments

Comments
 (0)