Repository: astropy/astropy

Issue:
# BUG: incoming deprecation warnings from numpy 2.5 (dev): direct mutations to array shape attributes are deprecated

xref: https://github.com/numpy/numpy/pull/29536

this PR (just merged) breaks about 150 tests throughout astropy. Some deprecations are more easily handled than others, so I'll start with the easy ones to reduce the number of failures so we can streamline the discussion where it's really needed.

upstream reports/PRs:
- https://github.com/brandon-rhodes/python-jplephem/issues/63
- https://github.com/brandon-rhodes/python-jplephem/pull/64

linked PRs:
- #19127 
- #19128 
- #19129 
- #19130 
- #19131 
- #19132 
- #19152
- #19158
- #19182 
- #19184
- #19187

Implementation patch:
diff --git a/astropy/io/fits/util.py b/astropy/io/fits/util.py
index 14b353e78d72..ad74e20ab6bc 100644
--- a/astropy/io/fits/util.py
+++ b/astropy/io/fits/util.py
@@ -20,6 +20,7 @@
 from packaging.version import Version
 
 from astropy.utils import data
+from astropy.utils.compat import NUMPY_LT_2_5
 from astropy.utils.compat.optional_deps import HAS_DASK
 from astropy.utils.exceptions import AstropyUserWarning
 
@@ -870,7 +871,10 @@ def _rstrip_inplace(array):
     # Note: the code will work if this fails; the chunks will just be larger.
     if b.ndim > 2:
         try:
-            b.shape = -1, b.shape[-1]
+            if NUMPY_LT_2_5:
+                b.shape = -1, b.shape[-1]
+            else:
+                b._set_shape((-1, b.shape[-1]))
         except AttributeError:  # can occur for non-contiguous arrays
             pass
     for j in range(0, b.shape[0], bufsize):



Test patch:
diff --git a/astropy/io/fits/tests/test_image.py b/astropy/io/fits/tests/test_image.py
index 0d7b2d868035..2e6c1b31e310 100644
--- a/astropy/io/fits/tests/test_image.py
+++ b/astropy/io/fits/tests/test_image.py
@@ -9,6 +9,7 @@
 from numpy.testing import assert_equal
 
 from astropy.io import fits
+from astropy.utils.compat import NUMPY_LT_2_5
 from astropy.utils.data import get_pkg_data_filename
 from astropy.utils.exceptions import AstropyUserWarning
 
@@ -977,7 +978,12 @@ def test_open_scaled_in_update_mode(self):
 
         # Try reshaping the data, then closing and reopening the file; let's
         # see if all the changes are preserved properly
-        hdul[0].data.shape = (42, 10)
+        if NUMPY_LT_2_5:
+            hdul[0].data.shape = (42, 10)
+        else:
+            # ndarray._set_shape is semi-private, but the only
+            # non deprecated, strict semantic equivalent in Numpy 2.5+
+            hdul[0].data._set_shape((42, 10))
         hdul.close()
 
         hdul = fits.open(testfile)
@@ -988,6 +994,13 @@ def test_open_scaled_in_update_mode(self):
         assert "BSCALE" not in hdul[0].header
         hdul.close()
 
+        with fits.open(testfile, mode="update") as hdul:
+            # Try reshaping the data again, this time using np.reshape
+            hdul[0].data = np.reshape(hdul[0].data, (21, 20))
+
+        with fits.open(testfile) as hdul:
+            assert hdul[0].shape == (21, 20)
+
     def test_scale_back(self):
         """A simple test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/120