Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename Fits class in units.format to FITS #16455

Merged
merged 1 commit into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions astropy/units/format/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@
A collection of different unit formats.
"""

import sys
import warnings

from astropy.utils.exceptions import AstropyDeprecationWarning

# This is pretty atrocious, but it will prevent a circular import for those
# formatters that need access to the units.core module An entry for it should
# exist in sys.modules since astropy.units.core imports this module
import sys

core = sys.modules["astropy.units.core"]

from .base import Base
from .cds import CDS
from .console import Console
from .fits import Fits
from .fits import FITS
from .generic import Generic, Unscaled
from .latex import Latex, LatexInline
from .ogip import OGIP
Expand All @@ -26,7 +29,7 @@
"Generic",
"CDS",
"Console",
"Fits",
"FITS",
"Latex",
"LatexInline",
"OGIP",
Expand All @@ -37,6 +40,19 @@
]


def __getattr__(name):
if name == "Fits":
warnings.warn(

Check warning on line 45 in astropy/units/format/__init__.py

View check run for this annotation

Codecov / codecov/patch

astropy/units/format/__init__.py#L45

Added line #L45 was not covered by tests
AstropyDeprecationWarning(
'The class "Fits" has been renamed to "FITS" in version 7.0. The old '
"name is deprecated and may be removed in a future version.\n"
" Use FITS instead."
)
)
return FITS

Check warning on line 52 in astropy/units/format/__init__.py

View check run for this annotation

Codecov / codecov/patch

astropy/units/format/__init__.py#L52

Added line #L52 was not covered by tests
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")


def _known_formats():
inout = [
name
Expand Down
2 changes: 1 addition & 1 deletion astropy/units/format/fits.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from . import core, generic, utils


class Fits(generic.Generic):
class FITS(generic.Generic):
"""
The FITS standard unit format.

Expand Down
24 changes: 19 additions & 5 deletions astropy/units/tests/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from astropy.units import PrefixUnit, Unit, UnitBase, UnitsWarning, dex
from astropy.units import format as u_format
from astropy.units.utils import is_effectively_unity
from astropy.utils.exceptions import AstropyDeprecationWarning


@pytest.mark.parametrize(
Expand Down Expand Up @@ -306,13 +307,13 @@ def test_roundtrip(self, unit):

class TestRoundtripFITS(RoundtripBase):
format_ = "fits"
deprecated_units = u_format.Fits._deprecated_units
deprecated_units = u_format.FITS._deprecated_units

@pytest.mark.parametrize(
"unit",
[
unit
for unit in u_format.Fits._units.values()
for unit in u_format.FITS._units.values()
if (isinstance(unit, UnitBase) and not isinstance(unit, PrefixUnit))
],
)
Expand Down Expand Up @@ -388,7 +389,7 @@ def test_roundtrip(self, unit):


def test_fits_units_available():
u_format.Fits._units
u_format.FITS._units


def test_vo_units_available():
Expand Down Expand Up @@ -566,7 +567,7 @@ def test_fits_to_string_function_error():
"""

with pytest.raises(TypeError, match="unit argument must be"):
u_format.Fits.to_string(None)
u_format.FITS.to_string(None)


def test_fraction_repr():
Expand Down Expand Up @@ -650,7 +651,7 @@ def test_fits_function(string):
# Function units cannot be written, so ensure they're not parsed either.
with pytest.raises(ValueError):
print(string)
u_format.Fits().parse(string)
u_format.FITS().parse(string)


@pytest.mark.parametrize("string", ["mag(ct/s)", "dB(mW)", "dex(cm s**-2)"])
Expand Down Expand Up @@ -1003,3 +1004,16 @@ def test_format_latex_one(format_spec, expected_mantissa):
m, ex = split_mantissa_exponent(1, format_spec)
assert ex == ""
assert m == expected_mantissa


def test_Fits_name_deprecation():
with pytest.warns(
AstropyDeprecationWarning,
match=(
r'^The class "Fits" has been renamed to "FITS" in version 7\.0\. '
r"The old name is deprecated and may be removed in a future version\.\n"
r" Use FITS instead\.$"
),
):
from astropy.units.format import Fits
assert Fits is u.format.FITS
4 changes: 4 additions & 0 deletions docs/changes/units/16455.api.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
The ``format.Fits`` formatter class has been renamed to ``format.FITS`` and the
old name is deprecated.
Specifying the FITS format for converting ``Quantity`` and ``UnitBase``
instances to and from strings is not affected by this change.