Skip to content

Conversation

amacati
Copy link

@amacati amacati commented Aug 21, 2025

Fancy indexing is currently not supported for __setitem__, which blocks some PRs in scipy (scipy/scipy#23425).

As discussed in data-apis/array-api#864 (comment), all frameworks of the array api already implement this feature, but not necessarily in a consistent manner for duplicate indices. It is currently not part of the standard. This PR adds a workaround for array api strict to allow fancy indexing in xpx.at(x, ...).

@amacati amacati changed the title Add __setitem__ fancy indexing support for array-api-strict ENH: Add __setitem__ fancy indexing support for array-api-strict Aug 21, 2025
@lucascolley lucascolley added the enhancement New feature or request label Aug 22, 2025
Copy link
Member

@lucascolley lucascolley left a comment

Choose a reason for hiding this comment

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

thanks @amacati !

@lucascolley lucascolley changed the title ENH: Add __setitem__ fancy indexing support for array-api-strict ENH: Add __setitem__ fancy indexing fallback Aug 22, 2025
@lucascolley lucascolley changed the title ENH: Add __setitem__ fancy indexing fallback ENH: at: add __setitem__ fancy indexing fallback Aug 22, 2025
@amacati
Copy link
Author

amacati commented Aug 23, 2025

The fallback is now implemented as error handling for __setitem__ and the behaviour for data races is documented. I also fixed the handling of negative indices and the assignment of scalar values.

One remaining concern is out-of-bounds indices. For anything negative or positive > len(x), the fallback currently does not throw an error. We would need to add value-based checks to catch that.

@lucascolley
Copy link
Member

For anything negative or positive > len(x), the fallback currently does not throw an error. We would need to add value-based checks to catch that.

Seems fine to leave that for now. I guess the pattern would be to hide things behind not is_lazy_array(x) like SciPy?

Copy link
Member

@lucascolley lucascolley left a comment

Choose a reason for hiding this comment

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

looks good to me, thanks @amacati !

Would you like to take a look @crusaderky ?

@lucascolley
Copy link
Member

the codecov failure is not a worry

@lucascolley lucascolley added this to the 0.9.0 milestone Aug 23, 2025
@lucascolley
Copy link
Member

lint failures are real but trivial

@lucascolley
Copy link
Member

I'll release 0.8.1 soon, and merge this for 0.9.0 by the end of the week if there is no further feedback

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants