Skip to content

cosmo: add type annotations to astropy.cosmology.units.py (REVERTED)#15852

Merged
mhvk merged 3 commits into
astropy:mainfrom
nstarman:cosmo-typing-units
May 21, 2024
Merged

cosmo: add type annotations to astropy.cosmology.units.py (REVERTED)#15852
mhvk merged 3 commits into
astropy:mainfrom
nstarman:cosmo-typing-units

Conversation

@nstarman
Copy link
Copy Markdown
Member

@nstarman nstarman commented Jan 10, 2024

When I add type hints to cosmology.funcs.optimize.py then I'll be able to add better hints to the atzkw arguments, but for now these generic annotations are good.

Plz squash and cleanup merge message.

@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
Copy link
Copy Markdown
Member Author

I'm debating whether to define a TypeAlias for kind: Literal["comoving", "lookback", "luminosity"].
Maybe

DistanceKind: TypeAlias = Literal["comoving", "lookback", "luminosity"]

@nstarman nstarman marked this pull request as ready for review January 10, 2024 20:49
@nstarman nstarman requested a review from a team as a code owner January 10, 2024 20:49
@nstarman nstarman requested a review from eerovaher January 10, 2024 20:51
@pllim pllim added this to the v6.1.0 milestone Jan 10, 2024
@nstarman nstarman changed the title Add type annotations to astropy/cosmology/units.py Add type annotations to astropy.cosmology.units.py Jan 10, 2024
@nstarman nstarman changed the title Add type annotations to astropy.cosmology.units.py cosmo: add type annotations to astropy.cosmology.units.py Jan 10, 2024
@nstarman
Copy link
Copy Markdown
Member Author

nstarman commented Jan 11, 2024

Sigh. RTD errors are the most annoying to resolve. Who the heck knows how it finds its reference targets.

As a note, I'm pretty sure that if we adopt astropy/astropy-APEs#85 then we won't have this problem since I think RTD correctly recognizes _-prefixed modules as private and won't then find duplicates, as is happening here. Not to harp, but standard problems are solved by standard solutions!

WARNING: more than one target found for cross-reference 'Equivalency': astropy.units.Equivalency, astropy.units.equivalencies.Equivalency

@namurphy
Copy link
Copy Markdown
Contributor

I'm excited to see this PR!

Sigh. RTD errors are the most annoying to resolve. Who the heck knows how it finds it's reference targets.

Sigh, indeed. We added a fairly long section on how to deal with Reference target not found RTD errors to PlasmaPy's documentation guide.

@nstarman
Copy link
Copy Markdown
Member Author

