Skip to content

__setitem__ should disallow invalid type promotions #136

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

Open
crusaderky opened this issue Mar 25, 2025 · 0 comments
Open

__setitem__ should disallow invalid type promotions #136

crusaderky opened this issue Mar 25, 2025 · 0 comments

Comments

@crusaderky
Copy link
Contributor

crusaderky commented Mar 25, 2025

binops correctly raise when the user tries to mix int, float, and bool dtypes, and in general in all use cases that the Type Promotion Rules declare as "undefined behaviour".

__setitem__ however quietly casts the rhs to the dtype of the input.

>>> import array_api_strict as xp
>>> a = xp.asarray([1,2])
>>> b = xp.asarray(3.5)
>>> xp.maximum(a, b)
TypeError: array_api_strict.int64 and array_api_strict.float64 cannot be type promoted together
>>> xp.maximum(a, 3.5)
TypeError: Python float scalars can only be promoted with floating-point arrays.
>>> a[0] = b  # No error
>>> a[1] = 4.5  # No error
>>> a
Out[12]: Array([3, 4], dtype=array_api_strict.int64)

__iadd__ et al behave as expected:

>>> a += b
TypeError: Cannot perform __iadd__ with dtypes array_api_strict.uint8 and array_api_strict.int64
>>> a += 4.5
TypeError: Python float scalars can only be promoted with floating-point arrays.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant