BUG: Fix missing dtype reset in MaskedRecords.view() when dtype is ndarray subclass#30837
BUG: Fix missing dtype reset in MaskedRecords.view() when dtype is ndarray subclass#30837sabasiddique1 wants to merge 4 commits intonumpy:mainfrom
Conversation
|
With |
|
Thanks for checking! This fix addresses a Coverity-flagged logic issue (#30441): the |
|
Usually, with fixes like this, the idea is that the added test fails on main, i.e. it requires the fix to pass/ |
|
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)
|
Description
Fixes a bug in
MaskedRecords.view()where thedtype = Noneassignment was missing whendtypeis an ndarray subclass, causing both branches of an if/else statement to have identical code.Changes
dtype = Noneassignment on line 367 in theif issubclass(dtype, np.ndarray):branch ofMaskedRecords.view()methodtest_view_ndarray_subclass_resets_dtype()to verify the fixnumpy/ma/core.py(line 3272)Related Issue
Fixes #30441