Skip to content

Commit 8dd7af0

Browse files
embraynstarman
authored andcommitted
[astropy#11094] Collect Frame Attributes from all base classes as well, fixing
a regression from the previous version (since OrderedDescriptorContainer did this automatically) It should not be necessary to walk the entire MRO since this merging of Attributes is already performed cumulatively by each base class's __init_subclass__.
1 parent ba8b835 commit 8dd7af0

2 files changed

Lines changed: 41 additions & 2 deletions

File tree

‎astropy/coordinates/baseframe.py‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,14 @@ def __init_subclass__(cls, **kwargs):
284284
else:
285285
frame_attrs = frame_attrs.copy()
286286

287-
frame_attrs.update({k: v for k, v in cls.__dict__.items()
288-
if isinstance(v, Attribute)})
287+
for basecls in (cls,) + cls.__bases__:
288+
if not issubclass(basecls, BaseCoordinateFrame):
289+
continue
290+
291+
for k, v in basecls.__dict__.items():
292+
if isinstance(v, Attribute):
293+
frame_attrs.setdefault(k, v)
294+
289295
cls.frame_attributes = frame_attrs
290296

291297
# Deal with setting the name of the frame:

‎astropy/coordinates/tests/test_frames.py‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,39 @@ class MyFK4(FK4):
121121
assert mfk4.newattr == 'world'
122122

123123

124+
def test_frame_multiple_inheritance_attribute_descriptor():
125+
"""
126+
Ensure that all attributes are accumulated in case of inheritance from
127+
multiple BaseCoordinateFrames. See
128+
https://github.com/astropy/astropy/pull/11099#issuecomment-735829157
129+
"""
130+
131+
class Frame1(BaseCoordinateFrame):
132+
attr1 = Attribute()
133+
134+
class Frame2(BaseCoordinateFrame):
135+
attr2 = Attribute()
136+
137+
class Frame3(Frame1, Frame2):
138+
pass
139+
140+
assert len(Frame3.frame_attributes) == 2
141+
assert 'attr1' in Frame3.frame_attributes
142+
assert 'attr2' in Frame3.frame_attributes
143+
144+
# In case the same attribute exists in both frames, the one from the
145+
# left-most class in the MRO should take precedence
146+
class Frame4(BaseCoordinateFrame):
147+
attr1 = Attribute()
148+
attr2 = Attribute()
149+
150+
class Frame5(Frame1, Frame4):
151+
pass
152+
153+
assert Frame5.frame_attributes['attr1'] is Frame1.frame_attributes['attr1']
154+
assert Frame5.frame_attributes['attr2'] is Frame4.frame_attributes['attr2']
155+
156+
124157
def test_differentialattribute():
125158

126159
# Test logic of passing input through to allowed class

0 commit comments

Comments
 (0)