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

Attempt 2 Make class variables immutable #188

Merged
merged 4 commits into from
Jul 14, 2023
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
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

- Drop support for Python 3.8 in accordance with NEP 29. [#180]
- Update ``RepresentationConverter`` for new class paths in astropy [#181]
- Update Converters so that all Class Variables are immutable [#188]

0.4.0 (2023-03-20)
------------------
Expand Down
15 changes: 6 additions & 9 deletions asdf_astropy/converters/coordinates/angle.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@


class AngleConverter(QuantityConverter):
tags = ["tag:astropy.org:astropy/coordinates/angle-*"]

types = ["astropy.coordinates.angles.Angle"]
tags = ("tag:astropy.org:astropy/coordinates/angle-*",)
types = ("astropy.coordinates.angles.Angle",)

def from_yaml_tree(self, node, tag, ctx):
from astropy.coordinates.angles import Angle
Expand All @@ -13,9 +12,8 @@ def from_yaml_tree(self, node, tag, ctx):


class LatitudeConverter(QuantityConverter):
tags = ["tag:astropy.org:astropy/coordinates/latitude-*"]

types = ["astropy.coordinates.angles.Latitude"]
tags = ("tag:astropy.org:astropy/coordinates/latitude-*",)
types = ("astropy.coordinates.angles.Latitude",)

def from_yaml_tree(self, node, tag, ctx):
from astropy.coordinates.angles import Latitude
Expand All @@ -24,9 +22,8 @@ def from_yaml_tree(self, node, tag, ctx):


class LongitudeConverter(QuantityConverter):
tags = ["tag:astropy.org:astropy/coordinates/longitude-*"]

types = ["astropy.coordinates.angles.Longitude"]
tags = ("tag:astropy.org:astropy/coordinates/longitude-*",)
types = ("astropy.coordinates.angles.Longitude",)

def to_yaml_tree(self, obj, tag, ctx):
tree = super().to_yaml_tree(obj, tag, ctx)
Expand Down
5 changes: 2 additions & 3 deletions asdf_astropy/converters/coordinates/earth_location.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@


class EarthLocationConverter(Converter):
tags = ["tag:astropy.org:astropy/coordinates/earthlocation-*"]

types = ["astropy.coordinates.earth.EarthLocation"]
tags = ("tag:astropy.org:astropy/coordinates/earthlocation-*",)
types = ("astropy.coordinates.earth.EarthLocation",)

def to_yaml_tree(self, obj, tag, ctx):
return obj.info._represent_as_dict()
Expand Down
5 changes: 2 additions & 3 deletions asdf_astropy/converters/coordinates/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,10 @@ def from_yaml_tree(self, node, tag, ctx):


class LegacyICRSConverter(Converter):
tags = ["tag:astropy.org:astropy/coordinates/frames/icrs-1.0.0"]

tags = ("tag:astropy.org:astropy/coordinates/frames/icrs-1.0.0",)
# Leave the types list empty so that the 1.1.0 ICRS converter
# is used on write.
types = []
types = ()

def to_yaml_tree(self, obj, tag, ctx):
from astropy.units import Quantity
Expand Down
7 changes: 3 additions & 4 deletions asdf_astropy/converters/coordinates/representation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@


class RepresentationConverter(Converter):
tags = ["tag:astropy.org:astropy/coordinates/representation-*"]

types = [
tags = ("tag:astropy.org:astropy/coordinates/representation-*",)
types = (
"astropy.coordinates.representation.CartesianDifferential",
"astropy.coordinates.representation.CartesianRepresentation",
"astropy.coordinates.representation.CylindricalDifferential",
Expand Down Expand Up @@ -34,7 +33,7 @@ class RepresentationConverter(Converter):
"astropy.coordinates.representation.spherical.UnitSphericalRepresentation",
"astropy.coordinates.representation.spherical.UnitSphericalDifferential",
"astropy.coordinates.representation.spherical.UnitSphericalCosLatDifferential",
]
)

def to_yaml_tree(self, obj, tag, ctx):
components = {}
Expand Down
5 changes: 2 additions & 3 deletions asdf_astropy/converters/coordinates/sky_coord.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@


class SkyCoordConverter(Converter):
tags = ["tag:astropy.org:astropy/coordinates/skycoord-*"]

types = ["astropy.coordinates.sky_coordinate.SkyCoord"]
tags = ("tag:astropy.org:astropy/coordinates/skycoord-*",)
types = ("astropy.coordinates.sky_coordinate.SkyCoord",)

def to_yaml_tree(self, obj, tag, ctx):
return obj.info._represent_as_dict()
Expand Down
5 changes: 2 additions & 3 deletions asdf_astropy/converters/coordinates/spectral_coord.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@


class SpectralCoordConverter(Converter):
tags = ["tag:astropy.org:astropy/coordinates/spectralcoord-*"]

types = ["astropy.coordinates.spectral_coordinate.SpectralCoord"]
tags = ("tag:astropy.org:astropy/coordinates/spectralcoord-*",)
types = ("astropy.coordinates.spectral_coordinate.SpectralCoord",)

def to_yaml_tree(self, obj, tag, ctx):
node = {
Expand Down
8 changes: 4 additions & 4 deletions asdf_astropy/converters/fits/fits.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ def from_yaml_tree(self, node, tag, ctx):


class AsdfFitsConverter(FitsConverter):
tags = ["tag:stsci.edu:asdf/fits/fits-*"]
types = []
tags = ("tag:stsci.edu:asdf/fits/fits-*",)
types = ()


class AstropyFitsConverter(FitsConverter):
tags = ["tag:astropy.org:astropy/fits/fits-*"]
types = ["astropy.io.fits.hdu.hdulist.HDUList"]
tags = ("tag:astropy.org:astropy/fits/fits-*",)
types = ("astropy.io.fits.hdu.hdulist.HDUList",)
19 changes: 8 additions & 11 deletions asdf_astropy/converters/table/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@


class ColumnConverter(Converter):
tags = ["tag:stsci.edu:asdf/core/column-*"]

types = [
tags = ("tag:stsci.edu:asdf/core/column-*",)
types = (
"astropy.table.column.Column",
"astropy.table.column.MaskedColumn",
]
)

def to_yaml_tree(self, obj, tag, ctx):
node = {"data": obj.data, "name": obj.name}
Expand Down Expand Up @@ -46,9 +45,8 @@ def from_yaml_tree(self, node, tag, ctx):


class AsdfTableConverter(Converter):
tags = ["tag:stsci.edu:asdf/core/table-*"]

types = []
tags = ("tag:stsci.edu:asdf/core/table-*",)
types = ()

def to_yaml_tree(self, obj, tag, ctx):
msg = "astropy does not support writing astropy.table.Table with the ASDF table-1.0.0 tag"
Expand All @@ -61,12 +59,11 @@ def from_yaml_tree(self, node, tag, ctx):


class AstropyTableConverter(Converter):
tags = ["tag:astropy.org:astropy/table/table-*"]

types = [
tags = ("tag:astropy.org:astropy/table/table-*",)
types = (
"astropy.table.table.Table",
"astropy.table.table.QTable",
]
)

def to_yaml_tree(self, obj, tag, ctx):
from astropy.table import QTable
Expand Down
5 changes: 2 additions & 3 deletions asdf_astropy/converters/time/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@


class TimeConverter(Converter):
tags = ["tag:stsci.edu:asdf/time/time-*"]

types = ["astropy.time.core.Time"]
tags = ("tag:stsci.edu:asdf/time/time-*",)
types = ("astropy.time.core.Time",)

def to_yaml_tree(self, obj, tag, ctx):
from astropy.time import Time
Expand Down
5 changes: 2 additions & 3 deletions asdf_astropy/converters/time/time_delta.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@


class TimeDeltaConverter(Converter):
tags = ["tag:astropy.org:astropy/time/timedelta-*"]

types = ["astropy.time.core.TimeDelta"]
tags = ("tag:astropy.org:astropy/time/timedelta-*",)
types = ("astropy.time.core.TimeDelta",)

def to_yaml_tree(self, obj, tag, ctx):
return obj.info._represent_as_dict()
Expand Down
7 changes: 3 additions & 4 deletions asdf_astropy/converters/transform/compound.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class CompoundConverter(TransformConverterBase):
ASDF serialization support for CompoundModel.
"""

tags = [
tags = (
"tag:stsci.edu:asdf/transform/add-*",
"tag:stsci.edu:asdf/transform/subtract-*",
"tag:stsci.edu:asdf/transform/multiply-*",
Expand All @@ -43,9 +43,8 @@ class CompoundConverter(TransformConverterBase):
"tag:stsci.edu:asdf/transform/compose-*",
"tag:stsci.edu:asdf/transform/concatenate-*",
"tag:stsci.edu:asdf/transform/fix_inputs-*",
]

types = ["astropy.modeling.core.CompoundModel"]
)
types = ("astropy.modeling.core.CompoundModel",)

def select_tag(self, model, tags, ctx):
tag_name = _OPERATOR_TO_TAG_NAME[model.op]
Expand Down
7 changes: 3 additions & 4 deletions asdf_astropy/converters/transform/functional_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ class ConstantConverter(TransformConverterBase):
# previously all values were 1D.
_2D_MIN_VERSION = parse_version("1.4.0")

tags = ["tag:stsci.edu:asdf/transform/constant-*"]

types = [
tags = ("tag:stsci.edu:asdf/transform/constant-*",)
types = (
"astropy.modeling.functional_models.Const1D",
"astropy.modeling.functional_models.Const2D",
]
)

def to_yaml_tree_transform(self, model, tag, ctx):
from astropy.modeling.functional_models import Const1D, Const2D
Expand Down
15 changes: 6 additions & 9 deletions asdf_astropy/converters/transform/mappings.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ class IdentityConverter(TransformConverterBase):
ASDF support for serializing the Identity model.
"""

tags = ["tag:stsci.edu:asdf/transform/identity-*"]

types = ["astropy.modeling.mappings.Identity"]
tags = ("tag:stsci.edu:asdf/transform/identity-*",)
types = ("astropy.modeling.mappings.Identity",)

def to_yaml_tree_transform(self, model, tag, ctx):
node = {}
Expand All @@ -29,9 +28,8 @@ class RemapAxesConverter(TransformConverterBase):
ASDF support for serializing the Mapping model
"""

tags = ["tag:stsci.edu:asdf/transform/remap_axes-*"]

types = ["astropy.modeling.mappings.Mapping"]
tags = ("tag:stsci.edu:asdf/transform/remap_axes-*",)
types = ("astropy.modeling.mappings.Mapping",)

def to_yaml_tree_transform(self, model, tag, ctx):
node = {"mapping": list(model.mapping)}
Expand All @@ -53,9 +51,8 @@ class UnitsMappingConverter(Converter):
from other models.
"""

tags = ["tag:astropy.org:astropy/transform/units_mapping-*"]

types = ["astropy.modeling.mappings.UnitsMapping"]
tags = ("tag:astropy.org:astropy/transform/units_mapping-*",)
types = ("astropy.modeling.mappings.UnitsMapping",)

def to_yaml_tree(self, model, tag, ctx):
node = {}
Expand Down
5 changes: 2 additions & 3 deletions asdf_astropy/converters/transform/math_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,8 @@ class MathFunctionsConverter(TransformConverterBase):
each of which corresponds to a numpy ufunc.
"""

tags = ["tag:stsci.edu:asdf/transform/math_functions-*"]

types = ["astropy.modeling.math_functions." + m for m in _MODEL_NAMES]
tags = ("tag:stsci.edu:asdf/transform/math_functions-*",)
types = tuple("astropy.modeling.math_functions." + m for m in _MODEL_NAMES)

def to_yaml_tree_transform(self, model, tag, ctx):
return {"func_name": model.func.__name__}
Expand Down
40 changes: 20 additions & 20 deletions asdf_astropy/converters/transform/polynomial.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ class PolynomialConverter(TransformConverterBase):
# versions because they don't validate.
_DOMAIN_WINDOW_MIN_VERSION = parse_version("1.2.0")

tags = ["tag:stsci.edu:asdf/transform/polynomial-*"]

types = [
tags = ("tag:stsci.edu:asdf/transform/polynomial-*",)
types = (
"astropy.modeling.polynomial.Polynomial1D",
"astropy.modeling.polynomial.Polynomial2D",
]
)

def to_yaml_tree_transform(self, model, tag, ctx):
from astropy.modeling.polynomial import Polynomial1D, Polynomial2D
Expand Down Expand Up @@ -95,37 +94,38 @@ def from_yaml_tree_transform(self, node, tag, ctx):
return model


_CLASS_NAME_TO_POLY_INFO = {
"Legendre1D": ("legendre", 1),
"Legendre2D": ("legendre", 2),
"Chebyshev1D": ("chebyshev", 1),
"Chebyshev2D": ("chebyshev", 2),
"Hermite1D": ("hermite", 1),
"Hermite2D": ("hermite", 2),
}

_POLY_INFO_TO_CLASS_NAME = {v: k for k, v in _CLASS_NAME_TO_POLY_INFO.items()}


class OrthoPolynomialConverter(TransformConverterBase):
"""
ASDF support for serializing models that inherit
OrthoPolyomialBase.
"""

# Map of model class name to (polynomial type, number of dimensions) tuple:
_CLASS_NAME_TO_POLY_INFO = {
"Legendre1D": ("legendre", 1),
"Legendre2D": ("legendre", 2),
"Chebyshev1D": ("chebyshev", 1),
"Chebyshev2D": ("chebyshev", 2),
"Hermite1D": ("hermite", 1),
"Hermite2D": ("hermite", 2),
}

_POLY_INFO_TO_CLASS_NAME = {v: k for k, v in _CLASS_NAME_TO_POLY_INFO.items()}

tags = ["tag:stsci.edu:asdf/transform/ortho_polynomial-*"]

types = [
tags = ("tag:stsci.edu:asdf/transform/ortho_polynomial-*",)
types = (
"astropy.modeling.polynomial.Legendre1D",
"astropy.modeling.polynomial.Legendre2D",
"astropy.modeling.polynomial.Chebyshev1D",
"astropy.modeling.polynomial.Chebyshev2D",
"astropy.modeling.polynomial.Hermite1D",
"astropy.modeling.polynomial.Hermite2D",
]
)

def to_yaml_tree_transform(self, model, tag, ctx):
poly_type = self._CLASS_NAME_TO_POLY_INFO[model.__class__.__name__][0]
poly_type = _CLASS_NAME_TO_POLY_INFO[model.__class__.__name__][0]
if model.n_inputs == 1:
coefficients = np.array(model.parameters)
else:
Expand Down Expand Up @@ -157,7 +157,7 @@ def from_yaml_tree_transform(self, node, tag, ctx):
poly_type = node["polynomial_type"]
n_dim = coefficients.ndim

class_name = self._POLY_INFO_TO_CLASS_NAME[(poly_type, n_dim)]
class_name = _POLY_INFO_TO_CLASS_NAME[(poly_type, n_dim)]
model_type = getattr(polynomial, class_name)

coefficients = np.asarray(node["coefficients"])
Expand Down
8 changes: 4 additions & 4 deletions asdf_astropy/converters/transform/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@


class ModelBoundingBoxConverter(Converter):
tags = ["tag:stsci.edu:asdf/transform/property/bounding_box-1.0.0"]
types = ["astropy.modeling.bounding_box.ModelBoundingBox"]
tags = ("tag:stsci.edu:asdf/transform/property/bounding_box-1.0.0",)
types = ("astropy.modeling.bounding_box.ModelBoundingBox",)

def to_yaml_tree(self, bbox, tag, ctx):
return {
Expand Down Expand Up @@ -40,8 +40,8 @@ def create_bounding_box(model, cbbox=None):


class CompoundBoundingBoxConverter(Converter):
tags = ["tag:stsci.edu:asdf/transform/property/compound_bounding_box-1.0.0"]
types = ["astropy.modeling.bounding_box.CompoundBoundingBox"]
tags = ("tag:stsci.edu:asdf/transform/property/compound_bounding_box-1.0.0",)
types = ("astropy.modeling.bounding_box.CompoundBoundingBox",)

def to_yaml_tree(self, cbbox, tag, ctx):
node = {
Expand Down
Loading
Loading