Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX Use array's device when promoting scalars #73

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion array_api_strict/_array_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ def _promote_scalar(self, scalar):
# behavior for integers within the bounds of the integer dtype.
# Outside of those bounds we use the default NumPy behavior (either
# cast or raise OverflowError).
return Array._new(np.array(scalar, dtype=self.dtype._np_dtype), device=CPU_DEVICE)
return Array._new(np.array(scalar, dtype=self.dtype._np_dtype), device=self.device)

@staticmethod
def _normalize_two_args(x1, x2) -> Tuple[Array, Array]:
Expand Down
10 changes: 9 additions & 1 deletion array_api_strict/tests/test_array_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import pytest

from .. import ones, asarray, result_type, all, equal
from .._array_object import Array, CPU_DEVICE
from .._array_object import Array, CPU_DEVICE, Device
from .._dtypes import (
_all_dtypes,
_boolean_dtypes,
Expand Down Expand Up @@ -88,6 +88,14 @@ def test_validate_index():
assert_raises(IndexError, lambda: a[0])
assert_raises(IndexError, lambda: a[:])

def test_promoted_scalar_inherits_device():
device1 = Device("device1")
x = asarray([1., 2, 3], device=device1)

y = x ** 2
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without the change this line would fail, but I felt like having the assert below is somehow useful for readers from the future, even though it isn't strictly necessary


assert y.device == device1

def test_operators():
# For every operator, we test that it works for the required type
# combinations and raises TypeError otherwise
Expand Down
Loading