Skip to content

Commit 8a22c4c

Browse files
committed
[astropy#11094] Remove use of OrderedDescriptor[Container] from
astropy.coordinates Now frame_attributes is simply set in FrameMeta.__new__, and Attribute uses __set_name__. A bit of other minor simplification in FrameMeta.__new__
1 parent be4a98e commit 8a22c4c

2 files changed

Lines changed: 29 additions & 38 deletions

File tree

‎astropy/coordinates/attributes.py‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77

88
# Project
99
from astropy import units as u
10-
from astropy.utils import OrderedDescriptor, ShapedLikeNDArray
10+
from astropy.utils import ShapedLikeNDArray
1111

1212
__all__ = ['Attribute', 'TimeAttribute', 'QuantityAttribute',
1313
'EarthLocationAttribute', 'CoordinateAttribute',
1414
'CartesianRepresentationAttribute',
1515
'DifferentialAttribute']
1616

1717

18-
class Attribute(OrderedDescriptor):
18+
class Attribute:
1919
"""A non-mutable data descriptor to hold a frame attribute.
2020
2121
This class must be used to define frame attributes (e.g. ``equinox`` or
@@ -50,15 +50,16 @@ class FK4(BaseCoordinateFrame):
5050
``default is None`` and no value was supplied during initialization.
5151
"""
5252

53-
_class_attribute_ = 'frame_attributes'
54-
_name_attribute_ = 'name'
5553
name = '<unbound>'
5654

5755
def __init__(self, default=None, secondary_attribute=''):
5856
self.default = default
5957
self.secondary_attribute = secondary_attribute
6058
super().__init__()
6159

60+
def __set_name__(self, owner, name):
61+
self.name = name
62+
6263
def convert_input(self, value):
6364
"""
6465
Validate the input ``value`` and convert to expected attribute class.

‎astropy/coordinates/baseframe.py‎

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
from astropy.utils.decorators import lazyproperty, format_doc
2222
from astropy.utils.exceptions import AstropyWarning, AstropyDeprecationWarning
2323
from astropy import units as u
24-
from astropy.utils import (OrderedDescriptorContainer, ShapedLikeNDArray,
25-
check_broadcast)
24+
from astropy.utils import ShapedLikeNDArray, check_broadcast
2625
from .transformations import TransformGraph
2726
from . import representation as r
2827
from .angles import Angle
@@ -141,51 +140,36 @@ def _normalize_representation_type(kwargs):
141140
# with ShapedLikeNDArray below (which is an ABC); without it, one gets
142141
# "TypeError: metaclass conflict: the metaclass of a derived class must be a
143142
# (non-strict) subclass of the metaclasses of all its bases"
144-
class FrameMeta(OrderedDescriptorContainer, abc.ABCMeta):
143+
class FrameMeta(abc.ABCMeta):
145144
def __new__(mcls, name, bases, members):
146-
if 'default_representation' in members:
147-
default_repr = members.pop('default_representation')
148-
found_default_repr = True
149-
else:
150-
default_repr = None
151-
found_default_repr = False
152-
153-
if 'default_differential' in members:
154-
default_diff = members.pop('default_differential')
155-
found_default_diff = True
156-
else:
157-
default_diff = None
158-
found_default_diff = False
159-
160-
if 'frame_specific_representation_info' in members:
161-
repr_info = members.pop('frame_specific_representation_info')
162-
found_repr_info = True
163-
else:
164-
repr_info = None
165-
found_repr_info = False
145+
missing = object()
146+
default_repr = members.pop('default_representation', missing)
147+
default_diff = members.pop('default_differential', missing)
148+
repr_info = members.pop('frame_specific_representation_info', missing)
149+
frame_attrs = members.pop('frame_attributes', missing)
166150

167151
# somewhat hacky, but this is the best way to get the MRO according to
168152
# https://mail.python.org/pipermail/python-list/2002-December/167861.html
169153
tmp_cls = super().__new__(mcls, name, bases, members)
170154

171-
# now look through the whole MRO for the class attributes, raw for
172-
# frame_attr_names, and leading underscore for others
155+
# now look through the whole MRO for the class attributes
173156
for m in (c.__dict__ for c in tmp_cls.__mro__):
174-
if not found_default_repr and '_default_representation' in m:
157+
if default_repr is missing and '_default_representation' in m:
175158
default_repr = m['_default_representation']
176-
found_default_repr = True
177159

178-
if not found_default_diff and '_default_differential' in m:
160+
if default_diff is missing and '_default_differential' in m:
179161
default_diff = m['_default_differential']
180-
found_default_diff = True
181162

182-
if (not found_repr_info
163+
if (repr_info is missing
183164
and '_frame_specific_representation_info' in m):
184165
# create a copy of the dict so we don't mess with the contents
185166
repr_info = m['_frame_specific_representation_info'].copy()
186-
found_repr_info = True
187167

188-
if found_default_repr and found_default_diff and found_repr_info:
168+
if frame_attrs is missing and 'frame_attributes' in m:
169+
frame_attrs = m['frame_attributes'].copy()
170+
171+
if missing not in (default_repr, default_diff, repr_info,
172+
frame_attrs):
189173
break
190174
else:
191175
raise ValueError(
@@ -279,6 +263,14 @@ def __new__(mcls, name, bases, members):
279263
'frame_specific_representation_info',
280264
copy.deepcopy(repr_info))
281265

266+
# Set the frame attributes
267+
# TODO: Should this be made to use readonly_prop_factory as well or
268+
# would it be inconvenient for getting the frame_attributes from
269+
# classes?
270+
frame_attrs.update({k: v for k, v in members.items()
271+
if isinstance(v, Attribute)})
272+
members['frame_attributes'] = frame_attrs
273+
282274
# now set the frame name as lower-case class name, if it isn't explicit
283275
if 'name' not in members:
284276
members['name'] = name.lower()
@@ -407,8 +399,6 @@ class BaseCoordinateFrame(ShapedLikeNDArray, metaclass=FrameMeta):
407399
# attributes.
408400
frame_specific_representation_info = {}
409401

410-
_inherit_descriptors_ = (Attribute,)
411-
412402
frame_attributes = {}
413403
# Default empty frame_attributes dict
414404

0 commit comments

Comments
 (0)