Skip to content

BUG: array_namespace returns compat for NumPy scalars with use_compat=None #164

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

Closed
lucascolley opened this issue Jul 27, 2024 · 7 comments · Fixed by #165
Closed

BUG: array_namespace returns compat for NumPy scalars with use_compat=None #164

lucascolley opened this issue Jul 27, 2024 · 7 comments · Fixed by #165

Comments

@lucascolley
Copy link
Member

lucascolley commented Jul 27, 2024

With array-api-compat 1.8, NumPy 2.0.1

In [22]: array_namespace(np.asarray(0.0), use_compat=None)
Out[22]: <module 'numpy' from '/Users/lucascolley/mambaforge/envs/scipy-dev/lib/python3.10/site-packages/numpy/__init__.py'>

In [23]: array_namespace(np.float64(0.0), use_compat=None)
Out[23]: <module 'scipy._lib.array_api_compat.numpy' from '/Users/lucascolley/programming/scipy/build-install/lib/python3.10/site-packages/scipy/_lib/array_api_compat/numpy/__init__.py'>

This behaviour is quite problematic for SciPy given NumPy's tendency to throw NumPy scalars at you under very little pressure.

In [25]: array_namespace(np.asarray(0.0) - 32, use_compat=None)
Out[25]: <module 'scipy._lib.array_api_compat.numpy' from '/Users/lucascolley/programming/scipy/build-install/lib/python3.10/site-packages/scipy/_lib/array_api_compat/numpy/__init__.py'>

It seems practical to have NumPy scalars return the same namespace as NumPy arrays when use_compat=None. Or perhaps a different argument should have to be passed to use_compat (that would be fine for us).

@lucascolley
Copy link
Member Author

lucascolley commented Jul 27, 2024

discovered in scipy/scipy#21264 (comment). The real problem it causes is that we check for matching namespaces in our tests, and often compare NumPy scalars against NumPy arrays. I suppose we could add extra checks for NumPy scalars to that machinery, but I think this should be fixed here instead.

@rgommers
Copy link
Member

It seems practical to have NumPy scalars return the same namespace as NumPy arrays when use_compat=None.

Agreed, that should be the right default.

@lucascolley
Copy link
Member Author

fix at gh-165

@asmeurer
Copy link
Member

Seems like NumPy should be fixed here too.

@rgommers
Copy link
Member

Can you spell out what should change in NumPy? It doesn't have an array_namespace function, so I don't immediately see it.

@lucascolley
Copy link
Member Author

Do you mean giving NumPy scalars a matching __array_namespace__ attribute?

@asmeurer
Copy link
Member

Yes. This should work

>>> np.asarray(0).__array_namespace__()
<module 'numpy' from '/Users/aaronmeurer/miniconda3/envs/numpy2/lib/python3.12/site-packages/numpy/__init__.py'>
>>> np.asarray([0])[0].__array_namespace__()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'numpy.int64' object has no attribute '__array_namespace__'. Did you mean: '__array_interface__'?

asmeurer added a commit to asmeurer/array-api-compat that referenced this issue Jul 30, 2024
NumPy 2.0 and 2.1 both have some incompatibilities with the array API (see data-apis#167 and data-apis#164).

Closes data-apis#168.
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

Successfully merging a pull request may close this issue.

3 participants