Skip to content

Commit

Permalink
Check complex numbers explicitly
Browse files Browse the repository at this point in the history
  • Loading branch information
dcherian committed Oct 23, 2024
1 parent d5352f6 commit 7e2b28b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
12 changes: 10 additions & 2 deletions src/zarr/core/metadata/v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,12 +485,20 @@ def parse_fill_value(
pass
elif fill_value in ["Infinity", "-Infinity"] and not np.isfinite(casted_value):
pass
elif np_dtype.kind in "cf":
elif np_dtype.kind == "f":
# float comparison is not exact, especially when dtype <float64
# so we us np.isclose for this comparison.
# so we use np.isclose for this comparison.
# this also allows us to compare nan fill_values
if not np.isclose(fill_value, casted_value, equal_nan=True):
raise ValueError(f"fill value {fill_value!r} is not valid for dtype {data_type}")
elif np_dtype.kind == "c":
# confusingly np.isclose(np.inf, np.inf + 0j) is False, so compare real and imag parts
# explicitly.
if not (
np.isclose(np.real(fill_value), np.real(casted_value), equal_nan=True)
and np.isclose(np.imag(fill_value), np.imag(casted_value), equal_nan=True)
):
raise ValueError(f"fill value {fill_value!r} is not valid for dtype {data_type}")
else:
if fill_value != casted_value:
raise ValueError(f"fill value {fill_value!r} is not valid for dtype {data_type}")
Expand Down
3 changes: 2 additions & 1 deletion tests/test_array.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import math
import pickle
from itertools import accumulate
from typing import Any, Literal
Expand Down Expand Up @@ -448,7 +449,7 @@ def test_array_create_order(
(np.inf, ["Infinity", 0.0]),
(np.inf * 1j, ["NaN", "Infinity"]),
(-np.inf, ["-Infinity", 0.0]),
# (math.inf, ["Infinity", 0.0]),
(math.inf, ["Infinity", 0.0]),
],
)
async def test_special_complex_fill_values_roundtrip(fill_value: Any, expected: list[Any]) -> None:
Expand Down

0 comments on commit 7e2b28b

Please sign in to comment.