Skip to content

allow mixed -type comparisons #76

Closed
@ev-br

Description

@ev-br

Somewhat similar to #51 (I've still no idea how to open an enhancement request against the standard itself).
The difference to #51 is that one can argue that int/int division is float; for comparisons however the limitation to matching dtypes serves no obvious purpose.

In [1]: import array_api_strict as xp

In [2]: xp.arange(5) < 3.0
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[2], line 1
----> 1 xp.arange(5) < 3.0

File ~/miniforge3/envs/scipy-dev/lib/python3.12/site-packages/array_api_strict/_array_object.py:717, in Array.__lt__(self, other)
    713 def __lt__(self: Array, other: Union[int, float, Array], /) -> Array:
    714     """
    715     Performs the operation __lt__.
    716     """
--> 717     other = self._check_allowed_dtypes(other, "real numeric", "__lt__")
    718     if other is NotImplemented:
    719         return other

File ~/miniforge3/envs/scipy-dev/lib/python3.12/site-packages/array_api_strict/_array_object.py:169, in Array._check_allowed_dtypes(self, other, dtype_category, op)
    167     raise TypeError(f"Only {dtype_category} dtypes are allowed in {op}")
    168 if isinstance(other, (int, complex, float, bool)):
--> 169     other = self._promote_scalar(other)
    170 elif isinstance(other, Array):
    171     if other.dtype not in _dtype_categories[dtype_category]:

File ~/miniforge3/envs/scipy-dev/lib/python3.12/site-packages/array_api_strict/_array_object.py:229, in Array._promote_scalar(self, scalar)
    227 elif isinstance(scalar, float):
    228     if self.dtype not in _floating_dtypes:
--> 229         raise TypeError(
    230             "Python float scalars can only be promoted with floating-point arrays."
    231         )
    232 elif isinstance(scalar, complex):
    233     if self.dtype not in _complex_floating_dtypes:

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