Thanks to visit codestin.com
Credit goes to github.com

Skip to content

FIX: Fix unit example so that we can unpin numpy<2.1 #29616

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

Merged
merged 1 commit into from
Feb 14, 2025

Conversation

timhoffm
Copy link
Member

Closes #28780.

The underlying problem is that operations on numpy scalars try to eagerly convert the other operand to an array. As a result scalar = np .float64(2); scalar * radians would result in a numpy scalar. But we don't want that.

Instead we enforce radians.__rmul__(scalar) by giving the unit a higher __array_priority__. See also numpy/numpy#17650.

I haven't found any specific change notes on this in numpy 2.1. Interestingly, the full story is even more complex. Also for numpy<2.1 radians.__rmul__(scalar) is not called, but there seems another mechanism through __array__ and __array_warp__ catching back in so that the result is again a TaggedValue. But I have not fully investigated why it worked previously. In fact, we want the solution here with going through __rmul__, and that works for all numpy versions.

Closes matplotlib#28780.

The underlying problem is that operations on numpy scalars try to
eagerly convert the other operand to an array. As a result `scalar = np
.float64
(2); scalar * radians` would result in a numpy scalar. But we don't want
 that.
Instead we enforce `radians.__rmul__(scalar)` by giving the unit a
higher
`__array_priority__`. See also https://github
.com/numpy/numpy/issues/17650.

I haven't found any specific change notes on this in numpy 2.1.
Interestingly, the full story is even more complex. Also for numpy<2.1
`radians.__rmul__(scalar)` is not called, but there seems another
mechanism through __array__ and __array_warp__ catching back in so that
the result is again a TaggedValue. But I have not fully investigated why
 it worked previously. In fact, we want the solution here with going
 through __rmul__, and that works for all numpy versions.

`
@github-actions github-actions bot added the Documentation: examples files in galleries/examples label Feb 13, 2025
@timhoffm timhoffm added this to the v3.10.0-doc milestone Feb 13, 2025
@timhoffm timhoffm changed the title FIX: Fix unit example so that we can unpin numpy>2.1 FIX: Fix unit example so that we can unpin numpy<2.1 Feb 13, 2025
Copy link
Member

@story645 story645 left a comment

Choose a reason for hiding this comment

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

but also will follow up on discussion in issue about this example

@story645 story645 merged commit 3fa730d into matplotlib:main Feb 14, 2025
40 checks passed
Copy link

lumberbot-app bot commented Feb 14, 2025

Owee, I'm MrMeeseeks, Look at me.

There seem to be a conflict, please backport manually. Here are approximate instructions:

  1. Checkout backport branch and update it.
git checkout v3.10.x
git pull
  1. Cherry pick the first parent branch of the this PR on top of the older branch:
git cherry-pick -x -m1 3fa730d807f823baa27177a1a499cd7e293537fb
  1. You will likely have some merge/cherry-pick conflict here, fix them and commit:
git commit -am 'Backport PR #29616: FIX: Fix unit example so that we can unpin numpy<2.1'
  1. Push to a named branch:
git push YOURFORK v3.10.x:auto-backport-of-pr-29616-on-v3.10.x
  1. Create a PR against branch v3.10.x, I would have named this PR:

"Backport PR #29616 on branch v3.10.x (FIX: Fix unit example so that we can unpin numpy<2.1)"

And apply the correct labels and milestones.

Congratulations — you did some good work! Hopefully your backport PR will be tested by the continuous integration and merged soon!

Remember to remove the Still Needs Manual Backport label once the PR gets merged.

If these instructions are inaccurate, feel free to suggest an improvement.

Copy link

lumberbot-app bot commented Feb 14, 2025

Owee, I'm MrMeeseeks, Look at me.

There seem to be a conflict, please backport manually. Here are approximate instructions:

  1. Checkout backport branch and update it.
git checkout v3.10.0-doc
git pull
  1. Cherry pick the first parent branch of the this PR on top of the older branch:
git cherry-pick -x -m1 3fa730d807f823baa27177a1a499cd7e293537fb
  1. You will likely have some merge/cherry-pick conflict here, fix them and commit:
git commit -am 'Backport PR #29616: FIX: Fix unit example so that we can unpin numpy<2.1'
  1. Push to a named branch:
git push YOURFORK v3.10.0-doc:auto-backport-of-pr-29616-on-v3.10.0-doc
  1. Create a PR against branch v3.10.0-doc, I would have named this PR:

"Backport PR #29616 on branch v3.10.0-doc (FIX: Fix unit example so that we can unpin numpy<2.1)"

And apply the correct labels and milestones.

Congratulations — you did some good work! Hopefully your backport PR will be tested by the continuous integration and merged soon!

Remember to remove the Still Needs Manual Backport label once the PR gets merged.

If these instructions are inaccurate, feel free to suggest an improvement.

@timhoffm timhoffm deleted the fix-units branch February 14, 2025 05:44
QuLogic pushed a commit to QuLogic/matplotlib that referenced this pull request Feb 14, 2025
@QuLogic
Copy link
Member

QuLogic commented Feb 14, 2025

Don't see any point of backporting to the doc branch if 3.10.1 is imminent.

@QuLogic QuLogic modified the milestones: v3.10.0-doc, v3.10.1 Feb 14, 2025
timhoffm added a commit that referenced this pull request Feb 14, 2025
…3.10.x

Backport PR #29616 on branch v3.10.x (FIX: Fix unit example so that we can unpin numpy<2.1)
@timhoffm
Copy link
Member Author

It's not urgent but somehow these changes should propagate to the doc branch. How does it work? Is doc reset to 3.10.x when that is released or is 3.10.x merged into doc?

@QuLogic
Copy link
Member

QuLogic commented Feb 14, 2025

It's merged together before a release, and v3.10.1-doc starts fresh after v3.10.1

@ksunden ksunden mentioned this pull request Mar 3, 2025
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation: examples files in galleries/examples
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Doc build fails with numpy>=2.1.0
3 participants