Skip to content

Define __all__ for units#15862

Merged
mhvk merged 3 commits into
astropy:mainfrom
nstarman:units-import-order
Jan 11, 2024
Merged

Define __all__ for units#15862
mhvk merged 3 commits into
astropy:mainfrom
nstarman:units-import-order

Conversation

@nstarman
Copy link
Copy Markdown
Member

@nstarman nstarman commented Jan 11, 2024

This PR:

  1. Sorts units.__init__.py.
  2. Expands the arguments to def_unit to add the units to __all__ if it exists.
  3. Adds __all__ to units.

Plz squash.

Signed-off-by: nstarman <[email protected]>
Signed-off-by: nstarman <[email protected]>
@nstarman nstarman added this to the v6.1.0 milestone Jan 11, 2024
@github-actions github-actions Bot added the units label Jan 11, 2024
@github-actions
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 instructions for rebase and squash.
  • 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.

@github-actions
Copy link
Copy Markdown
Contributor

👋 Thank you for your draft pull request! Do you know that you can use [ci skip] or [skip ci] in your commit messages to skip running continuous integration tests until you are ready?

@nstarman nstarman requested a review from mhvk January 11, 2024 06:46
@nstarman nstarman marked this pull request as ready for review January 11, 2024 06:57
Comment thread docs/changes/units/15862.feature.rst Outdated
Copy link
Copy Markdown
Contributor

@mhvk mhvk left a comment

Choose a reason for hiding this comment

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

I like the principle very much (plus the simplification of __init__.py), but the implementation less so. Rather than add something somewhat arbitray to def_unit, I think it makes more sense to define a new function that just looks in the namespace and creates a list of all UnitBase instances. Indeed, it barely needs a function, one can just do at the end of each module,

__all__.extend(n for n, v in _globals().items() if isinstance(v, UnitBase))

(though a new function is fine too, which can return an iterator or list)

A bit separately, but a benefit of __all__ is that we don't have to clean up the namespace any more.

@nstarman nstarman requested review from mhvk and pllim January 11, 2024 20:17
Comment thread astropy/units/cds.py Outdated
Comment thread astropy/units/astrophys.py Outdated
@nstarman nstarman force-pushed the units-import-order branch 2 times, most recently from 5802009 to fc64572 Compare January 11, 2024 20:54
Comment thread astropy/units/__init__.py Outdated
__all__ += photometric.__all__
__all__ += structured.__all__
__all__ += equivalencies.__all__
__all__ += function_units.__all__
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.

Why not write it as e.g.:

__all__ = (
    module1.__all__ +
    module2.__all__ +
    ...
)

rather than do it via a series of updates?

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.

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.

__all__ += submodule.__all__ is my personal preference in this scenario. We could do __all__.extend(submodule.__all__), but I find it less clean.

Copy link
Copy Markdown
Contributor

@mhvk mhvk left a comment

Choose a reason for hiding this comment

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

Looking very nice! Mostly comments about the __init__.py, including what I think are missing ones from function.

Comment thread astropy/units/__init__.py
Comment thread astropy/units/__init__.py Outdated
from .equivalencies import *

from .function import units as function_units
from .function.core import *
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I guess this and the next one could in principle be replaced by from .function import *, though ideally then we also at an __all__ to its __init__.py. Not sure at all why that one doesn't include a from .units import *! Would be more logical.

(I think there may have been problems before when units.function was also typeset in sphinx; but that is no longer the case.)

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.

Done! I added from .units import * in units.function and from .function import * in units.

Comment thread astropy/units/__init__.py
Comment thread astropy/units/__init__.py Outdated
from . import equivalencies
from .equivalencies import *

from .function import units as function_units
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I had to look back through the git logs to understand why this is imported. It became clear the only reason is to mention it in set_enabled_units below, but of course there we might as well just do function.units, which seems much cleaner. Adjust?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Though perhaps one should then make sure to add from . import function - which would seem logical anyway.

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.

Done!

Comment thread astropy/units/__init__.py Outdated
__all__ += photometric.__all__
__all__ += structured.__all__
__all__ += equivalencies.__all__
__all__ += function_units.__all__
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think this is missing function.core.__all__ and function.logarithmic.__all__ -- though I think those two and function_units.__all__ could be replaced by function.__all__ if we bring that up to date too.

Comment thread astropy/units/astrophys.py
Comment thread astropy/units/cds.py Outdated

core.def_unit(["µas"], u.microarcsecond, doc="microsecond of arc", namespace=_ns)
core.def_unit(["mas"], u.milliarcsecond, doc="millisecond of arc", namespace=_ns)
core.def_unit(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ideally, undo these changes (also below).

Comment thread astropy/units/function/units.py Outdated
Signed-off-by: nstarman <[email protected]>
@nstarman
Copy link
Copy Markdown
Member Author

Changelog needed?

Copy link
Copy Markdown
Contributor

@mhvk mhvk left a comment

Choose a reason for hiding this comment

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

Super! This looks all OK, now. I don't see why a changelog entry would be needed, since this should not be visible to the user...

@mhvk mhvk merged commit e3c3638 into astropy:main Jan 11, 2024
@nstarman nstarman deleted the units-import-order branch January 11, 2024 22:56
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.

4 participants