Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce allow_newaxis to xps.indices() #3303

Merged
merged 4 commits into from
Apr 22, 2022

Conversation

honno
Copy link
Member

@honno honno commented Apr 21, 2022

The Array API spec recently updated to allow expanding dimensions via None (data-apis/array-api#408), so this PR adds support for dim-expanding indexing in xps.indices() via an allow_newaxis kwarg, similiar to how nps.basic_indices() works.

Note numpy.array_api arrays don't support newaxis yet (numpy/numpy#21373), so test_generate_valid_indices[numpy] will fail when smoking the indexer. Fortunately the mock does support it, so HYPOTHESIS_TEST_ARRAY_API=all pytest tests/array_api/test_indices.py comes in quite handy here.

@honno
Copy link
Member Author

honno commented Apr 21, 2022

Note numpy.array_api arrays don't support newaxis yet (numpy/numpy#21373)

I didn't realise CI was already testing numpy.array_api, neat! I've added a runtime xfail if xp == numpy.array_api... that's probably preferable to xp.__name__ == "numpy.array_api" I think? I put it in a try: ... except ImportError: pass block to work if you have older NumPy versions installed (or don't have any installed), which still seems like a good idea.

Assuming this gets patched, the next NumPy release following that patch we can check for numpy.__version__ first. Note most array_api patches seem to get backported. I guess we want to pin NumPy to the patched version rather than faffing around with this.

Copy link
Member

@Zac-HD Zac-HD left a comment

Choose a reason for hiding this comment

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

Another fantastic PR @honno - thanks again!

One comment below about how we can ensure that our test is weakened as little as possible while np.array_api doesn't yet support newaxis (including fail-when-added, so that we undo the patch as soon as possible); otherwise this looks merge-ready to me. Congrats!

hypothesis-python/tests/array_api/test_indices.py Outdated Show resolved Hide resolved
@honno honno requested a review from Zac-HD April 22, 2022 10:27
@Zac-HD Zac-HD enabled auto-merge April 22, 2022 16:21
@Zac-HD Zac-HD merged commit 9799fef into HypothesisWorks:master Apr 22, 2022
@honno
Copy link
Member Author

honno commented Apr 23, 2022

Awesome @Zac-HD, thanks!

@honno honno deleted the xps-indices-newaxis branch February 28, 2024 11:10
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