Skip to content

Fix unreachable match arms in parse_dtype_v3 (float64/complex64 unsupported)#167

Merged
d-v-b merged 9 commits into
zarr-developers:mainfrom
d-v-b:fix/parse-dtype-v3
Jun 19, 2026
Merged

Fix unreachable match arms in parse_dtype_v3 (float64/complex64 unsupported)#167
d-v-b merged 9 commits into
zarr-developers:mainfrom
d-v-b:fix/parse-dtype-v3

Conversation

@d-v-b

@d-v-b d-v-b commented Jun 13, 2026

Copy link
Copy Markdown
Collaborator

Bug

parse_dtype_v3 in src/pydantic_zarr/v3.py and src/pydantic_zarr/experimental/v3.py had two copy-paste errors in its match statement. The third and fourth float/complex arms used the wrong type classes:

# Before (broken)
case np.dtypes.Float16DType():   return "float16"
case np.dtypes.Float32DType():   return "float32"
case np.dtypes.Float16DType():   return "float64"    # unreachable: should be Float64DType
case np.dtypes.Float32DType():   return "complex64"  # unreachable: should be Complex64DType
case np.dtypes.Complex128DType(): return "complex128"

Because Python's structural pattern matching skips duplicate arms, the float64 and complex64 cases were unreachable and fell through to the case _: arm, raising ValueError.

Reproduction:

from pydantic_zarr.v3 import parse_dtype_v3
import numpy as np

parse_dtype_v3(np.dtype("float64"))   # raises ValueError: Unsupported dtype: float64
parse_dtype_v3(np.dtype("complex64")) # raises ValueError: Unsupported dtype: complex64

Fix

Replace the two incorrect match arms:

  • np.dtypes.Float16DType()np.dtypes.Float64DType() returning "float64"
  • np.dtypes.Float32DType()np.dtypes.Complex64DType() returning "complex64"

Applied in both pydantic_zarr.v3 and pydantic_zarr.experimental.v3.

Additional changes

  • Added parametrized regression tests covering all 13 supported numpy dtypes (int8/16/32/64, uint8/16/32/64, float16/32/64, complex64/128) passed as np.dtype(...) objects through parse_dtype_v3, in both tests/test_pydantic_zarr/test_v3.py and tests/test_pydantic_zarr/test_experimental/test_v3.py.
  • Fixed the test conftest to handle zarr ≥3.2.1's new Struct dtype class (a subclass of Structured) by using issubclass instead of ==, which was causing a TypeError during test collection.
  • Bumped python_version in [tool.mypy] from "3.11" to "3.12" to match zarr ≥3.2.0's minimum Python requirement (zarr 3.2.0+ uses Python 3.12 type X = ... syntax).

Fixes part of #165.

🤖 Generated with Claude Code

d-v-b and others added 3 commits June 13, 2026 14:45
Two match arms in `parse_dtype_v3` (in both `pydantic_zarr.v3` and
`pydantic_zarr.experimental.v3`) were copy-paste errors: the third arm
used `Float16DType` instead of `Float64DType`, and the fourth used
`Float32DType` instead of `Complex64DType`. This made `float64` and
`complex64` dtypes unreachable, causing `ValueError: Unsupported dtype`
for those types. Fixes part of zarr-developers#165.

Also fixes the test conftest to handle zarr 3.2.1's new `Struct` dtype
class (a subclass of `Structured`) by using `issubclass` instead of
`==` in the special-case branch, and bumps the mypy `python_version`
to 3.12 to match zarr >=3.2.0's minimum Python requirement.

Adds parametrized regression tests covering all 13 supported numpy
dtypes through `parse_dtype_v3` in both test suites.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@d-v-b d-v-b merged commit cdb2130 into zarr-developers:main Jun 19, 2026
16 checks passed
@d-v-b d-v-b deleted the fix/parse-dtype-v3 branch June 19, 2026 10:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant