Skip to content

Commit 875e4c7

Browse files
authored
Merge pull request data-apis#209 from ev-br/setitem_device
ENH: make __setitem__ raise on a device mismatch
2 parents 34f301f + 5d0a185 commit 875e4c7

2 files changed

Lines changed: 12 additions & 0 deletions

File tree

array_api_strict/_array_object.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,10 @@ def __setitem__(
962962
other = value
963963
if isinstance(value, (bool, int, float, complex)):
964964
other = self._promote_scalar(value)
965+
else:
966+
if value.device != self.device:
967+
raise ValueError(f"mismatched devices: {self.device = } != {value.device =}.")
968+
965969
dt = _result_type(self.dtype, other.dtype)
966970
if dt != self.dtype:
967971
raise TypeError(f"mismatched dtypes: {self.dtype = } and {other.dtype = }")

array_api_strict/tests/test_array_object.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,14 @@ def test_setitem_invalid_promotions():
221221
a[0] = asarray(3.5j, dtype=complex128)
222222

223223

224+
def test_setitem_device_transfer():
225+
a = arange(3)
226+
b = arange(4, 1, -1, device=Device('device1'))
227+
228+
with pytest.raises(ValueError):
229+
a[:] = b[:]
230+
231+
224232
def test_promoted_scalar_inherits_device():
225233
device1 = Device("device1")
226234
x = asarray([1., 2, 3], device=device1)

0 commit comments

Comments
 (0)