Skip to content

Commit 7cc5a2c

Browse files
authored
Merge pull request #19158 from neutrinoceros/utils.masked/19126
BUG/TST: avoid (or accept) NumPy deprecation warnings for mutating shape attributes in utils.masked
2 parents b5754a6 + 33171f9 commit 7cc5a2c

2 files changed

Lines changed: 20 additions & 7 deletions

File tree

‎astropy/utils/masked/function_helpers.py‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from numpy.lib._function_base_impl import _quantile_is_valid, _ureduce
1818

1919
from astropy.units.quantity_helper.function_helpers import FunctionAssigner
20-
from astropy.utils.compat import NUMPY_LT_2_1, NUMPY_LT_2_2, NUMPY_LT_2_4
20+
from astropy.utils.compat import NUMPY_LT_2_1, NUMPY_LT_2_2, NUMPY_LT_2_4, NUMPY_LT_2_5
2121

2222
# This module should not really be imported, but we define __all__
2323
# such that sphinx can typeset the functions with docstrings.
@@ -1427,7 +1427,10 @@ def in1d(ar1, ar2, assume_unique=False, invert=False, *, kind=None):
14271427
def isin(element, test_elements, assume_unique=False, invert=False, *, kind=None):
14281428
element = np.asanyarray(element)
14291429
result = _in1d(element, test_elements, assume_unique, invert, kind=kind)
1430-
result.shape = element.shape
1430+
if NUMPY_LT_2_5:
1431+
result.shape = element.shape
1432+
else:
1433+
result._set_shape(element.shape)
14311434
return result, _copy_of_mask(element), None
14321435

14331436

‎astropy/utils/masked/tests/test_masked.py‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from astropy import units as u
1515
from astropy.coordinates import Longitude
1616
from astropy.units import Quantity
17-
from astropy.utils.compat import NUMPY_LT_2_2, NUMPY_LT_2_3
17+
from astropy.utils.compat import NUMPY_LT_2_1, NUMPY_LT_2_2, NUMPY_LT_2_3
1818
from astropy.utils.compat.optional_deps import HAS_PLT
1919
from astropy.utils.masked import Masked, MaskedNDArray
2020

@@ -304,8 +304,11 @@ def test_viewing(self):
304304

305305
def test_viewing_independent_shape(self):
306306
mms = Masked(self.a, mask=self.m)
307-
mms2 = mms.view()
308-
mms2.shape = mms2.shape[::-1]
307+
if NUMPY_LT_2_1:
308+
mms2 = mms.view()
309+
mms2.shape = mms2.shape[::-1]
310+
else:
311+
mms2 = np.reshape(mms, mms.shape[::-1], copy=False)
309312
assert mms2.shape == mms.shape[::-1]
310313
assert mms2.mask.shape == mms.shape[::-1]
311314
# This should not affect the original array!
@@ -563,14 +566,21 @@ def test_reshape(self):
563566
assert_array_equal(ma_reshape.mask, expected_mask)
564567

565568
def test_shape_setting(self):
566-
ma_reshape = self.ma.copy()
567-
ma_reshape.shape = (6,)
569+
if NUMPY_LT_2_1:
570+
ma_reshape = self.ma.copy()
571+
ma_reshape.shape = (6,)
572+
else:
573+
ma_reshape = np.reshape(self.ma, (6,), copy=True)
574+
568575
expected_data = self.a.reshape((6,))
569576
expected_mask = self.mask_a.reshape((6,))
570577
assert ma_reshape.shape == expected_data.shape
571578
assert_array_equal(ma_reshape.unmasked, expected_data)
572579
assert_array_equal(ma_reshape.mask, expected_mask)
573580

581+
@pytest.mark.filterwarnings(
582+
"default:Setting the shape on a NumPy array has been deprecated in NumPy 2.5:DeprecationWarning"
583+
)
574584
def test_shape_setting_failure(self):
575585
ma = self.ma.copy()
576586
with pytest.raises(ValueError, match="cannot reshape"):

0 commit comments

Comments
 (0)