Skip to content

Use descriptors for frame attributes - #2626

Merged
taldcroft merged 8 commits into
astropy:masterfrom
taldcroft:coordinates-frame-attr-descriptor
Jun 19, 2014
Merged

taldcroft merged 8 commits into
astropy:masterfrom
taldcroft:coordinates-frame-attr-descriptor

Conversation

@taldcroft

Copy link
Copy Markdown
Member

This changes the primary mechanism for defining frame attributes like equinox or obstime from the frame_attr_names class attribute to properties using a FrameAttribute class descriptor.

The current implementation has a limitation that the frame attributes are not inherited when subclassing. Given realistic use cases I don't think this is a big problem but we could always put the MRO searching back in. (Django has a similar issue for database fields, and they have a limitation that you can only (easily) inherit from specially marked abstract classes).

@taldcroft

Copy link
Copy Markdown
Member Author

@eteq - previous test fails were unrelated, but I had to rebase. I just added some unit tests of FrameAttribute which should test and demonstrate the slightly intricate logic relating to default handling. I feel better now with some tests. :-)

@eteq

eteq commented Jun 15, 2014

Copy link
Copy Markdown
Member

@taldcroft - I was just about to point out the Example -> Examples and need to add it to __all__... so you read my mind!

So now this looks good except for three things:

  • Not using the MRO to get the attribute names is a problem, IMHO. Note the following behavior:
>>> class SubFK5(FK5):
>>>     pass
>>> c = SubFK5()
>>> c
<SubFK5 Frame>
>>> c.equinox
<Time object: scale='utc' format='jyear_str' value=J2000.000>
>>> c.frame_attr_names()
{}

That's confusing: the equinox attribute exists because it's in the superclass, but it's not settable or visible in frame_attr_names. So I would prefer if you put back the MRO-searching code when populating frame_attr_names. As you say, it doesn't matter right now because there are no sub-class frames, but I think consistency now is important so as not to have to break it in future versions. Also, a PR I'm working on now will probably need the MRO-searching stuff to stay in.

  • The machinery to do the conversion from strings like 'J2000' to Time objects still lives in BaseCoordinateFrame. It might make more sense to add a convert_input method to FrameAttribute that can be used in BaseCoordinateFrame, and then also add a TimeFrameAttribute subclass that just overrides convert_input. That would allow user-defined FrameAttributes that do any arbitrary sort of conversions (which I'd love, because the time_attr_names grated on me as I wrote it, as it's not as extensible as I would have liked)
  • I think it would be better to rename frame_attr_names to get_frame_attr_names so that it's clear it's now a method, not an attribute.

@taldcroft

Copy link
Copy Markdown
Member Author

@eteq - almost there with getting rid of time_attr_name (agreed this is a very good thing). Something is still messed up but I have to stop. If you have time check out the commit here : taldcroft@b49b0c8.

Also: https://github.com/taldcroft/astropy/compare/coordinates-generic-frame-attr

@eteq eteq mentioned this pull request Jun 16, 2014
@embray

embray commented Jun 16, 2014

Copy link
Copy Markdown
Member

I hadn't looked too closely at much of the coordinates code yet, but this looks like a much nicer solution than what was already there.

@eteq

eteq commented Jun 17, 2014

Copy link
Copy Markdown
Member

@taldcroft - if it helps any, I cleaned up the MRO stuff some in #2629 , so that might help here...

@taldcroft

Copy link
Copy Markdown
Member Author

@eteq - I got stuck in a deep rabbit hole on this but finally dug my way out. This is passing tests locally and as far as I know the documentation updates are complete. Some of the logic is more intricate than I'd like but I didn't find any ways to simplify further.

@taldcroft taldcroft added this to the v0.4.0 milestone Jun 18, 2014
@taldcroft

Copy link
Copy Markdown
Member Author

OK, tests passing and ready for final review. I milestoned this for 0.4.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I get the point of this - what exactly does it validate?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you set the _ internal version with the frame attribute from the user, nothing more gets done (now that the _convert_to_time function is no longer being called here). It's not until you get into FrameAttribute.__get__ that the convert_input method and all that logic goes into action. It's a little hacky, but just getting the attribute here was the easiest way I could think of to force an error here in init rather than later the first time the attribute is accessed.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha, I get it now, and that makes sense.

@eteq

eteq commented Jun 18, 2014

Copy link
Copy Markdown
Member

Looks good to merge to me, @taldcroft, aside from the minor comment and question above (which may require no action depending on the answer).

@taldcroft

Copy link
Copy Markdown
Member Author

@eteq - Now we have a fully working example with doctest coverage. Much better! And I think this is ready for merge now? The getattr issue is, in any case, an implementation detail that we don't have to resolve right now.

@taldcroft

Copy link
Copy Markdown
Member Author

@eteq - ball in your court now. I think this is done.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why this isn't:

out : correctly-typed object
    ...
converted : bool
    ...

? That's what it normally looks like when there are two outputs...

@eteq

eteq commented Jun 19, 2014

Copy link
Copy Markdown
Member

@taldcroft - one very minor remaining doc comment, but the code itself looks good to me. If you haven't gotten to this in a little while, I'll probably just merge this so that I can get to rebasing #2629 , and the docfix can go in a separate PR or something.

@taldcroft

Copy link
Copy Markdown
Member Author

@eteq - I'm just going to merge now to save waiting for Travis (which would be formality, but still...). We can fix it any time, maybe even in #2629. I just didn't know the right format for multiple outputs.

taldcroft added a commit that referenced this pull request Jun 19, 2014
…ptor

Use descriptors for frame attributes
@taldcroft
taldcroft merged commit 3327d41 into astropy:master Jun 19, 2014
@taldcroft
taldcroft deleted the coordinates-frame-attr-descriptor branch June 19, 2014 02:13
@eteq

eteq commented Jun 19, 2014

Copy link
Copy Markdown
Member

@taldcroft - cool - I'll probably fix it in #2629 then, or possibly as a separate minor PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants