Skip to content

TST: fix deprecation warnings from numpy dev's __array_wrap__ signature#15925

Merged
mhvk merged 2 commits intoastropy:mainfrom
neutrinoceros:tst/numpy2/array_wrap_return_scalar
Jan 23, 2024
Merged

TST: fix deprecation warnings from numpy dev's __array_wrap__ signature#15925
mhvk merged 2 commits intoastropy:mainfrom
neutrinoceros:tst/numpy2/array_wrap_return_scalar

Conversation

@neutrinoceros
Copy link
Copy Markdown
Contributor

Description

Fixes #15924
The discussion upstream is pretty rich so I haven't read it all, but the docstring says

Subclasses may ignore the value, or return array[()] to behave more like NumPy.

I'm just going with the first option for now, but I don't think I understand the problem solved by this new argument deeply enough to make a call between the two proposed approaches. Since @mhvk served as a reviewer to numpy/numpy#25409, I assume he's in a better position to make the call.

  • 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?
  • Is a milestone set? Milestone must be set but we cannot check for it on Actions; do not let the green checkmark fool you.
  • 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.

@github-actions
Copy link
Copy Markdown
Contributor

👋 Thank you for your draft pull request! Do you know that you can use [ci skip] or [skip ci] in your commit messages to skip running continuous integration tests until you are ready?

@taldcroft
Copy link
Copy Markdown
Member

I requested review from @mhvk since he is really the numpy expert here.

@neutrinoceros
Copy link
Copy Markdown
Contributor Author

Remaining failures are unrelated, and reported as #15926

Copy link
Copy Markdown
Contributor

@mhvk mhvk left a comment

Choose a reason for hiding this comment

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

Arrggh, I see I didn't actually submit my comments. Now doing so. You'll see one requested change for table, and a question about what to do with convolution, which may need @larrybradley's input.

p.s. Since it keeps the status quot it, I'd be happy to just go with your change for convolution for now if @larrybradley does not have time (or we need to think about it more).

self.info = obj.info

def __array_wrap__(self, out_arr, context=None):
def __array_wrap__(self, out_arr, context=None, return_scalar=False):
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 think you want to pass on return_scalar in the super() call below.

Also the semantics of the change is that one should do

return self.data[()] if return_scalar else self.data

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.

fixed ! thank you !

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.

Ah, good point about the backward compatibility.

Could you also change the if statement on (new) line 749 to look like

        if ((NUMPY_LT_2_0 or return_scalar)
            and (self.shape != out_arr.shape
                 or (isinstance(out_arr, BaseColumn)
                     and (context is not None and context[0] in _comparison_functions)
                    )
                )
            ):

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.

done !

self.info = obj.info

def __array_wrap__(self, obj, context=None):
def __array_wrap__(self, obj, context=None, return_scalar=False):
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.

This one is just right - for Quantity, we never return scalars.

return self._array

def __array_wrap__(self, array, context=None):
def __array_wrap__(self, array, context=None, return_scalar=False):
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.

This one is a little weird, as I'm not sure which numpy functions are allowed if ufuncs are not - if one simply does not want to look like an array, then I think __array__ = None would do it. Or if it is just ufunc, then __array_ufunc__ = None.

However, I notice that the log gives no warnings about this __array_wrap__, so perhaps it is never used? I guess the coverage would tell... If so, maybe best just to remove it? cc @larrybradley

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.

Looks like this was added by @adonath many years ago to fix something:

Copy link
Copy Markdown
Member

@larrybradley larrybradley Jan 23, 2024

Choose a reason for hiding this comment

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

It looks like __array_wrap__ was added to allow a Kernel object (stored internally as an array) to be multiplied by a scalar. Is there a better way to do that?

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.

Ah, yes, that makes sense. Two solutions: (1) set __array_priority__ = 1000, so that you get precedence, (2) set __array_ufunc__ = None to indicate numpy should not try to do any arithmetic. The latter is the more "modern" one and probably best here - one can always revisit if it is decided to allow more than the basic operations. But obviously check whether the tests still pass!

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.

let's try __array_ufunc__ = None

@neutrinoceros neutrinoceros force-pushed the tst/numpy2/array_wrap_return_scalar branch from 681436a to 013f551 Compare January 23, 2024 17:21
@neutrinoceros neutrinoceros force-pushed the tst/numpy2/array_wrap_return_scalar branch from 595cc4d to b84aa27 Compare January 23, 2024 19:38
@neutrinoceros neutrinoceros force-pushed the tst/numpy2/array_wrap_return_scalar branch from b84aa27 to 5025468 Compare January 23, 2024 19:39
@neutrinoceros
Copy link
Copy Markdown
Contributor Author

I think this one is go ?

Copy link
Copy Markdown
Member

@pllim pllim left a comment

Choose a reason for hiding this comment

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

Diff seems reasonable but I'll let @mhvk approve/merge. Might wanna use Squash & Merge.

@pllim
Copy link
Copy Markdown
Member

pllim commented Jan 23, 2024

To add to the fun, now https://datacenter.iers.org/data/9/finals2000A.all is down... Hopefully temporary. But unrelated to numpy-dev.

Copy link
Copy Markdown
Contributor

@mhvk mhvk left a comment

Choose a reason for hiding this comment

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

Super, thanks!

@mhvk mhvk merged commit 316f8b9 into astropy:main Jan 23, 2024
@lumberbot-app

This comment was marked as resolved.

@pllim
Copy link
Copy Markdown
Member

pllim commented Jan 23, 2024

Anyone able to manually backport? Thanks! 🙏

@neutrinoceros neutrinoceros deleted the tst/numpy2/array_wrap_return_scalar branch January 24, 2024 06:42
@neutrinoceros
Copy link
Copy Markdown
Contributor Author

On it.

neutrinoceros pushed a commit to neutrinoceros/astropy that referenced this pull request Jan 24, 2024
…wrap_return_scalar

TST: fix deprecation warnings from numpy dev's __array_wrap__ signature
(cherry picked from commit 316f8b9)
pllim added a commit that referenced this pull request Jan 24, 2024
Backport PR #15925 on branch v6.0.x (TST: fix deprecation warnings from numpy dev's __array_wrap__ signature)
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.

TST: (numpy dev) DeprecationWarning: __array_wrap__ must accept context and return_scalar arguments

5 participants