Skip to content

BUG: np.matrix is not an array API object#423

Open
ev-br wants to merge 1 commit intodata-apis:mainfrom
ev-br:reject_np_matrix
Open

BUG: np.matrix is not an array API object#423
ev-br wants to merge 1 commit intodata-apis:mainfrom
ev-br:reject_np_matrix

Conversation

@ev-br
Copy link
Copy Markdown
Member

@ev-br ev-br commented Apr 23, 2026

Being an ndarray subclass, np.matrix inherits the __array_namespace__ method, even though it is not Array API compatible, and is not meant to be.

(Pdb) x
matrix([[3]])
(Pdb) p x.__array_namespace__() is np
True

closes gh-388

@ev-br ev-br added this to the 1.15 milestone Apr 23, 2026
Copilot AI review requested due to automatic review settings April 23, 2026 08:53
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes is_array_api_obj() incorrectly treating numpy.matrix as Array API compatible by explicitly rejecting it, and adds a regression test to ensure np.matrix is not recognized as an Array API object.

Changes:

  • Add a numpy.matrix exclusion in is_array_api_obj().
  • Add a NumPy-specific regression test covering np.matrix.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
array_api_compat/common/_helpers.py Explicitly returns False for numpy.matrix instances in is_array_api_obj().
tests/test_numpy.py Adds a regression test asserting np.matrix is not an Array API object.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread array_api_compat/common/_helpers.py Outdated
Comment thread tests/test_numpy.py
Being an ndarray subclass, np.matrix inherits the `__array_namespace__`
method, even though it is not Array API compatible, and is not meant
to be.

(Pdb) x
matrix([[3]])
(Pdb) p x.__array_namespace__() is np
True
```
@ev-br ev-br force-pushed the reject_np_matrix branch from 0d27b73 to bd39005 Compare April 23, 2026 09:07
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 this pull request may close these issues.

BUG: is_array_api_obj(np.matrix(...)) == True

2 participants