data-apis / array-api-tests

Test suite for the PyData Array APIs standard
https://data-apis.org/array-api-tests/
MIT License
64 stars 41 forks source link

Avoid inplace operators in general tests #287

Open Illviljan opened 2 months ago

Illviljan commented 2 months ago

numpy returns scalars instead of 0d arrays:

import numpy as np
import array_api_strict as xps

xps.mean(xps.asarray(4, dtype=xps.float32)).__iadd__(1)
np.mean(np.asarray(4, dtype=np.float32)).__iadd__(1)  # AttributeError: 'numpy.float32' object has no attribute '__iadd__'

And this causes errors in unrelated tests since inplace operators are used in more generic tests: https://github.com/data-apis/array-api-tests/blob/4caff2867c69d5b9b329320319b61c5444bf6f87/array_api_tests/pytest_helpers.py#L483

I don't think using inplace-operators in this case is particularly important and can probably be switched to a normal bitwise or.

xref: https://github.com/numpy/numpy/issues/27305

asmeurer commented 2 months ago

Is there a specific test that's failing because of this? I haven't seen it before.

I'm a little confused why this would be an issue, since Python automatically converts a += b into a = a + b if a.__iadd__ is not defined. For instance, that line you showed works with NumPy scalars

>>> a = np.bool_(False)
>>> b = np.bool_(True)
>>> a |= b
>>> a
True

even though a.__ior__ is not defined.

There are tests that test in-place operators directly, which do directly call the __i*__ methods https://github.com/data-apis/array-api-tests/blob/b4c0823469c02d6ce6e512ad4c2bd8ba42b1b4b2/array_api_tests/test_operators_and_elementwise_functions.py#L609, but outside of those I don't think the dunder methods are ever called directly.

Actually, there's a general issue in the test suite which is that the hypothesis strategies for arrays do not actually generate NumPy scalars. So right now, NumPy scalars are not really tested at all, except in a few places inside of some tests where they are created from indexing. We should really fix this because there are likely several issues with NumPy scalars and the array API (I've already seen others outside of the one you noted here).

Illviljan commented 1 month ago

I suppose it becomes a problem once using __ior__ directly? That's what I've been doing. I've been following array-api-strict's example. Array-api-strict cleverly avoids this problem since it actively forces to 0d-arrays instead of scalars.

Here's 1 example, but I've seen it in several other tests as well:

 _________________________________ test_unstack _________________________________

    @pytest.mark.min_version("2023.12")
>   @given(x=hh.arrays(dtype=hh.all_dtypes, shape=hh.shapes(min_dims=1)), data=st.data())

array_api_tests/test_manipulation_functions.py:445: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
array_api_tests/test_manipulation_functions.py:467: in test_unstack
    ph.assert_array_elements("unstack", out=arr, expected=x[tuple(idx)], kw=kw, out_repr=f"out[{i}]")
array_api_tests/pytest_helpers.py:483: in _real_float_strict_equals
    ignore_mask |= out_zero_mask
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <Namedarray, shape=(), dims=(), dtype=bool, data=False>
other = <Namedarray, shape=(), dims=(), dtype=bool, data=True>

    def __ior__(self, other: int | bool | NamedArray, /):
        from xarray.namedarray._array_api import asarray

>       self._data.__ior__(self._maybe_asarray(other)._data)
E       AttributeError: 'numpy.bool' object has no attribute '__ior__'. Did you mean: '__or__'?
E       Falsifying example: test_unstack(
E           x=<Namedarray, shape=(1,), dims=('dim_0',), dtype=float32, data=[0.]>,
E           data=data(...),
E       )
E       Draw 1 (axis): -1
E       Draw 2 (kw): {'axis': -1}
E       Explanation:
E           These lines were always and only run by failing examples:
E               /home/runner/work/xarray/xarray/array-api-tests/array_api_tests/pytest_helpers.py:460
E               /home/runner/work/xarray/xarray/array-api-tests/array_api_tests/pytest_helpers.py:464
E               /home/runner/work/xarray/xarray/array-api-tests/array_api_tests/pytest_helpers.py:466
E               /home/runner/work/xarray/xarray/array-api-tests/array_api_tests/pytest_helpers.py:472
E               /home/runner/work/xarray/xarray/array-api-tests/array_api_tests/pytest_helpers.py:473
E               (and 23 more with settings.verbosity >= verbose)
E       
E       You can reproduce this example by temporarily adding @reproduce_failure('6.111.2', b'AXic42RkZEAGjAAAygAN') as a decorator on your test case

https://github.com/pydata/xarray/actions/runs/10655652392/job/29533434595

You do make a good point of using the operators instead! I'll try that out.