Skip to content

Commit 9c2efdd

Browse files
committed
(fix): dtype encoding roundtrip
1 parent 7898ef8 commit 9c2efdd

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/zarr/core/metadata/v2.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,12 @@ def to_dict(self) -> dict[str, JSON]:
193193
zarray_dict["fill_value"] = fill_value
194194

195195
_ = zarray_dict.pop("dtype")
196-
zarray_dict["dtype"] = self.dtype.str
196+
dtype_json: JSON
197+
if self.dtype.kind == "V":
198+
dtype_json = tuple(self.dtype.descr)
199+
else:
200+
dtype_json = self.dtype.str
201+
zarray_dict["dtype"] = dtype_json
197202

198203
return zarray_dict
199204

@@ -220,6 +225,8 @@ def update_attributes(self, attributes: dict[str, JSON]) -> Self:
220225

221226

222227
def parse_dtype(data: npt.DTypeLike) -> np.dtype[Any]:
228+
if isinstance(data, list): # this is a valid _VoidDTypeLike check
229+
data = [tuple(d) for d in data]
223230
return np.dtype(data)
224231

225232

tests/test_v2.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,15 +246,22 @@ def test_default_filters_and_compressor(dtype_expected: Any) -> None:
246246

247247

248248
@pytest.mark.parametrize("fill_value", [None, (b"", 0, 0.0)], ids=["no_fill", "fill"])
249-
def test_structured_dtype(fill_value, tmp_path) -> None:
249+
def test_structured_dtype_roundtrip(fill_value, tmp_path) -> None:
250250
a = np.array(
251251
[(b"aaa", 1, 4.2), (b"bbb", 2, 8.4), (b"ccc", 3, 12.6)],
252252
dtype=[("foo", "S3"), ("bar", "i4"), ("baz", "f8")],
253253
)
254+
array_path = tmp_path / "data.zarr"
254255
za = zarr.create(
255-
shape=(3,), path=tmp_path, chunks=(2,), fill_value=fill_value, zarr_format=2, dtype=a.dtype
256+
shape=(3,),
257+
store=array_path,
258+
chunks=(2,),
259+
fill_value=fill_value,
260+
zarr_format=2,
261+
dtype=a.dtype,
256262
)
257263
if fill_value is not None:
258264
assert (np.array([fill_value] * a.shape[0], dtype=a.dtype) == za[:]).all()
259265
za[...] = a
266+
za = zarr.open_array(store=array_path)
260267
assert (a == za[:]).all()

0 commit comments

Comments
 (0)