Skip to content

Fix MaskedNDArray mask preservation during pickling#19717

Open
charliesheh wants to merge 1 commit into
astropy:mainfrom
charliesheh:fix-maskedndarray-pickle-mask
Open

Fix MaskedNDArray mask preservation during pickling#19717
charliesheh wants to merge 1 commit into
astropy:mainfrom
charliesheh:fix-maskedndarray-pickle-mask

Conversation

@charliesheh
Copy link
Copy Markdown

Fixes an issue where MaskedNDArray objects lost their mask after being pickled and unpickled.

This PR:

adds a regression test for MaskedNDArray pickle round-tripping
preserves _mask state during pickling via reduce
restores the mask during unpickling via setstate

Closes #19154

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 8, 2026

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.

@pllim pllim added this to the v8.1.0 milestone May 8, 2026
@pllim pllim added the Bug label May 8, 2026
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.

Thanks for the PR! But I worry about problems with previously pickled instances, which were brought up by @evanshinechen in #19154 (comment) (see also my in-line comment).

My sense would be that perhaps we should follow Quantity.__reduce__ and just add __dict__ to the state. However, we then also have to make sure Quantity doesn't do the same. It is a bit tricky!

Putting "request changes" mostly since we need to be sure we do this right...

assert type(m) is type(m2)
assert type(m.info) is type(m2.info)

def test_masked_ndarray_pickle_preserves_mask():
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.

Would it be possible to try to combine this test with the preceding one, by using a data entry of [1, 2, 3] in the parametrization? The only issue that I can see is that the other instances all have units, so you'd have to special-case, like

if hasattr(m, "unit"):
    assert m.unit == m2.unit

Longitude([1, 2, 3], u.deg),
],
)

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.

Don't add an empty line here.

return (pickled_state[0], pickled_state[1], new_state)

def __setstate__(self, state):
super().__setstate__(state[:-1])
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.

Here we need to ensure that if something was pickled before this PR, it would be read in correctly. See discussion at #19154 (comment)

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.

MaskedNDArray does not preserve mask when pickled

3 participants