Skip to content

support numpy scalar indexing#3258

Closed
mm65x wants to merge 1 commit intoml-explore:mainfrom
mm65x:fix-numpy-scalar-indexing
Closed

support numpy scalar indexing#3258
mm65x wants to merge 1 commit intoml-explore:mainfrom
mm65x:fix-numpy-scalar-indexing

Conversation

@mm65x
Copy link
Contributor

@mm65x mm65x commented Mar 15, 2026

Proposed changes

#2710

indexing with numpy integer scalars (np.int64(1), np.int32(2), etc.) throws
"Cannot index mlx array using the given type" because the indexing code only
checks for Python's built-in int via nb::isinstance<nb::int_>. numpy
integer scalars aren't int subclasses but they do implement __index__
(PEP 357).

the fix replaces all nb::isinstance<nb::int_> checks in the indexing path
with a helper that uses PyIndex_Check, which matches any object implementing
__index__. this covers direct indexing (x[np.int64(1)]), slicing
(x[np.int64(1):]), tuple indexing (x[np.int64(0), np.int64(1)]), and
assignment through all the same paths.

Checklist

  • I have read the CONTRIBUTING document
  • I have run pre-commit run --all-files to format my code / installed pre-commit prior to committing changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the necessary documentation (if needed)

@angeloskath
Copy link
Member

There is already a PR implementing the exact same fix #3210, feel free to comment there if there is something that isn't supported or can be improved.

It has also been several PRs that already had PRs submitted with the exact same fix (a lot of which I merged because they were small). Please do a search and contribute to the existing PRs before submitting a new one.

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.

2 participants