Skip to content

BUG: support numpy's unwrap ufunc - #20132

Merged
neutrinoceros merged 2 commits into
astropy:mainfrom
neutrinoceros:units/compat/numpy-2.6-unwrap-ufunc
Jul 30, 2026
Merged

neutrinoceros merged 2 commits into
astropy:mainfrom
neutrinoceros:units/compat/numpy-2.6-unwrap-ufunc

Conversation

@neutrinoceros

Copy link
Copy Markdown
Contributor

Description

xref numpy/numpy#31848

  • By checking this box, the PR author has requested that maintainers do NOT use the "Squash and Merge" button. Maintainers should respect this when possible; however, the final decision is at the discretion of the maintainer that merges the PR.

@github-actions

Copy link
Copy Markdown
Contributor

Thank you for your contribution to Astropy! 🌌 This checklist is meant to remind the package maintainers who will review this pull request of some common things to look for.

  • Do the proposed changes actually accomplish desired goals?
  • Do the proposed changes follow the Astropy coding guidelines?
  • Are tests added/updated as required? If so, do they follow the Astropy testing guidelines?
  • Are docs added/updated as required? If so, do they follow the Astropy documentation guidelines?
  • Is rebase and/or squash necessary? If so, please provide the author with appropriate instructions. Also see instructions for rebase and squash.
  • Did the CI pass? If no, are the failures related? If you need to run daily and weekly cron jobs as part of the PR, please apply the "Extra CI" label. Codestyle issues can be fixed by the bot.
  • Is a change log needed? If yes, did the change log check pass? If no, add the "no-changelog-entry-needed" label. If this is a manual backport, use the "skip-changelog-checks" label unless special changelog handling is necessary.
  • Is this a big PR that makes a "What's new?" entry worthwhile and if so, is (1) a "what's new" entry included in this PR and (2) the "whatsnew-needed" label applied?
  • At the time of adding the milestone, if the milestone set requires a backport to release branch(es), apply the appropriate "backport-X.Y.x" label(s) before merge.

@neutrinoceros neutrinoceros added backport-v7.2.x on-merge: backport to v7.2.x backport-v8.0.x on-merge: backport to v8.0.x labels Jul 28, 2026
@neutrinoceros
neutrinoceros force-pushed the units/compat/numpy-2.6-unwrap-ufunc branch 2 times, most recently from c17d791 to fd94ee2 Compare July 28, 2026 06:46
@neutrinoceros

Copy link
Copy Markdown
Contributor Author

Nightlies are not caught up yet. Pending.

Comment thread docs/changes/units/20132.bugfix.rst Resolved
@neutrinoceros
neutrinoceros force-pushed the units/compat/numpy-2.6-unwrap-ufunc branch from fd94ee2 to f469498 Compare July 29, 2026 05:11
@neutrinoceros
neutrinoceros marked this pull request as ready for review July 29, 2026 05:11
@neutrinoceros
neutrinoceros force-pushed the units/compat/numpy-2.6-unwrap-ufunc branch from f469498 to 57b1ed3 Compare July 29, 2026 13:09

@nstarman nstarman left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Wait. CI failure is real

@neutrinoceros
neutrinoceros force-pushed the units/compat/numpy-2.6-unwrap-ufunc branch from 57b1ed3 to c894804 Compare July 29, 2026 13:40
@neutrinoceros
neutrinoceros enabled auto-merge July 29, 2026 13:52

@mhvk mhvk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sadly, I think this needs a new helper! I think with this, though, the override of unwrap itself can be removed.

invariant_ufuncs += (np_umath.imag, np_umath.real)

if not NUMPY_LT_2_6:
invariant_ufuncs += (np._core.umath._unwrap,)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't think _unwrap would be invariant if called directly - discont and period need to be converted to the unit of p.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

of course, what was I thinking... what's surprising to me though is that unwrap is already supported as a NEP-18 array function. I'll dig this further down.

@neutrinoceros
neutrinoceros disabled auto-merge July 29, 2026 14:05
@neutrinoceros
neutrinoceros marked this pull request as draft July 29, 2026 14:07
@neutrinoceros

neutrinoceros commented Jul 29, 2026 •

Copy link
Copy Markdown
Contributor Author

@mhvk by "override", do you mean the existing @dispatched_function implementation ?

