You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Right now the array type implements __array__, but just as convenience. This was done back when the package was numpy.array_api.
I'm wondering if we should actually remove this. It isn't part of the array API standard, and could just lead people incorrectly thinking it works. And more notably, it is leading to bugs in array_api_strict itself (like #66) where you can forget to use ._array when passing an array to a NumPy function and it will still work (but maybe not in the same way, if that function also passes the dtype argument for instance).
I believe that should be supported in the standard, but the logic in asarray seems to check __array__ first. The docs don't mention any kind of fallback exception that can be used https://numpy.org/devdocs/user/basics.interoperability.html#the-array-method. Maybe there's some internal function in NumPy for converting lists-of-lists that we can reuse?
If this has to be removed, I'd suggest that the error message at least suggest how such a conversion should be performed (e.g. from_dlpack?). This is causing 5.7k+ test failures in SciPy right now (see scipy/scipy#21828).
Can SciPy not disable the specific warning in its tests? This seems to me to be the best way to deliver info to people without breaking stuff.
Quite a few projects run their CI with "turn warnings into errors", which is very useful to get signal and fix issues quickly (especially DeprecationWarning, which is hidden by default otherwise) - but the downside is that CI does break. So once a library like numpy or array-api-strict plans to do something that may take a while to fix, giving a heads up and waiting until it's fixed before doing a release with the warning is the better way to go about it.
I know that is a little annoying, but if this library is to be run in the test suites of the likes of SciPy, scikit-learn, Xarray, etc. then I don't see a way to avoid being careful about these things.
@lucascolley put up a draft PR at scipy/scipy#21835, so let's look at getting that landed first to ensure removing __array__ is actually doable without too much trouble.
Question: did anyone try implementing the buffer protocol in Python (possible since recently, see PEP 688)? Adding that to array-api-strict's array object makes sense in principle, and it then makes it trivially easy to delete __array__.
Activity
Fix some functions that were missing ._array
asmeurer commentedon Oct 15, 2024
I tested removing it and found some more issues where weren't using
._arraylike in #66. I pushed fixes for this to #68.At the same time, if you remove
__array__, then NumPy converts it to an object array:Or we can explicitly make
__array__raise an exception, which would makenp.asarray(<array_api_strict Array>)fail.asmeurer commentedon Oct 15, 2024
A small hiccup here is that we implicitly use this logic in
asarrayfor conversion from lists of 0-D arrays:array-api-strict/array_api_strict/_creation_functions.py
Line 97 in 019d935
I believe that should be supported in the standard, but the logic in
asarrayseems to check__array__first. The docs don't mention any kind of fallback exception that can be used https://numpy.org/devdocs/user/basics.interoperability.html#the-array-method. Maybe there's some internal function in NumPy for converting lists-of-lists that we can reuse?Remove __array__
asmeurer commentedon Oct 15, 2024
Prototype of this without the fix to asarray at #69.
mdhaber commentedon Nov 8, 2024
If this has to be removed, I'd suggest that the error message at least suggest how such a conversion should be performed (e.g.
from_dlpack?). This is causing 5.7k+ test failures in SciPy right now (see scipy/scipy#21828).rgommers commentedon Nov 8, 2024
+1 to include a hint about how to fix. Writing that hint is hard - which is a problem.
There are more relevant comments on gh-69, so let's keep the discussion there I'd say.
asmeurer commentedon Nov 8, 2024
So do you think this change should be reverted? I can also issue a warning in
__array__instead of removing it outright.rgommers commentedon Nov 8, 2024
A warning doesn't help, the SciPy test suite will turn those straight into errors. And there'll be either zero or a million warnings probably.
asmeurer commentedon Nov 8, 2024
#91
asmeurer commentedon Nov 8, 2024
Can SciPy not disable the specific warning in its tests? This seems to me to be the best way to deliver info to people without breaking stuff.
Re-enable __array__
rgommers commentedon Nov 10, 2024
Quite a few projects run their CI with "turn warnings into errors", which is very useful to get signal and fix issues quickly (especially
DeprecationWarning, which is hidden by default otherwise) - but the downside is that CI does break. So once a library likenumpyorarray-api-strictplans to do something that may take a while to fix, giving a heads up and waiting until it's fixed before doing a release with the warning is the better way to go about it.I know that is a little annoying, but if this library is to be run in the test suites of the likes of SciPy, scikit-learn, Xarray, etc. then I don't see a way to avoid being careful about these things.
rgommers commentedon Nov 10, 2024
@lucascolley put up a draft PR at scipy/scipy#21835, so let's look at getting that landed first to ensure removing
__array__is actually doable without too much trouble.rgommers commentedon Dec 12, 2024
Question: did anyone try implementing the buffer protocol in Python (possible since recently, see PEP 688)? Adding that to
array-api-strict's array object makes sense in principle, and it then makes it trivially easy to delete__array__.lucascolley commentedon Dec 12, 2024
Evgeni mentioned that in the community meeting yesterday, but would we not have to wait until we can drop Python 3.11?
rgommers commentedon Dec 12, 2024
It can be implemented conditionally, only for Python >=3.12. For SciPy I don't see a problem with only testing on >=3.12
ev-br commentedon Dec 12, 2024
#102 (comment)
3.12+ indeed.
EDIT: gh-115 proposes to replace dunder array with dunder buffer.
ev-br commentedon Jun 16, 2025
__array__is gone in gh-115, replaced with__buffer__.