Skip to content

Commit 0c76778

Browse files
ilan-goldd-v-b
andauthored
(fix): structured dtype fill value consolidated metadata (#3015)
* (fix): structured dtype consolidated metadata fill value * (chore): relnote * (chore): test * (fix): more robust testing --------- Co-authored-by: Davis Bennett <[email protected]>
1 parent 0351c4e commit 0c76778

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

changes/2998.bugfix.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix structured `dtype` fill value serialization for consolidated metadata

src/zarr/core/group.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import asyncio
4+
import base64
45
import itertools
56
import json
67
import logging
@@ -358,7 +359,13 @@ def to_buffer_dict(self, prototype: BufferPrototype) -> dict[str, Buffer]:
358359
d[f"{k}/{ZATTRS_JSON}"] = _replace_special_floats(attrs)
359360
if "shape" in v:
360361
# it's an array
361-
d[f"{k}/{ZARRAY_JSON}"] = _replace_special_floats(v)
362+
if isinstance(v.get("fill_value", None), np.void):
363+
v["fill_value"] = base64.standard_b64encode(
364+
cast(bytes, v["fill_value"])
365+
).decode("ascii")
366+
else:
367+
v = _replace_special_floats(v)
368+
d[f"{k}/{ZARRAY_JSON}"] = v
362369
else:
363370
d[f"{k}/{ZGROUP_JSON}"] = {
364371
"zarr_format": self.zarr_format,

tests/test_metadata/test_v2.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,3 +316,28 @@ def test_zstd_checksum() -> None:
316316
arr.metadata.to_buffer_dict(default_buffer_prototype())[".zarray"].to_bytes()
317317
)
318318
assert "checksum" not in metadata["compressor"]
319+
320+
321+
@pytest.mark.parametrize(
322+
"fill_value", [None, np.void((0, 0), np.dtype([("foo", "i4"), ("bar", "i4")]))]
323+
)
324+
def test_structured_dtype_fill_value_serialization(tmp_path, fill_value):
325+
group_path = tmp_path / "test.zarr"
326+
root_group = zarr.open_group(group_path, mode="w", zarr_format=2)
327+
dtype = np.dtype([("foo", "i4"), ("bar", "i4")])
328+
root_group.create_array(
329+
name="structured_dtype",
330+
shape=(100, 100),
331+
chunks=(100, 100),
332+
dtype=dtype,
333+
fill_value=fill_value,
334+
)
335+
336+
zarr.consolidate_metadata(root_group.store, zarr_format=2)
337+
root_group = zarr.open_group(group_path, mode="r")
338+
assert (
339+
root_group.metadata.consolidated_metadata.to_dict()["metadata"]["structured_dtype"][
340+
"fill_value"
341+
]
342+
== fill_value
343+
)

0 commit comments

Comments
 (0)