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/167.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed unreachable match arms in `parse_dtype_v3` (in both `pydantic_zarr.v3` and `pydantic_zarr.experimental.v3`) where copy-paste errors caused `float64` and `complex64` numpy dtypes to raise `ValueError: Unsupported dtype` instead of returning the correct string names.
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ Source = "https://github.com/zarr-developers/pydantic-zarr"
zarr = ["zarr>=3.0.0"]

[dependency-groups]
# pytest pin is due to https://github.com/pytest-dev/pytest-cov/issues/693
test-base = [
"coverage",
"pytest<8.4",
"pytest==9.1.0",
"pytest-cov",
"pytest-examples",
"xarray==2025.10.0",
Expand All @@ -46,6 +45,7 @@ test = [
"pydantic-zarr[zarr]",
]
docs = [
{include-group = "test-base"},
"mkdocs-material",
"mkdocstrings[python]",
"pytest-examples",
Expand Down
4 changes: 2 additions & 2 deletions src/pydantic_zarr/experimental/v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,9 @@ def parse_dtype_v3(dtype: npt.DTypeLike | Mapping[str, object]) -> Mapping[str,
return "float16"
case np.dtypes.Float32DType():
return "float32"
case np.dtypes.Float16DType():
case np.dtypes.Float64DType():
return "float64"
case np.dtypes.Float32DType():
case np.dtypes.Complex64DType():
return "complex64"
case np.dtypes.Complex128DType():
return "complex128"
Expand Down
17 changes: 13 additions & 4 deletions src/pydantic_zarr/v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,17 @@
FloatFillValue = Literal["Infinity", "-Infinity", "NaN"] | float
ComplexFillValue = tuple[FloatFillValue, FloatFillValue]
RawFillValue = tuple[int, ...]

FillValue = BoolFillValue | IntFillValue | FloatFillValue | ComplexFillValue | RawFillValue | str
StructFillValue = Mapping[str, object]

FillValue = (
BoolFillValue
| IntFillValue
| FloatFillValue
| ComplexFillValue
| RawFillValue
| str
| StructFillValue
)

TName = TypeVar("TName", bound=str)
TConfig = TypeVar("TConfig", bound=Mapping[str, object])
Expand Down Expand Up @@ -161,9 +170,9 @@ def parse_dtype_v3(dtype: npt.DTypeLike | Mapping[str, object]) -> Mapping[str,
return "float16"
case np.dtypes.Float32DType():
return "float32"
case np.dtypes.Float16DType():
case np.dtypes.Float64DType():
return "float64"
case np.dtypes.Float32DType():
case np.dtypes.Complex64DType():
return "complex64"
case np.dtypes.Complex128DType():
return "complex128"
Expand Down
4 changes: 2 additions & 2 deletions tests/test_docs/test_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
SOURCES_ROOT: Path = Path(__file__).parent.parent.parent / "src/pydantic_zarr"


@pytest.mark.parametrize("example", find_examples(str(SOURCES_ROOT)), ids=str)
@pytest.mark.parametrize("example", tuple(find_examples(str(SOURCES_ROOT))), ids=str)
def test_docstrings(example: CodeExample, eval_example: EvalExample) -> None:
eval_example.run_print_check(example)


@pytest.mark.parametrize("example", find_examples("docs"), ids=str)
@pytest.mark.parametrize("example", tuple(find_examples("docs")), ids=str)
def test_docs_examples(example: CodeExample, eval_example: EvalExample) -> None:
pytest.importorskip("zarr")

Expand Down
3 changes: 1 addition & 2 deletions tests/test_pydantic_zarr/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ class DTypeExample:
Int32,
NullTerminatedBytes,
RawBytes,
Structured,
TimeDelta64,
data_type_registry,
)
Expand All @@ -85,7 +84,7 @@ class DTypeExample:
dt = dtype_cls(unit="s", scale_factor=10)
elif dtype_cls in (FixedLengthUTF32, RawBytes, NullTerminatedBytes):
dt = dtype_cls(length=10)
elif dtype_cls == Structured:
elif dtype_cls._zarr_v3_name in ("struct", "structured"):
dt = dtype_cls(fields=[("a", Int32()), ("b", Float16())])
else:
dt = dtype_cls()
Expand Down
30 changes: 30 additions & 0 deletions tests/test_pydantic_zarr/test_experimental/test_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
RegularChunking,
RegularChunkingConfig,
auto_codecs,
parse_dtype_v3,
)

from ..conftest import DTYPE_EXAMPLES_V3, ZARR_AVAILABLE, DTypeExample
Expand Down Expand Up @@ -479,6 +480,35 @@ def test_consolidated_metadata_to_from_zarr() -> None:
assert json.loads(store["zarr.json"].to_bytes()) == json.loads(store2["zarr.json"].to_bytes())


@pytest.mark.parametrize(
("dtype", "expected"),
[
(np.dtype("int8"), "int8"),
(np.dtype("int16"), "int16"),
(np.dtype("int32"), "int32"),
(np.dtype("int64"), "int64"),
(np.dtype("uint8"), "uint8"),
(np.dtype("uint16"), "uint16"),
(np.dtype("uint32"), "uint32"),
(np.dtype("uint64"), "uint64"),
(np.dtype("float16"), "float16"),
(np.dtype("float32"), "float32"),
(np.dtype("float64"), "float64"),
(np.dtype("complex64"), "complex64"),
(np.dtype("complex128"), "complex128"),
],
ids=str,
)
def test_parse_dtype_v3_numpy(dtype: np.dtype, expected: str) -> None:
"""
Regression test: parse_dtype_v3 must correctly handle all supported numpy dtypes.
Previously, the float64 and complex64 match arms were copy-paste errors (using
Float16DType and Float32DType respectively), making those dtypes unreachable and
causing ValueError to be raised for float64 and complex64 inputs.
"""
assert parse_dtype_v3(dtype) == expected


def test_v2_chunk_key_encoding() -> None:
# Simple smoke test to make sure v2 chunk key encoding is allowed
ArraySpec(
Expand Down
30 changes: 30 additions & 0 deletions tests/test_pydantic_zarr/test_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
RegularChunking,
RegularChunkingConfig,
auto_codecs,
parse_dtype_v3,
)

from .conftest import DTYPE_EXAMPLES_V3, DTypeExample
Expand Down Expand Up @@ -313,3 +314,32 @@ def test_v2_chunk_key_encoding() -> None:
fill_value="NaN",
storage_transformers=[],
)


@pytest.mark.parametrize(
("dtype", "expected"),
[
(np.dtype("int8"), "int8"),
(np.dtype("int16"), "int16"),
(np.dtype("int32"), "int32"),
(np.dtype("int64"), "int64"),
(np.dtype("uint8"), "uint8"),
(np.dtype("uint16"), "uint16"),
(np.dtype("uint32"), "uint32"),
(np.dtype("uint64"), "uint64"),
(np.dtype("float16"), "float16"),
(np.dtype("float32"), "float32"),
(np.dtype("float64"), "float64"),
(np.dtype("complex64"), "complex64"),
(np.dtype("complex128"), "complex128"),
],
ids=str,
)
def test_parse_dtype_v3_numpy(dtype: np.dtype, expected: str) -> None:
"""
Regression test: parse_dtype_v3 must correctly handle all supported numpy dtypes.
Previously, the float64 and complex64 match arms were copy-paste errors (using
Float16DType and Float32DType respectively), making those dtypes unreachable and
causing ValueError to be raised for float64 and complex64 inputs.
"""
assert parse_dtype_v3(dtype) == expected
Loading