BUG: `__matmul__` should return `NotImplemented` instead of raising a `TypeError`
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 55/100
Research direction
Start in _array_object.py at Array.matmul, especially the type check shown in the traceback, and reproduce the issue with the provided scipy LinearOperator example. Done means unsupported operands return NotImplemented so Python can try the reflected operator instead of raising the current TypeError.
Written by the indexing model from the issue text.
Description
__matmul__ doesn't work with fallback to reflected operators per https://docs.python.org/3/library/constants.html#NotImplemented
In [1]: import scipy, numpy as np, array_api_strict as xp
In [3]: class HelloOp(scipy.sparse.linalg.LinearOperator):
...: def _matvec(self, x):
...: return x
...:
...: def __rmatmul__(self, other):
...: print("hello :)")
...:
In [4]: H = HelloOp(dtype=None, shape=(2, 2))
In [5]: np.eye(2) @ H
hello :)
In [6]: xp.eye(2) @ H
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[6], line 1
----> 1 xp.eye(2) @ H
File ~/ghq/github.com/scipy/scipy/.pixi/envs/array-api-cpu/lib/python3.13/site-packages/array_api_strict/_array_object.py:832, in Array.__matmul__(self, other)
828 def __matmul__(self, other: Array, /) -> Array:
829 """
830 Performs the operation __matmul__.
831 """
--> 832 self._check_type_device(other)
833 # matmul is not defined for scalars, but without this, we may get
834 # the wrong error message from asarray.
835 other = self._check_allowed_dtypes(other, "numeric", "__matmul__")
File ~/ghq/github.com/scipy/scipy/.pixi/envs/array-api-cpu/lib/python3.13/site-packages/array_api_strict/_array_object.py:241, in Array._check_type_device(self, other)
239 # Disallow subclasses of Python scalars, such as np.float64 and np.complex128
240 elif type(other) not in (bool, int, float, complex):
--> 241 raise TypeError(f"Expected Array or Python scalar; got {type(other)}")
TypeError: Expected Array or Python scalar; got <class '__main__.HelloOp'>
@ev-br happy to accept a patch for this?
- Dominant language
- Python
- Stars
- 34
- Forks
- 14
- Avg merge
- 3m
- Merged PRs (30d)
- 4
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from data-apis/array-api-strict
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
data-apis/array-api-strict#239 · 3 comments · 1 reaction ·
-
Difficulty 3/5 1-2 days Newbie friendliness 72/100
data-apis/array-api-strict#245 ·
-
enhancement
Difficulty 4/5 3-5 days Newbie friendliness 48/100
data-apis/array-api-strict#168 · 11 comments ·
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
data-apis/array-api-strict#128 · 3 comments ·
-
Medium Priority
Difficulty 5/5 Over a week Newbie friendliness 35/100
data-apis/array-api-strict#70 · 12 comments ·
All issues in data-apis/array-api-strict
Similar issues
-
triage/confirmed
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
agentscope-ai/agentscope#2775 ·
-
comp/desktop P3 type/bug
Difficulty 1/5 Under an hour Newbie friendliness 92/100
NousResearch/hermes-agent#118866 ·
-
bug
Difficulty 1/5 Under an hour Newbie friendliness 90/100
apache/cloudstack#14222 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 82/100