X Tutup
Skip to content

BUG: Fix missing dtype reset in MaskedRecords.view() when dtype is ndarray subclass#30837

Open
sabasiddique1 wants to merge 4 commits intonumpy:mainfrom
sabasiddique1:fix-30441
Open

BUG: Fix missing dtype reset in MaskedRecords.view() when dtype is ndarray subclass#30837
sabasiddique1 wants to merge 4 commits intonumpy:mainfrom
sabasiddique1:fix-30441

Conversation

@sabasiddique1
Copy link
Contributor

Description

Fixes a bug in MaskedRecords.view() where the dtype = None assignment was missing when dtype is an ndarray subclass, causing both branches of an if/else statement to have identical code.

Changes

  • Added missing dtype = None assignment on line 367 in the if issubclass(dtype, np.ndarray): branch of MaskedRecords.view() method
  • Added test test_view_ndarray_subclass_resets_dtype() to verify the fix
  • This matches the reference implementation in numpy/ma/core.py (line 3272)

Related Issue

Fixes #30441

@jorenham
Copy link
Member

With numpy==2.4.2 the added test already passes. So what exactly does this fix?

@sabasiddique1
Copy link
Contributor Author

Thanks for checking! This fix addresses a Coverity-flagged logic issue (#30441): the issubclass(dtype, np.ndarray) branch in MaskedRecords.view() was missing dtype = None, making both branches identical and inconsistent with numpy/ma/core.py.
The added test passes on 2.4.2 because the missing assignment doesn’t always change runtime behavior, but it restores the intended control flow for the ndarray-subclass path.
Happy to clarify the PR description or add a version-specific failing test if preferred.

@charris charris added the 09 - Backport-Candidate PRs tagged should be backported label Feb 28, 2026
@jorenham
Copy link
Member

Usually, with fixes like this, the idea is that the added test fails on main, i.e. it requires the fix to pass/

@stratakis
Copy link
Contributor

This is still dead code as the fill_value logic is missing (another omission from copy-pasting).

A full proper diff would be:

  --- a/numpy/ma/mrecords.py
  +++ b/numpy/ma/mrecords.py
  @@ -364,6 +364,7 @@ class MaskedRecords(MaskedArray):
               try:
                   if issubclass(dtype, np.ndarray):
                       output = np.ndarray.view(self, dtype)
  +                    dtype = None
                   else:
                       output = np.ndarray.view(self, dtype)
               # OK, there's the change
  @@ -385,6 +386,10 @@ class MaskedRecords(MaskedArray):
               mdtype = ma.make_mask_descr(output.dtype)
               output._mask = self._mask.view(mdtype, np.ndarray)
               output._mask = output._mask.reshape(output.shape)
  +        # Make sure to reset the _fill_value if needed
  +        if getattr(output, '_fill_value', None) is not None:
  +            if dtype is not None:
  +                output._fill_value = None
           return output

       def harden_mask(self):

  --- a/numpy/ma/tests/test_mrecords.py
  +++ b/numpy/ma/tests/test_mrecords.py
  @@ -384,6 +384,14 @@ class TestView:
           assert_(test['B'][3] is masked)
           assert_equal(test.dtype, np.dtype(alttype))
           assert_(test._fill_value is None)
  +
  +    def test_view_maskedarray_preserves_fill_value(self):
  +        mrec = self._create_data()[0]
  +        original_fv = mrec.fill_value
  +
  +        test = mrec.view(ma.MaskedArray)
  +        assert_(isinstance(test, ma.MaskedArray))
  +        assert_equal(test.fill_value, original_fv)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

00 - Bug 09 - Backport-Candidate PRs tagged should be backported component: numpy.ma masked arrays

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: MaskedRecords.view(): Missing dtype reset when dtype is ndarray subclass

4 participants

X Tutup