Skip to content

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

Closed

Description

@lucascolley

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).

Activity

  1. lucascolley commented on Jul 27, 2024

    @lucascolley
    MemberAuthor

    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.

  2. rgommers commented on Jul 27, 2024

    @rgommers
    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.

  3. lucascolley commented on Jul 27, 2024

    @lucascolley
    MemberAuthor

    fix at gh-165

  4. asmeurer commented on Jul 28, 2024

    @asmeurer
    Member

    Seems like NumPy should be fixed here too.

  5. rgommers commented on Jul 28, 2024

    @rgommers
    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.

  6. lucascolley commented on Jul 28, 2024

    @lucascolley
    MemberAuthor

    Do you mean giving NumPy scalars a matching __array_namespace__ attribute?

  7. asmeurer commented on Jul 29, 2024

    @asmeurer
    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__'?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions