Skip to content
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
1 change: 1 addition & 0 deletions changes/4495.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Structured data types now default byte-string (`S`) and raw-bytes (`V`) fields to zero bytes. Before, an array with a raw-bytes field failed to create without an explicit fill value, and byte-string fields defaulted to `b"0"`.
2 changes: 1 addition & 1 deletion src/zarr/core/dtype/npy/structured.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ def default_scalar(self) -> np.void:
# zero count is representable by viewing the integer storage.
value = np.zeros(1, dtype=dtype.byteorder + "i8").view(dtype)[0]
else:
value = np.array([0], dtype=dtype)[0]
value = np.zeros(1, dtype=dtype)[0]
values.append(value)
return self._cast_scalar_unchecked(tuple(values))

Expand Down
9 changes: 9 additions & 0 deletions tests/test_dtype/test_npy/test_structured.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,3 +392,12 @@ def test_subarray_structured_dtype_raises() -> None:
dtype = np.dtype([("f0", "i4", (2,))])
with pytest.raises(ValueError, match="field 'f0' is a subarray"):
Struct.from_native_dtype(dtype)


@pytest.mark.parametrize("field_dtype", ["S4", "V4"])
def test_default_scalar_bytes_field_is_zero(field_dtype: str) -> None:
"""
The default scalar of a bytes field is zero bytes, as for the same dtype outside a struct.
"""
dtype = np.dtype([("a", field_dtype), ("b", "<f4")])
assert Struct.from_native_dtype(dtype).default_scalar().tobytes() == bytes(dtype.itemsize)
Loading