Skip to content

Add explicit index and bytes support - #4388

Open
aaishwarymishra wants to merge 1 commit into
ml-explore:mainfrom
aaishwarymishra:index
Open

Add explicit index and bytes support#4388
aaishwarymishra wants to merge 1 commit into
ml-explore:mainfrom
aaishwarymishra:index

Conversation

@aaishwarymishra

Copy link
Copy Markdown
Contributor
  • ☑️ I understand it is strictly prohibited to use AI to write PR description
    Hi, this PR adds __index__ support to MLX arrays, allowing integer-valued scalar arrays to be used directly as Python indices. I found MLX did not had __index__ in data-apis/array-api-compat#451, adding __index__ caused regressions in boolean masking and bytes() conversion. Fixed boolean masking indexing behavior and added an explicit __bytes__ implementation.

  • AI usage disclosure:
    AI was used for bug Identification and diagnosis caused by introducing __index__.

@ev-br

ev-br commented Aug 25, 2026

Copy link
Copy Markdown

From the Array API perspective, having the __index__ dunder to match the existing __int__ dunder would be a great QoL improvement, as it enables the idiomatic operator.index usage, per PEP 357:

In [1]: import mlx.core as mx

In [2]: int(mx.asarray(2))
Out[2]: 2

In [5]: import operator

In [6]: operator.index(mx.asarray(2))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[6], line 1
----> 1 operator.index(mx.asarray(2))

TypeError: 'mlx.core.array' object cannot be interpreted as an integer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants