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
{{ message }}
Repository navigation
BUG: array_namespace returns compat for NumPy scalars with use_compat=None #164
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).
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.
Activity
lucascolley commentedon 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 commentedon Jul 27, 2024
Agreed, that should be the right default.
lucascolley commentedon Jul 27, 2024
fix at gh-165
asmeurer commentedon Jul 28, 2024
Seems like NumPy should be fixed here too.
rgommers commentedon Jul 28, 2024
Can you spell out what should change in NumPy? It doesn't have an
array_namespacefunction, so I don't immediately see it.lucascolley commentedon Jul 28, 2024
Do you mean giving NumPy scalars a matching
__array_namespace__attribute?asmeurer commentedon Jul 29, 2024
Yes. This should work
Re-enable wrapping unconditionally for NumPy