Skip to content

Commit 7e2b28b

Browse files
committed
Check complex numbers explicitly
1 parent d5352f6 commit 7e2b28b

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/zarr/core/metadata/v3.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,12 +485,20 @@ def parse_fill_value(
485485
pass
486486
elif fill_value in ["Infinity", "-Infinity"] and not np.isfinite(casted_value):
487487
pass
488-
elif np_dtype.kind in "cf":
488+
elif np_dtype.kind == "f":
489489
# float comparison is not exact, especially when dtype <float64
490-
# so we us np.isclose for this comparison.
490+
# so we use np.isclose for this comparison.
491491
# this also allows us to compare nan fill_values
492492
if not np.isclose(fill_value, casted_value, equal_nan=True):
493493
raise ValueError(f"fill value {fill_value!r} is not valid for dtype {data_type}")
494+
elif np_dtype.kind == "c":
495+
# confusingly np.isclose(np.inf, np.inf + 0j) is False, so compare real and imag parts
496+
# explicitly.
497+
if not (
498+
np.isclose(np.real(fill_value), np.real(casted_value), equal_nan=True)
499+
and np.isclose(np.imag(fill_value), np.imag(casted_value), equal_nan=True)
500+
):
501+
raise ValueError(f"fill value {fill_value!r} is not valid for dtype {data_type}")
494502
else:
495503
if fill_value != casted_value:
496504
raise ValueError(f"fill value {fill_value!r} is not valid for dtype {data_type}")

tests/test_array.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
import math
23
import pickle
34
from itertools import accumulate
45
from typing import Any, Literal
@@ -448,7 +449,7 @@ def test_array_create_order(
448449
(np.inf, ["Infinity", 0.0]),
449450
(np.inf * 1j, ["NaN", "Infinity"]),
450451
(-np.inf, ["-Infinity", 0.0]),
451-
# (math.inf, ["Infinity", 0.0]),
452+
(math.inf, ["Infinity", 0.0]),
452453
],
453454
)
454455
async def test_special_complex_fill_values_roundtrip(fill_value: Any, expected: list[Any]) -> None:

0 commit comments

Comments
 (0)