Skip to content

Commit

Permalink
Raise a ValueError when dtype is not the same
Browse files Browse the repository at this point in the history
  • Loading branch information
FrancescAlted committed Mar 13, 2024
1 parent 6ac26c1 commit 2066abb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
3 changes: 3 additions & 0 deletions blosc2/ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ def __setitem__(self, key, value):
value = np.full(shape, value, dtype=self.dtype)
elif isinstance(value, NDArray):
value = value[...]
elif isinstance(value, np.ndarray):
if value.dtype != self.dtype:
raise ValueError("The dtype of the value should be the same as the array")

return super().set_slice(key, value)

Expand Down
17 changes: 17 additions & 0 deletions tests/ndarray/test_setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,20 @@ def test_setitem(shape, chunks, blocks, slices, dtype):
a[slices] = b
nparray[slices] = b[...]
np.testing.assert_almost_equal(a[...], nparray)


@pytest.mark.parametrize(
"shape, slices",
[
([456], slice(0, 1)),
([77, 134, 13], (slice(3, 7), slice(50, 100), 7)),
([12, 13, 14, 15, 16], (slice(1, 3), ..., slice(3, 6))),
],
)
def test_setitem_different_dtype(shape, slices):
size = int(np.prod(shape))
nparray = np.arange(size, dtype=np.int32).reshape(shape)
a = blosc2.empty(nparray.shape, dtype=np.float64)

with pytest.raises(ValueError):
a[slices] = nparray

0 comments on commit 2066abb

Please sign in to comment.