Skip to content

__setitem__ should disallow invalid type promotions #136

Open
@crusaderky

Description

@crusaderky

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions