Skip to content

Support indexing with any type which implmented __index__#3210

Open
aisk wants to merge 5 commits intoml-explore:mainfrom
aisk:index
Open

Support indexing with any type which implmented __index__#3210
aisk wants to merge 5 commits intoml-explore:mainfrom
aisk:index

Conversation

@aisk
Copy link
Contributor

@aisk aisk commented Mar 5, 2026

Proposed changes

Any type which implemented __index__ can be used to indexing mlx array, so we can use numpy scalar to indexing array.

bool is not supported also it implemented __index__ (actually, bool is a subclass of int) because it's to quirky.

Close: #2710

Checklist

Put an x in the boxes that apply.

  • 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 angeloskath mentioned this pull request Mar 16, 2026
4 tasks
Copy link
Collaborator

@zcbenz zcbenz left a comment

Choose a reason for hiding this comment

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

Can you fix the linting error? The code looks good to me.

@aisk aisk requested a review from zcbenz March 18, 2026 17:05
Copy link
Member

@angeloskath angeloskath left a comment

Choose a reason for hiding this comment

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

Numpy arrays and mlx arrays do not implement __index__ so no need for the extra checks.

}
if (!PyIndex_Check(obj.ptr())) {
return false;
}
Copy link
Member

Choose a reason for hiding this comment

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

I think this should simply be return PyIndex_Check(obj.ptr()) && !nb::isinstance<nb::bool_>(obj);

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.

Support of numpy scalar indexing

3 participants