Skip to content

Commit

Permalink
check that fill value is valid for dtype
Browse files Browse the repository at this point in the history
  • Loading branch information
jhamman committed Sep 20, 2024
1 parent 0012508 commit 44d310b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/zarr/core/metadata/v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,17 @@ def parse_fill_value(
raise ValueError(msg)
msg = f"Cannot parse non-string sequence {fill_value} as a scalar with type {dtype}."
raise TypeError(msg)
return dtype.type(fill_value) # type: ignore[arg-type]

# Cast the fill_value to the given dtype
try:
casted_value = np.dtype(dtype).type(fill_value)
except (ValueError, OverflowError, TypeError) as e:
raise ValueError(f"fill value {fill_value!r} is not valid for dtype {dtype}") from e
# Check if the value is still representable by the dtype
if fill_value != casted_value:
raise ValueError(f"fill value {fill_value!r} is not valid for dtype {dtype}")

return casted_value


# For type checking
Expand Down
18 changes: 18 additions & 0 deletions tests/v3/test_metadata/test_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,3 +277,21 @@ async def test_invalid_dtype_raises() -> None:
def test_parse_invalid_dtype_raises(data):
with pytest.raises(ValueError, match=r"Invalid V3 data_type"):
parse_dtype(data)


@pytest.mark.parametrize(
"data_type,fill_value", [("uint8", -1), ("int32", 22.5), ("float32", "foo")]
)
async def test_invalid_fill_value_raises(data_type: str, fill_value: int | float) -> None:
metadata_dict = {
"zarr_format": 3,
"node_type": "array",
"shape": (1,),
"chunk_grid": {"name": "regular", "configuration": {"chunk_shape": (1,)}},
"data_type": data_type,
"chunk_key_encoding": {"name": "default", "separator": "."},
"codecs": (),
"fill_value": fill_value, # this is not a valid fill value for uint8
}
with pytest.raises(ValueError, match=rf"fill value .* is not valid for dtype {data_type}"):
ArrayV3Metadata.from_dict(metadata_dict)

0 comments on commit 44d310b

Please sign in to comment.