@mhvk

mhvk commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

@neutrinoceros - yes, I think if we support the new _unwrap ufunc, we should no longer need to dispatch on np.unwrap itself (but of course numpy version specific...). For us, not necessarily really better, but it helps ensure numpy treats subclasses as well as possible...

@pllim

pllim commented Jul 29, 2026

Copy link
Copy Markdown
Member

FWIW I see the ufunc failure in devdeps now, so would be nice if we can wrap this up for merging. Thanks, all!

@neutrinoceros

Copy link
Copy Markdown
Contributor Author

I'll wrap this up tomorrow

@neutrinoceros

Copy link
Copy Markdown
Contributor Author

I found a single precedent for this situation (a new ufunc)
ref 1c6fca3

however, I'm still not confident in my fix:

  • I do not remember exactly how helper functions are supposed to work (nor did I find the time to properly dig into it)
  • my test seems to pass even if the helper isn't registered (bad), so I'm not trusting that my implementation is correct
  • I'm not sure what to do with the array_function override. Removing it leads to serious-looking failures so I'm keeping it for the time being, but I don't think I understand how numpy behaves when both the override and the helper are registered.

@neutrinoceros
neutrinoceros force-pushed the units/compat/numpy-2.6-unwrap-ufunc branch from 28cc116 to dee5e5c Compare July 30, 2026 11:02
@neutrinoceros

Copy link
Copy Markdown
Contributor Author

my test seems to pass even if the helper isn't registered

my test was definitely incorrect. I removed it for now. Of course I'm even less confident in the patch now 😅

@pllim

pllim commented Jul 30, 2026

Copy link
Copy Markdown
Member

The devdeps is green (I guess it ran before #20164 happened), so do we proceed despite the decrease in confidence or are we pursuing an alternative fix? Hope you can advise. Thanks!

@neutrinoceros

Copy link
Copy Markdown
Contributor Author

It's green but the helper isn't covered. I'd like to get feedback from @mhvk before moving forward with this.

@pllim

pllim commented Jul 30, 2026

Copy link
Copy Markdown
Member

@nstarman also would need to re-review because he, too, requested changes.

Thanks, all!

@mhvk

mhvk commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

I'm having a look...

@mhvk
mhvk force-pushed the units/compat/numpy-2.6-unwrap-ufunc branch from dee5e5c to fbe3c45 Compare July 30, 2026 17:46
@mhvk

mhvk commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

OK, I added the code to properly support the ufunc. Unfortunately, actually letting Quantity pass through from np.unwrap is a bit tricky, since the defaults for discont and period are dimensionless by default, and in the helper I have no access to their values, so would need to assume anything dimensionless can also be in radian. It seemed easier to just keep the np.unwrap override in place, which can deal with this a little better.

Anyway, bottom line, we now support a gufunc that nobody is likely to call with Quantity inputs...

@mhvk mhvk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Now a bit too much of an auto-approve, but at least that removes my request changes...

@mhvk
mhvk marked this pull request as ready for review July 30, 2026 17:49
@neutrinoceros

Copy link
Copy Markdown
Contributor Author

Thanks a lot Marten ! I would self-approve too if authors were allowed to.
I also note that @nstarman's request for change was addressed a couple iterations back.

@neutrinoceros
neutrinoceros enabled auto-merge (squash) July 30, 2026 17:58
@neutrinoceros
neutrinoceros merged commit 55da6b4 into astropy:main Jul 30, 2026
36 of 38 checks passed
@neutrinoceros
neutrinoceros deleted the units/compat/numpy-2.6-unwrap-ufunc branch July 30, 2026 17:59
@lumberbot-app

This comment was marked as resolved.

@pllim

pllim commented Jul 30, 2026 •

Copy link
Copy Markdown
Member

Looks like for v7.2.x, we either have to max pin numpy or manually backport this. See #20170

pllim added a commit that referenced this pull request Jul 30, 2026
…132-on-v8.0.x

Backport PR #20132 on branch v8.0.x (BUG: support numpy's unwrap ufunc)
pllim pushed a commit that referenced this pull request Jul 30, 2026
Co-authored-by: Marten H. van Kerkwijk <[email protected]>
(cherry picked from commit 55da6b4)
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.

4 participants