Skip to content

Commit d265b48

Browse files
committed
Squashed commit of the following:
commit c9aedcd Author: Tom Augspurger <[email protected]> Date: Sun Sep 29 20:43:07 2024 -0500 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 zarr-developers#2269
1 parent ae02bb5 commit d265b48

4 files changed

Lines changed: 29 additions & 7 deletions

File tree

‎src/zarr/core/array.py‎

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

379379
metadata = ArrayV2Metadata(
380380
shape=shape,
381-
dtype=np.dtype(dtype),
382-
chunks=chunks,
381+
data_type=np.dtype(dtype),
382+
chunk_grid=chunks,
383383
order=order,
384384
dimension_separator=dimension_separator,
385385
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"
@@ -134,11 +136,15 @@ def parse_named_configuration(
134136
return name_parsed, configuration_parsed
135137

136138

137-
def parse_shapelike(data: int | Iterable[int]) -> tuple[int, ...]:
139+
def parse_shapelike(data: int | Iterable[int] | RegularChunkGrid) -> tuple[int, ...]:
140+
from zarr.core.chunk_grids import RegularChunkGrid
141+
138142
if isinstance(data, int):
139143
if data < 0:
140144
raise ValueError(f"Expected a non-negative integer. Got {data} instead")
141145
return (data,)
146+
elif isinstance(data, RegularChunkGrid):
147+
return data.chunk_shape
142148
try:
143149
data_tuple = tuple(data)
144150
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)