Skip to content

RFC: make all MaskedNDArray.__wrap_array__'s helper functions return a (result, mask, out) tuple (fix compat with numpy dev)#15948

Merged
mhvk merged 1 commit intoastropy:mainfrom
neutrinoceros:rfc/numpy2/maskedndarray_wrap_array_early_returner
Jan 25, 2024
Merged

RFC: make all MaskedNDArray.__wrap_array__'s helper functions return a (result, mask, out) tuple (fix compat with numpy dev)#15948
mhvk merged 1 commit intoastropy:mainfrom
neutrinoceros:rfc/numpy2/maskedndarray_wrap_array_early_returner

Conversation

@neutrinoceros
Copy link
Copy Markdown
Contributor

Description

Fixes #15947
This is a proof-of-concept level refactor, though maybe there's appetite to get this in as is to make devdeps CI green first.

  • 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?

@neutrinoceros neutrinoceros force-pushed the rfc/numpy2/maskedndarray_wrap_array_early_returner branch from f8454a2 to 6cc458b Compare January 25, 2024 10:26
@neutrinoceros neutrinoceros marked this pull request as ready for review January 25, 2024 13:44
@pllim pllim requested review from bsipocz and mhvk January 25, 2024 14:51
@pllim pllim added this to the v6.0.1 milestone Jan 25, 2024
@pllim pllim added Affects-dev PRs and issues that do not impact an existing Astropy release no-changelog-entry-needed numpy-dev labels Jan 25, 2024
@pllim
Copy link
Copy Markdown
Member

pllim commented Jan 25, 2024

Would be nice if we can turn that failing astroquery case into a test here but without all that server calls, if possible.

If not, then maybe try this branch out over at astroquery as a draft PR to be sure: https://github.com/astropy/astroquery/blob/110cb833459b97a6c233bba871447bbeb814b25d/tox.ini#L31

Thanks!

@pllim pllim requested a review from nstarman January 25, 2024 14:58
@mhvk
Copy link
Copy Markdown
Contributor

mhvk commented Jan 25, 2024

This looks promising! I'll have a better look later...

@neutrinoceros
Copy link
Copy Markdown
Contributor Author

neutrinoceros commented Jan 25, 2024

Would be nice if we can turn that failing astroquery case into a test here but without all that server calls, if possible.

I'm not convinced it's worth it: we're already catching the issue seen in astroquery with our current test suite.

If not, then maybe try this branch out over at astroquery as a draft PR to be sure

I'll do that now !

update: here it is: astropy/astroquery#2931

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.

I think this is the right approach, but since it affects Masked and not quantity, I'd prefer to just fix it there. Looking at the code, easiest would seem to be to insist dispatched_function always returns a tuple of 3, so if the result is already OK, one should just use result, None, None.

The one exception perhaps would be that returning a plain None is OK (for the various put functions).

What do you think? If OK, that should be a quick update, but don't forget to update the docstring for DISPATCHED_FUNCTIONS as well!

return self._not_implemented_or_raise(function, types)

if not isinstance(dispatched_result, tuple):
if dispatched_function._direct_results:
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.

With my main suggestion, this branch is not necessary any more, but I think I still like the ability to just return None, so this would be

if dispatched_result is None:
    return

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.

p.s. Note that with result, None, None, the fall-through does the right thing, of just returning result. I don't quite understand why it wasn't a tuple of 3 to start with, but likely I just started with returning the plain result, and then realized there were many cases where I had to attach the mask or deal with out, so might as well have the option to return a tuple of three.

@neutrinoceros
Copy link
Copy Markdown
Contributor Author

@mhvk yes that should work too, I'll push that in a minute !

@neutrinoceros
Copy link
Copy Markdown
Contributor Author

switching to draft for now, will rebase to edit the branch history and commit description when CI completes.

@neutrinoceros neutrinoceros marked this pull request as draft January 25, 2024 17:01
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.

A few little leftovers, but looks good!

np.concatenate(masks, out=out.mask, axis=axis)
np.concatenate(data, out=out.unmasked, axis=axis, dtype=dtype, casting=casting)
return out
return (out, None, None)
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.

Might as well remove the parentheses if you rebase...

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

for (result, mask) in zip(results, masks)
)
return results if len(results) > 1 else results[0]
return results if len(results) > 1 else results[0], None, None
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 does need parentheses,

    return (results if len(results) > 1 else results[0]), None, None


result = np.interp(xd, xp, fp, *args, **kwargs)
return result if xm is None else Masked(result, xm.copy())
return result if xm is None else Masked(result, xm.copy()), None, None
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.

Also here

return (result if xm is None else Masked(result, xm.copy())), None, None

@neutrinoceros neutrinoceros force-pushed the rfc/numpy2/maskedndarray_wrap_array_early_returner branch 2 times, most recently from cb8e920 to 80c7e11 Compare January 25, 2024 17:16
@neutrinoceros neutrinoceros changed the title RFC: explicitly mark early returner function helpers for MaskedNDArray.__wrap_array__ (fix compat with numpy dev) RFC: make all MaskedNDArray.__wrap_array__'s helper functions return a (result, mask, out) tuple (fix compat with numpy dev) Jan 25, 2024
…a (result, mask, out) tuple (fix compat with numpy dev)
@neutrinoceros neutrinoceros force-pushed the rfc/numpy2/maskedndarray_wrap_array_early_returner branch from 80c7e11 to 13411bd Compare January 25, 2024 17:19
@neutrinoceros
Copy link
Copy Markdown
Contributor Author

Should be good now 🤞🏻

@neutrinoceros neutrinoceros marked this pull request as ready for review January 25, 2024 17:19
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, looks good! Nice collaborative effort here!

@mhvk mhvk enabled auto-merge January 25, 2024 18:57
@mhvk mhvk merged commit f85f796 into astropy:main Jan 25, 2024
meeseeksmachine pushed a commit to meeseeksmachine/astropy that referenced this pull request Jan 25, 2024
…'s helper functions return a (result, mask, out) tuple (fix compat with numpy dev)
pllim added a commit that referenced this pull request Jan 25, 2024
…948-on-v6.0.x

Backport PR #15948 on branch v6.0.x (RFC: make all MaskedNDArray.__wrap_array__'s helper functions return a (result, mask, out) tuple (fix compat with numpy dev))
@neutrinoceros neutrinoceros deleted the rfc/numpy2/maskedndarray_wrap_array_early_returner branch January 25, 2024 22:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Affects-dev PRs and issues that do not impact an existing Astropy release no-changelog-entry-needed numpy-dev units utils.masked

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TST: MaskedNDArray.__array_wrap__ is incompatible with numpy dev

3 participants