Thanks @namurphy. That's a useful guide! Unfortunately I don't think it covers this edge case (see sphinx-doc/sphinx#10400)

@nstarman
Copy link
Copy Markdown
Member Author

937f0af shows that removing Equivalency from astropy.units.equivalencies.__all__ fixes the issue since it's no longer doubly defined.
08d3e40 shows that we can have __all__ defined if the module is properly private.
This problem might arise here and hasn't come up elsewhere because units.__init__.py doesn't have an __all__ defined. Adding __all__ might fix this problem due to sphinx treating it with higher precedence or something. I'll investigate.

@nstarman
Copy link
Copy Markdown
Member Author

nstarman commented Jan 11, 2024

Hopefully #15862 will fix this. Sphinx uses the PEP8 definitions of public API, which is how my type annotation tripped it up here. Sphinx also has a precedence criteria, so hopefully defining units.__all__ should work.

Update: unfortunately #15862 is not a fix. The precedence for units.__all__ above units.equivalencies.__all__ didn't work.

@nstarman nstarman marked this pull request as draft January 11, 2024 07:19
@nstarman
Copy link
Copy Markdown
Member Author

Ping @mhvk. I'm trying to resolve an issue with RTD. Sphinx is trying to resolve the type annotations and finds Equivalency at both astropy.units.Equivalency and astropy.units.equivalencies.Equivalency. Sphinx uses PEP8-style rules for determining which to point to. In experiments (08d3e40) I renamed the equivalences module to _equivalencies and this sorted out the problem. I'm looking for a solution we can use short of renaming modules.

@mhvk
Copy link
Copy Markdown
Contributor

mhvk commented Jan 12, 2024

One issue we had before with sphinx is that we displayed the definitions of things more than two times (e.g., something like units.function.core, units.function, and units.function). At the time, the sphinx people told us that they can deal with having both a place where things are defined and one where they are exposed, but not also an API link to something in between (if my memory serves, this is why units.function is not shown any more).

I suspect you are running into something similar. To test this, could you try removing astro.units.equivalencies from astropy/docs/units/ref_api.rst and see if that solves things?

I think even if it does not, since sphinx can deal with our setup for the regular documentation, this looks to me like a sphinx bug that they would likely want to know about!

That said, there is something somewhat illogical more generally about showing API/contents for what nominally are private modules. If we could adjust automodapi to insert some headings in between groupings, that might in fact be much nicer (they are actually ordered like that in the current docs, see https://docs.astropy.org/en/latest/units/ref_api.html#module-astropy.units -- but note that this is actually a different order from stable, where it is alphabetical... separate issue -- #15877).

@nstarman
Copy link
Copy Markdown
Member Author

@mhvk. That appears to work! Looks like we also need to remove astropy.units.quantity

@nstarman nstarman marked this pull request as ready for review January 12, 2024 23:55
@mhvk
Copy link
Copy Markdown
Contributor

mhvk commented Jan 13, 2024

OK, great, that at least narrows it down. It sounds like something to raise over at sphinx (independently of whether we change our docs around ourselves).

@nstarman
Copy link
Copy Markdown
Member Author

Given the addition of units.all, I don't think we're losing anything except logical groupings from the docs by removing these references. Should we merge this in and work on the docs in a separate PR?

@mhvk
Copy link
Copy Markdown
Contributor

mhvk commented Jan 13, 2024

That seems the wrong order - let's separate out the removals to a new PR that also gets the docs OK again first.

@nstarman
Copy link
Copy Markdown
Member Author

See #15891

Comment thread docs/units/ref_api.rst
@nstarman nstarman force-pushed the cosmo-typing-units branch from c9497b3 to 4d26aa7 Compare April 28, 2024 01:29
@nstarman nstarman mentioned this pull request Apr 28, 2024
1 task
@nstarman nstarman requested a review from mhvk April 28, 2024 03:21
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.

Beyond those :noindex: additions, everything looks good...

@nstarman
Copy link
Copy Markdown
Member Author

Thanks @mhvk, @eerovaher, @eerovaher for the reviews! These RTD errors can be hard to diagnose and fix.

@nstarman nstarman force-pushed the cosmo-typing-units branch from 4d26aa7 to bfe48a8 Compare May 19, 2024 20:30
@nstarman nstarman force-pushed the cosmo-typing-units branch from bfe48a8 to 35da854 Compare May 19, 2024 20:30
Copy link
Copy Markdown
Member

@eerovaher eerovaher left a comment

Choose a reason for hiding this comment

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

I think this is ready to be squashed and merged, but I'd prefer this to have more than one approval, so I'm not merging immediately.

Copy link
Copy Markdown
Contributor

@neutrinoceros neutrinoceros left a comment

Choose a reason for hiding this comment

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

I'm this close to approving.

def redshift_distance(cosmology=None, kind="comoving", **atzkw):
def redshift_distance(
cosmology: Cosmology | str | None = None,
kind: Literal["comoving", "lookback", "luminosity"] = "comoving",
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.

tangent, but I notice that the docstring doesn't say which option is the default, maybe now is as good a time as any to fix this.

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.

The first value listed in braces is the default and it does not need to be highlighted more than that: https://numpydoc.readthedocs.io/en/latest/format.html#parameters

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.

Today I learned. Thanks !

Comment thread docs/units/ref_api.rst Outdated
Co-authored-by: Clément Robert <[email protected]>
@nstarman nstarman requested a review from neutrinoceros May 21, 2024 16:14
@nstarman nstarman enabled auto-merge (squash) May 21, 2024 16:50
@nstarman nstarman disabled auto-merge May 21, 2024 16:50
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.

Yes, happy with this!

Comment thread docs/units/ref_api.rst
@mhvk mhvk merged commit 642fbc9 into astropy:main May 21, 2024
@mhvk
Copy link
Copy Markdown
Contributor

mhvk commented May 21, 2024

Thanks, @nstarman!

@nstarman nstarman deleted the cosmo-typing-units branch May 21, 2024 22:17
@pllim
Copy link
Copy Markdown
Member

pllim commented May 21, 2024

Did this break RTD?

WARNING: py:class reference target not found: astropy.units.quantity.Quantity [ref.class]

build finished with problems, 1563 warnings.

@mhvk
Copy link
Copy Markdown
Contributor

mhvk commented May 21, 2024

How weird!!

@nstarman
Copy link
Copy Markdown
Member Author

I think this might just need another addition to nitpick-exceptions. We've had to clobber every problem with Sphinx by just ignoring it using this file.

@nstarman nstarman mentioned this pull request May 22, 2024
@pllim pllim changed the title cosmo: add type annotations to astropy.cosmology.units.py cosmo: add type annotations to astropy.cosmology.units.py (REVERTED) May 22, 2024
@eerovaher eerovaher added the typing related to type annotations label May 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: Done
Status: Done

Development

Successfully merging this pull request may close these issues.

7 participants