diff --git a/astropy/coordinates/angle_formats.py b/astropy/coordinates/angle_formats.py index 849d10bd3483..73be99b92090 100644 --- a/astropy/coordinates/angle_formats.py +++ b/astropy/coordinates/angle_formats.py @@ -22,8 +22,7 @@ import numpy as np from astropy import units as u -from astropy.utils import format_exception, parsing -from astropy.utils.decorators import deprecated +from astropy.utils import parsing from .errors import ( IllegalHourError, @@ -413,113 +412,6 @@ def degrees_to_dms(d): return np.floor(sign * d), sign * np.floor(m), sign * s -@deprecated( - since="5.1", - message=( - "dms_to_degrees (or creating an Angle with a tuple) has ambiguous " - "behavior when the degree value is 0. Use {alternative}." - ), - alternative=( - "another way of creating angles instead (e.g. a less " - "ambiguous string like '-0d1m2.3s')" - ), -) -def dms_to_degrees(d, m, s=None): - """ - Convert degrees, arcminute, arcsecond to a float degrees value. - """ - _check_minute_range(m) - _check_second_range(s) - - # determine sign - sign = np.copysign(1.0, d) - - try: - d = np.floor(np.abs(d)) - if s is None: - m = np.abs(m) - s = 0 - else: - m = np.floor(np.abs(m)) - s = np.abs(s) - except ValueError as err: - raise ValueError( - format_exception( - "{func}: dms values ({1[0]},{2[1]},{3[2]}) could not be " - "converted to numbers.", - d, - m, - s, - ) - ) from err - - return sign * (d + m / 60.0 + s / 3600.0) - - -@deprecated( - since="5.1", - message=( - "hms_to_hours (or creating an Angle with a tuple) has ambiguous " - "behavior when the hour value is 0. Use {alternative}." - ), - alternative=( - "another way of creating angles instead (e.g. a less " - "ambiguous string like '-0h1m2.3s')" - ), -) -def hms_to_hours(h, m, s=None): - """ - Convert hour, minute, second to a float hour value. - """ - check_hms_ranges(h, m, s) - - # determine sign - sign = np.copysign(1.0, h) - - try: - h = np.floor(np.abs(h)) - if s is None: - m = np.abs(m) - s = 0 - else: - m = np.floor(np.abs(m)) - s = np.abs(s) - except ValueError as err: - raise ValueError( - format_exception( - "{func}: HMS values ({1[0]},{2[1]},{3[2]}) could not be " - "converted to numbers.", - h, - m, - s, - ) - ) from err - - return sign * (h + m / 60.0 + s / 3600.0) - - -def hms_to_degrees(h, m, s): - """ - Convert hour, minute, second to a float degrees value. - """ - return hms_to_hours(h, m, s) * 15.0 - - -def hms_to_radians(h, m, s): - """ - Convert hour, minute, second to a float radians value. - """ - return u.degree.to(u.radian, hms_to_degrees(h, m, s)) - - -def hms_to_dms(h, m, s): - """ - Convert degrees, arcminutes, arcseconds to an ``(hour, minute, second)`` - tuple. - """ - return degrees_to_dms(hms_to_degrees(h, m, s)) - - def hours_to_decimal(h): """ Convert any parseable hour value into a float value. diff --git a/astropy/coordinates/angles.py b/astropy/coordinates/angles.py index 9551bfadeb4b..40dfa28e16da 100644 --- a/astropy/coordinates/angles.py +++ b/astropy/coordinates/angles.py @@ -112,7 +112,10 @@ def __new__(cls, angle, unit=None, dtype=np.inexact, copy=True, **kwargs): unit = cls._convert_unit_to_angle_unit(u.Unit(unit)) if isinstance(angle, tuple): - angle = cls._tuple_to_float(angle, unit) + raise NotImplementedError( + "Creating an Angle with a tuple has ambiguous behavior " + "and is no longer supported." + ) elif isinstance(angle, str): angle, angle_unit = form.parse_angle(angle, unit) @@ -141,20 +144,6 @@ def __new__(cls, angle, unit=None, dtype=np.inexact, copy=True, **kwargs): return super().__new__(cls, angle, unit, dtype=dtype, copy=copy, **kwargs) - @staticmethod - def _tuple_to_float(angle, unit): - """ - Converts an angle represented as a 3-tuple or 2-tuple into a floating - point number in the given unit. - """ - # TODO: Numpy array of tuples? - if unit == u.hourangle: - return form.hms_to_hours(*angle) - elif unit == u.degree: - return form.dms_to_degrees(*angle) - else: - raise u.UnitsError(f"Can not parse '{angle}' as unit '{unit}'") - @staticmethod def _convert_unit_to_angle_unit(unit): return u.hourangle if unit == u.hour else unit diff --git a/astropy/coordinates/tests/test_angles.py b/astropy/coordinates/tests/test_angles.py index a0244fd76aa5..a507192d3485 100644 --- a/astropy/coordinates/tests/test_angles.py +++ b/astropy/coordinates/tests/test_angles.py @@ -17,7 +17,6 @@ IllegalSecondError, IllegalSecondWarning, ) -from astropy.utils.exceptions import AstropyDeprecationWarning def test_create_angles(): @@ -46,11 +45,10 @@ def test_create_angles(): a10 = Angle(3.60827466667, unit=u.hour) a11 = Angle("3:36:29.7888000120", unit=u.hour) - with pytest.warns(AstropyDeprecationWarning, match="hms_to_hour"): - a12 = Angle((3, 36, 29.7888000120), unit=u.hour) # *must* be a tuple - with pytest.warns(AstropyDeprecationWarning, match="hms_to_hour"): - # Regression test for #5001 - a13 = Angle((3, 36, 29.7888000120), unit="hour") + with pytest.raises(NotImplementedError, match="no longer supported"): + Angle((3, 36, 29.7888000120), unit=u.hour) + with pytest.raises(NotImplementedError, match="no longer supported"): + Angle((3, 36, 29.7888000120), unit="hour") Angle(0.944644098745, unit=u.radian) @@ -93,34 +91,32 @@ def test_create_angles(): assert_allclose(a5.radian, a6.radian) assert_allclose(a10.degree, a11.degree) - assert a11 == a12 == a13 == a14 + assert a11 == a14 assert a21 == a22 assert a23 == -a24 assert a24 == a25 # check for illegal ranges / values with pytest.raises(IllegalSecondError): - a = Angle("12 32 99", unit=u.degree) + Angle("12 32 99", unit=u.degree) with pytest.raises(IllegalMinuteError): - a = Angle("12 99 23", unit=u.degree) + Angle("12 99 23", unit=u.degree) with pytest.raises(IllegalSecondError): - a = Angle("12 32 99", unit=u.hour) + Angle("12 32 99", unit=u.hour) with pytest.raises(IllegalMinuteError): - a = Angle("12 99 23", unit=u.hour) + Angle("12 99 23", unit=u.hour) with pytest.raises(IllegalHourError): - a = Angle("99 25 51.0", unit=u.hour) + Angle("99 25 51.0", unit=u.hour) with pytest.raises(ValueError): - a = Angle("12 25 51.0xxx", unit=u.hour) + Angle("12 25 51.0xxx", unit=u.hour) with pytest.raises(ValueError): - a = Angle("12h34321m32.2s") - - assert a1 is not None + Angle("12h34321m32.2s") def test_angle_from_view(): @@ -425,9 +421,9 @@ def test_radec(): """ with pytest.raises(u.UnitsError): - ra = Longitude("4:08:15.162342") # error - hours or degrees? + Longitude("4:08:15.162342") # error - hours or degrees? with pytest.raises(u.UnitsError): - ra = Longitude("-4:08:15.162342") + Longitude("-4:08:15.162342") # the "smart" initializer allows >24 to automatically do degrees, but the # Angle-based one does not @@ -436,29 +432,27 @@ def test_radec(): # ra = Longitude("26:34:15.345634") # unambiguous b/c hours don't go past 24 # assert_allclose(ra.degree, 26.570929342) with pytest.raises(u.UnitsError): - ra = Longitude("26:34:15.345634") + Longitude("26:34:15.345634") # ra = Longitude(68) with pytest.raises(u.UnitsError): - ra = Longitude(68) + Longitude(68) with pytest.raises(u.UnitsError): - ra = Longitude(12) + Longitude(12) with pytest.raises(ValueError): - ra = Longitude("garbage containing a d and no units") + Longitude("garbage containing a d and no units") ra = Longitude("12h43m23s") assert_allclose(ra.hour, 12.7230555556) - # TODO: again, fix based on >24 behavior - # ra = Longitude((56,14,52.52)) - with pytest.raises(u.UnitsError): - ra = Longitude((56, 14, 52.52)) - with pytest.raises(u.UnitsError): - ra = Longitude((12, 14, 52)) # ambiguous w/o units - with pytest.warns(AstropyDeprecationWarning, match="hms_to_hours"): - ra = Longitude((12, 14, 52), unit=u.hour) + with pytest.raises(NotImplementedError, match="no longer supported"): + Longitude((56, 14, 52.52)) + with pytest.raises(NotImplementedError, match="no longer supported"): + Longitude((12, 14, 52)) # ambiguous w/o units + with pytest.raises(NotImplementedError, match="no longer supported"): + Longitude((12, 14, 52), unit=u.hour) # Units can be specified ra = Longitude("4:08:15.162342", unit=u.hour) @@ -468,7 +462,7 @@ def test_radec(): # nearly always specified in degrees, so this is the default. # dec = Latitude("-41:08:15.162342") with pytest.raises(u.UnitsError): - dec = Latitude("-41:08:15.162342") + Latitude("-41:08:15.162342") dec = Latitude("-41:08:15.162342", unit=u.degree) # same as above @@ -688,9 +682,9 @@ def test_wrap_at_inplace(): def test_latitude(): with pytest.raises(ValueError): - lat = Latitude(["91d", "89d"]) + Latitude(["91d", "89d"]) with pytest.raises(ValueError): - lat = Latitude("-91d") + Latitude("-91d") lat = Latitude(["90d", "89d"]) # check that one can get items @@ -735,7 +729,7 @@ def test_latitude(): TypeError, match="A Latitude angle cannot be created from a Longitude angle" ): lon = Longitude(10, "deg") - lat = Latitude(lon) + Latitude(lon) with pytest.raises( TypeError, match="A Longitude angle cannot be assigned to a Latitude angle" @@ -838,7 +832,7 @@ def test_longitude(): TypeError, match="A Longitude angle cannot be created from a Latitude angle" ): lat = Latitude(10, "deg") - lon = Longitude(lat) + Longitude(lat) with pytest.raises( TypeError, match="A Latitude angle cannot be assigned to a Longitude angle" @@ -929,12 +923,9 @@ def test_empty_sep(): def test_create_tuple(): """ Tests creation of an angle with an (h,m,s) tuple - - (d, m, s) tuples are not tested because of sign ambiguity issues (#13162) """ - with pytest.warns(AstropyDeprecationWarning, match="hms_to_hours"): - a1 = Angle((1, 30, 0), unit=u.hourangle) - assert a1.value == 1.5 + with pytest.raises(NotImplementedError, match="no longer supported"): + Angle((1, 30, 0), unit=u.hourangle) def test_list_of_quantities(): diff --git a/astropy/coordinates/tests/test_arrays.py b/astropy/coordinates/tests/test_arrays.py index 16e08ac28192..b105b3aeeeea 100644 --- a/astropy/coordinates/tests/test_arrays.py +++ b/astropy/coordinates/tests/test_arrays.py @@ -18,7 +18,6 @@ from astropy.tests.helper import assert_quantity_allclose as assert_allclose from astropy.time import Time from astropy.utils.compat import NUMPY_LT_1_24 -from astropy.utils.exceptions import AstropyDeprecationWarning def test_angle_arrays(): @@ -56,7 +55,7 @@ def test_angle_arrays(): else: stack.enter_context(pytest.raises(ValueError)) - a7 = Angle([a1, a2, a3], unit=u.degree) + Angle([a1, a2, a3], unit=u.degree) a8 = Angle(["04:02:02", "03:02:01", "06:02:01"], unit=u.degree) npt.assert_almost_equal(a8.value, [4.03388889, 3.03361111, 6.03361111]) @@ -65,7 +64,7 @@ def test_angle_arrays(): npt.assert_almost_equal(a9.value, a8.value) with pytest.raises(u.UnitsError): - a10 = Angle(["04:02:02", "03:02:01", "06:02:01"]) + Angle(["04:02:02", "03:02:01", "06:02:01"]) def test_dms(): @@ -87,10 +86,8 @@ def test_hms(): hours = hms[0] + hms[1] / 60.0 + hms[2] / 3600.0 npt.assert_almost_equal(a1.hour, hours) - with pytest.warns(AstropyDeprecationWarning, match="hms_to_hours"): - a2 = Angle(hms, unit=u.hour) - - npt.assert_almost_equal(a2.radian, a1.radian) + with pytest.raises(NotImplementedError, match="no longer supported"): + Angle(hms, unit=u.hour) def test_array_coordinates_creation(): @@ -101,9 +98,9 @@ def test_array_coordinates_creation(): assert not c.ra.isscalar with pytest.raises(ValueError): - c = ICRS(np.array([1, 2]) * u.deg, np.array([3, 4, 5]) * u.deg) + ICRS(np.array([1, 2]) * u.deg, np.array([3, 4, 5]) * u.deg) with pytest.raises(ValueError): - c = ICRS(np.array([1, 2, 4, 5]) * u.deg, np.array([[3, 4], [5, 6]]) * u.deg) + ICRS(np.array([1, 2, 4, 5]) * u.deg, np.array([[3, 4], [5, 6]]) * u.deg) # make sure cartesian initialization also works cart = CartesianRepresentation( @@ -116,9 +113,9 @@ def test_array_coordinates_creation(): # but invalid strings cannot with pytest.raises(ValueError): - c = SkyCoord(Angle(["10m0s", "2h02m00.3s"]), Angle(["3d", "4d"])) + SkyCoord(Angle(["10m0s", "2h02m00.3s"]), Angle(["3d", "4d"])) with pytest.raises(ValueError): - c = SkyCoord(Angle(["1d0m0s", "2h02m00.3s"]), Angle(["3x", "4d"])) + SkyCoord(Angle(["1d0m0s", "2h02m00.3s"]), Angle(["3x", "4d"])) def test_array_coordinates_distances(): diff --git a/docs/changes/coordinates/15205.api.rst b/docs/changes/coordinates/15205.api.rst new file mode 100644 index 000000000000..ca1e75cc936e --- /dev/null +++ b/docs/changes/coordinates/15205.api.rst @@ -0,0 +1,3 @@ +Removed deprecated ``dms_to_degrees``, ``hms_to_hours``, ``hms_to_degrees``, ``hms_to_radians``, +and ``hms_to_dms`` in ``astropy.coordinates.angle_formats``. Initializing ``Angle`` and ``Longitude`` +with a tuple is no longer supported and will now throw ``NotImplementedError``.