Skip to content

Refactor representations as a module - #14792

Merged
mhvk merged 7 commits into
astropy:mainfrom
mhvk:representations-as-module
May 10, 2023
Merged

mhvk merged 7 commits into
astropy:mainfrom
mhvk:representations-as-module

Conversation

@mhvk

@mhvk mhvk commented May 8, 2023 •

Copy link
Copy Markdown
Contributor

This is a reimplementation of #13688, redoing the refactoring to ensure that (1) there are no changes other than what is strictly necessary, and (2) any commits since that time are included (I restarted when I noticed #13688 had an import of a deprecated function that had been removed, and worried there were other changes). I avoided introducing any __all__ in the submodules, for now just relying on importing what is relevant in the new __init__.py. I cherry-picked commits from #13688 that fix errors in the documentation and in serialization because of the class name changes (making a few small changes in the process). Like in #13688, I also keep a few variables in __init__.py that are not exported, but are used in tests, for backward compatibility.

I mostly did this since I still think the refactoring is a very good idea, and it would be helpful for #14763, which introduces more general geodetic representations, if there is a place to put them.

Ping @nstarman who did #13688 and @eerovaher who had requested changes to it, mostly to ensure we make no actual code changes (which I think I ensured!); if we all still think this is a good idea, we should probably get it in sooner rather than later... (thankfully, representations is quite mature, so I don't foresee problems with backporting to 5.3).

p.s. The commits are meant to be fairly logical: for all pytest -P coordinates worked (except a few names in doctests after spherical is factored out).

EDIT: Close #13688

mhvk and others added 7 commits May 8, 2023 16:46
Only change is to use absolute imports for other modules in astropy.coordinates.
Just copied text over to new file and added required imports on top.
In __init__.py, added import from new .cartesian at bottom to avoid
circular imports.
Again, just adding imports on top, and an import from cylindrical
at the bottom of __init__.py.
With usual local imports on top, and import from spherical at bottom
of __init__.py. Also change to import in cyclindrical, and adjustment
of one test, since the location of the classes now has changed.

Not yet changing the locations in the failing doctests.
Now __init__.py can be cleaned up.  Import some variables for backward
compatibility.  For the one private one only used in a test, change the
test.  Change imports on top of cartesian, etc., to be from .base.
Signed-off-by: Nathaniel Starkman (@nstarman) <[email protected]>
@github-actions

github-actions Bot commented May 8, 2023

Copy link
Copy Markdown
Contributor

Thank you for your contribution to Astropy! 🌌 This checklist is meant to remind the package maintainers who will review this pull request of some common things to look for.

  • Do the proposed changes actually accomplish desired goals?
  • Do the proposed changes follow the Astropy coding guidelines?
  • Are tests added/updated as required? If so, do they follow the Astropy testing guidelines?
  • Are docs added/updated as required? If so, do they follow the Astropy documentation guidelines?
  • Is rebase and/or squash necessary? If so, please provide the author with appropriate instructions. Also see "When to rebase and squash commits".
  • Did the CI pass? If no, are the failures related? If you need to run daily and weekly cron jobs as part of the PR, please apply the "Extra CI" label. Codestyle issues can be fixed by the bot.
  • Is a change log needed? If yes, did the change log check pass? If no, add the "no-changelog-entry-needed" label. If this is a manual backport, use the "skip-changelog-checks" label unless special changelog handling is necessary.
  • Is this a big PR that makes a "What's new?" entry worthwhile and if so, is (1) a "what's new" entry included in this PR and (2) the "whatsnew-needed" label applied?
  • Is a milestone set? Milestone must be set but we cannot check for it on Actions; do not let the green checkmark fool you.
  • At the time of adding the milestone, if the milestone set requires a backport to release branch(es), apply the appropriate "backport-X.Y.x" label(s) before merge.

@mhvk mhvk mentioned this pull request May 8, 2023
3 of 13 tasks

@nstarman nstarman left a comment

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.

This looks good and was very easy to review, stepping though the commits!

Comment on lines +26 to +28
from .base import DIFFERENTIAL_CLASSES
from .base import DUPLICATE_REPRESENTATIONS
from .base import REPRESENTATION_CLASSES

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.

Would it be worthwhile to make these read-only?

Suggested change
from .base import DIFFERENTIAL_CLASSES
from .base import DUPLICATE_REPRESENTATIONS
from .base import REPRESENTATION_CLASSES
from types import MappingProxyType
from .base import MappingProxyType(DIFFERENTIAL_CLASSES)
from .base import MappingProxyType(DUPLICATE_REPRESENTATIONS)
from .base import MappingProxyType(REPRESENTATION_CLASSES)

Or to deprecate their access from here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I agree it should be deprecated, which is relatively easy to do with __getattr__, but think it is best to do as a separate PR after this, so one would not be distracted by all the other changes.

Comment on lines +45 to +58
"astropy.coordinates.representation.cartesian.CartesianRepresentation",
"astropy.coordinates.representation.spherical.UnitSphericalRepresentation",
"astropy.coordinates.representation.spherical.RadialRepresentation",
"astropy.coordinates.representation.spherical.SphericalRepresentation",
"astropy.coordinates.representation.spherical.PhysicsSphericalRepresentation",
"astropy.coordinates.representation.cylindrical.CylindricalRepresentation",
"astropy.coordinates.representation.cartesian.CartesianDifferential",
"astropy.coordinates.representation.spherical.UnitSphericalDifferential",
"astropy.coordinates.representation.spherical.SphericalDifferential",
"astropy.coordinates.representation.spherical.UnitSphericalCosLatDifferential",
"astropy.coordinates.representation.spherical.SphericalCosLatDifferential",
"astropy.coordinates.representation.spherical.RadialDifferential",
"astropy.coordinates.representation.spherical.PhysicsSphericalDifferential",
"astropy.coordinates.representation.cylindrical.CylindricalDifferential",

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 way to avoid changing the path? All the classes are imported to astropy.coordinates.representation.

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.

In cosmology I use a mapping to map from the fully-qualified path to a preferred path, e.g. astropy.coordinates.representation.cartesian.CartesianRepresentation to astropy.coordinates.CartesianRepresentation

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

You already opened #13689 for that purpose - where you made the suggestion of using entrypoints, which seems just right for this.

@mhvk

mhvk commented May 8, 2023

Copy link
Copy Markdown
Contributor Author

@nstarman - I think all your points are good ones - but also feel best for this PR is to make only the most minimal changes...

@nstarman nstarman left a comment

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.

LGTM. Thanks @mhvk !

@mhvk

mhvk commented May 10, 2023

Copy link
Copy Markdown
Contributor Author

OK, will merge now!

@mhvk
mhvk merged commit eadcd73 into astropy:main May 10, 2023
@mhvk
mhvk deleted the representations-as-module branch May 10, 2023 15:21
@pllim

pllim commented May 10, 2023

Copy link
Copy Markdown
Member

+3,728 −3,641 😱

@mhvk

mhvk commented May 10, 2023

Copy link
Copy Markdown
Contributor Author

Of course, mostly exactly the same lines. And a file with 3600 lines of code really is overly long!

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