Open
Description
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:
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
Activity
asmeurer commentedon 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
intoa = a + b
ifa.__iadd__
is not defined. For instance, that line you showed works with NumPy scalarseven though
a.__ior__
is not defined.There are tests that test in-place operators directly, which do directly call the
__i*__
methodsarray-api-tests/array_api_tests/test_operators_and_elementwise_functions.py
Line 609 in b4c0823
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 commentedon 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'sexample. 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:
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.