Skip to content

Commit 081b25b

Browse files
authored
Merge pull request astropy#19182 from neutrinoceros/time/19126
BUG: avoid deprecated shape mutation APIs in time
2 parents 143c9c1 + f04b9b5 commit 081b25b

3 files changed

Lines changed: 48 additions & 16 deletions

File tree

‎astropy/time/core.py‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from astropy.extern import _strptime
2828
from astropy.units import UnitConversionError
2929
from astropy.utils import lazyproperty
30+
from astropy.utils.compat import NUMPY_LT_2_5
3031
from astropy.utils.data_info import MixinInfo, data_info_factory
3132
from astropy.utils.decorators import deprecated
3233
from astropy.utils.exceptions import AstropyDeprecationWarning, AstropyWarning
@@ -925,15 +926,22 @@ def shape(self, shape):
925926
(self, "location"),
926927
):
927928
val = getattr(obj, attr, None)
928-
if val is not None and val.size > 1:
929-
try:
929+
if val is None or val.size <= 1:
930+
continue
931+
try:
932+
if NUMPY_LT_2_5:
930933
val.shape = shape
931-
except Exception:
932-
for val2 in reshaped:
933-
val2.shape = oldshape
934-
raise
935934
else:
936-
reshaped.append(val)
935+
val._set_shape(shape)
936+
except Exception:
937+
for val2 in reshaped:
938+
if NUMPY_LT_2_5:
939+
val2.shape = oldshape
940+
else:
941+
val2._set_shape(oldshape)
942+
raise
943+
else:
944+
reshaped.append(val)
937945

938946
def _shaped_like_input(self, value):
939947
if self.masked:

‎astropy/time/tests/test_methods.py‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from astropy.time import Time
1212
from astropy.time.utils import day_frac
1313
from astropy.utils import iers
14+
from astropy.utils.compat import NUMPY_LT_2_5
1415
from astropy.utils.exceptions import AstropyDeprecationWarning
1516

1617

@@ -314,24 +315,34 @@ def test_shape_setting(self, use_mask):
314315

315316
t0_reshape = self.t0.copy()
316317
mjd = t0_reshape.mjd # Creates a cache of the mjd attribute
317-
t0_reshape.shape = (5, 2, 5)
318+
t0_reshape = np.reshape(t0_reshape, (5, 2, 5))
318319
assert t0_reshape.shape == (5, 2, 5)
319320
assert mjd.shape != t0_reshape.mjd.shape # Cache got cleared
320321
assert np.all(t0_reshape.jd1 == self.t0._time.jd1.reshape(5, 2, 5))
321322
assert np.all(t0_reshape.jd2 == self.t0._time.jd2.reshape(5, 2, 5))
322323
assert t0_reshape.location is None
323324
# But if the shape doesn't work, one should get an error.
324325
t0_reshape_t = t0_reshape.T
325-
with pytest.raises(ValueError):
326-
t0_reshape_t.shape = (12,) # Wrong number of elements.
327-
with pytest.raises(AttributeError):
328-
t0_reshape_t.shape = (10, 5) # Cannot be done without copy.
326+
327+
if NUMPY_LT_2_5:
328+
depr_warning_action = "error"
329+
else:
330+
depr_warning_action = "ignore"
331+
with warnings.catch_warnings():
332+
warnings.simplefilter(depr_warning_action, DeprecationWarning)
333+
with pytest.raises(ValueError):
334+
t0_reshape_t.shape = (12,) # Wrong number of elements.
335+
with pytest.raises((AttributeError, ValueError)):
336+
# the exact exception type isn't really in our control,
337+
# as it ultimately comes from numpy but depends on astropy's
338+
# execution flow
339+
t0_reshape_t.shape = (10, 5) # Cannot be done without copy.
329340
# check no shape was changed.
330341
assert t0_reshape_t.shape == t0_reshape.T.shape
331342
assert t0_reshape_t.jd1.shape == t0_reshape.T.shape
332343
assert t0_reshape_t.jd2.shape == t0_reshape.T.shape
333344
t1_reshape = self.t1.copy()
334-
t1_reshape.shape = (2, 5, 5)
345+
t1_reshape = np.reshape(t1_reshape, (2, 5, 5))
335346
assert t1_reshape.shape == (2, 5, 5)
336347
assert np.all(t1_reshape.jd1 == self.t1.jd1.reshape(2, 5, 5))
337348
# location is a single element, so its shape should not change.

‎astropy/utils/masked/core.py‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import numpy as np
2424

25+
from astropy.utils.compat import NUMPY_LT_2_5
2526
from astropy.utils.data_info import ParentDtypeInfo
2627
from astropy.utils.shapes import NDArrayShapeMethods, ShapedLikeNDArray
2728

@@ -743,14 +744,26 @@ def shape(self):
743744

744745
@shape.setter
745746
def shape(self, shape):
747+
return self._set_shape(shape)
748+
749+
def _set_shape(self, shape):
746750
old_shape = self.shape
747-
self._mask.shape = shape
751+
if NUMPY_LT_2_5:
752+
self._mask.shape = shape
753+
else:
754+
self._mask._set_shape(shape)
748755
# Reshape array proper in try/except just in case some broadcasting
749756
# or so causes it to fail.
750757
try:
751-
super(MaskedNDArray, type(self)).shape.__set__(self, shape)
758+
if NUMPY_LT_2_5:
759+
super(MaskedNDArray, type(self)).shape.__set__(self, shape)
760+
else:
761+
super()._set_shape(shape)
752762
except Exception as exc:
753-
self._mask.shape = old_shape
763+
if NUMPY_LT_2_5:
764+
self._mask.shape = old_shape
765+
else:
766+
self._mask._set_shape(old_shape)
754767
# Given that the mask reshaping succeeded, the only logical
755768
# reason for an exception is something like a broadcast error in
756769
# in __array_finalize__, or a different memory ordering between

0 commit comments

Comments
 (0)