Skip to content

Commit c2451b3

Browse files
guhou-hvid-v-b
andauthored
fix: accept uint8 dtype aliases in Zarr v2 (#4381)
* Fix Zarr v2 uint8 dtype aliases (GH3622) * docs: associate changelog with PR 4381 --------- Co-authored-by: Davis Bennett <[email protected]>
1 parent 7291469 commit c2451b3

4 files changed

Lines changed: 59 additions & 1 deletion

File tree

‎changes/4381.bugfix.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Zarr format 2 arrays with a `<u1` or `>u1` data type can now be read as unsigned 8-bit integers. Serialized metadata continues to use the canonical `|u1` spelling.

‎src/zarr/core/dtype/npy/int.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,11 @@ class UInt8(BaseInt[np.dtypes.UInt8DType, np.uint8]):
415415

416416
dtype_cls = np.dtypes.UInt8DType
417417
_zarr_v3_name: ClassVar[Literal["uint8"]] = "uint8"
418-
_zarr_v2_names: ClassVar[tuple[Literal["|u1"]]] = ("|u1",)
418+
_zarr_v2_names: ClassVar[tuple[Literal["|u1"], Literal["<u1"], Literal[">u1"]]] = (
419+
"|u1",
420+
"<u1",
421+
">u1",
422+
)
419423

420424
@classmethod
421425
def from_native_dtype(cls, dtype: TBaseDType) -> Self:

‎tests/test_dtype/test_npy/test_int.py‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
from __future__ import annotations
22

33
import numpy as np
4+
import pytest
45

56
from tests.test_dtype.test_wrapper import BaseTestZDType
7+
from zarr.core.dtype import get_data_type_from_json
68
from zarr.core.dtype.npy.int import Int8, Int16, Int32, Int64, UInt8, UInt16, UInt32, UInt64
9+
from zarr.errors import DataTypeValidationError
710

811

912
class TestInt8(BaseTestZDType):
@@ -329,3 +332,30 @@ def test_string_integer_from_json_scalar() -> None:
329332
# Test that it works for v2 format too
330333
result = dtype_instance.from_json_scalar("123", zarr_format=2)
331334
assert result == np.int32(123)
335+
336+
337+
@pytest.mark.parametrize("name", ["|u1", "<u1", ">u1"])
338+
def test_uint8_v2_aliases(name: str) -> None:
339+
data = {"name": name, "object_codec_id": None}
340+
dtype = UInt8.from_json(data, zarr_format=2)
341+
assert dtype == UInt8()
342+
assert get_data_type_from_json(data, zarr_format=2) == dtype
343+
assert dtype.to_native_dtype() == np.dtype("uint8")
344+
serialized = dtype.to_json(zarr_format=2)
345+
assert serialized["name"] == "|u1"
346+
assert serialized["object_codec_id"] is None
347+
assert dtype.to_json(zarr_format=3) == "uint8"
348+
with pytest.raises(DataTypeValidationError):
349+
UInt8.from_json(name, zarr_format=3)
350+
351+
352+
@pytest.mark.parametrize("name", ["|u1", "<u1", ">u1"])
353+
def test_uint8_v2_aliases_reject_object_codec(name: str) -> None:
354+
with pytest.raises(DataTypeValidationError):
355+
UInt8.from_json({"name": name, "object_codec_id": "vlen-utf8"}, zarr_format=2)
356+
357+
358+
@pytest.mark.parametrize("name", ["|i1", "<u2", ">u2", "uint8", "invalid"])
359+
def test_uint8_v2_rejects_other_types(name: str) -> None:
360+
with pytest.raises(DataTypeValidationError):
361+
UInt8.from_json({"name": name, "object_codec_id": None}, zarr_format=2)

‎tests/test_metadata/test_v2.py‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,3 +399,26 @@ def test_structured_dtype_fill_value_serialization(
399399
root_group = zarr.open_group(group_path, mode="r")
400400
observed = root_group.metadata.consolidated_metadata.metadata["structured_dtype"].fill_value # type: ignore[union-attr]
401401
assert observed == fill_value
402+
403+
404+
@pytest.mark.parametrize("dtype", ["|u1", "<u1", ">u1"])
405+
def test_open_uint8_dtype_aliases(tmp_path: Path, dtype: str) -> None:
406+
metadata = {
407+
"zarr_format": 2,
408+
"shape": [3],
409+
"chunks": [3],
410+
"dtype": dtype,
411+
"compressor": None,
412+
"fill_value": 0,
413+
"order": "C",
414+
"filters": None,
415+
}
416+
metadata_path = tmp_path / ".zarray"
417+
metadata_path.write_text(json.dumps(metadata))
418+
(tmp_path / "0").write_bytes(bytes([0, 128, 255]))
419+
420+
array = zarr.open_array(tmp_path, mode="r")
421+
assert array.dtype == np.dtype("uint8")
422+
np.testing.assert_array_equal(array[:], np.array([0, 128, 255], dtype=np.uint8))
423+
assert array.metadata.to_dict()["dtype"] == "|u1"
424+
assert json.loads(metadata_path.read_text()) == metadata

0 commit comments

Comments
 (0)