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

Avoid inplace operators in general tests #287

Open
Illviljan opened this issue Sep 1, 2024 · 2 comments
Open

Avoid inplace operators in general tests #287

Illviljan opened this issue Sep 1, 2024 · 2 comments

Comments

@Illviljan
Copy link

Illviljan commented Sep 1, 2024

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:

ignore_mask |= out_zero_mask

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

xref: numpy/numpy#27305

@asmeurer
Copy link
Member

asmeurer commented Sep 3, 2024

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

, 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
Copy link
Author

Illviljan commented Sep 5, 2024

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.

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

2 participants