Skip to content

Annotate CoordinateTransform and its subclasses - #18739

Draft
eerovaher wants to merge 2 commits into
astropy:mainfrom
eerovaher:annotate-CoordinateTransform
Draft

eerovaher wants to merge 2 commits into
astropy:mainfrom
eerovaher:annotate-CoordinateTransform

Conversation

@eerovaher

Copy link
Copy Markdown
Member

Description

The first commit adds initial type annotations to CoordinateTransform and its subclasses. In many cases I rewrote type information from docstrings as annotations. If there are mistakes in the docstrings then the annotations will contain the same mistakes. I did check some of the types at runtime by adding temporary isinstance() checks to the code and running the coordinates tests, but this might not catch mistakes if test coverage is incomplete. I also used

mypy --follow-imports silent astropy/coordinates/transformations/

to ensure the new annotations are self-consistent, but that ignores unannotated code, so there is still the possibility that adding more annotations elsewhere in coordinates will reveal typing problems here.

Mypy did reveal that a code block in FunctionTransform is a no-op. The second commit here removes the block in question because otherwise Mypy would keep complaining about it.

I am not annotating TransformGraph yet because this pull request is large enough as it is and also because I don't want to create merge conflicts with #18695.

  • By checking this box, the PR author has requested that maintainers do NOT use the "Squash and Merge" button. Maintainers should respect this when possible; however, the final decision is at the discretion of the maintainer that merges the PR.

`CoordinateTransform` and its subclasses now have type annotations.
The code inside the deleted `with suppress(TypeError):` block was
guaranteed to raise a `TypeError` because it tried to check the length
of a generator.
@eerovaher eerovaher added this to the v7.2.0 milestone Oct 16, 2025
@eerovaher
eerovaher requested a review from mhvk October 16, 2025 21:35
@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?
  • 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.

Comment on lines -60 to -67
with suppress(TypeError):
sig = signature(func)
kinds = [x.kind for x in sig.parameters.values()]
if (
len(x for x in kinds if x == sig.POSITIONAL_ONLY) != 2
and sig.VAR_POSITIONAL not in kinds
):
raise ValueError("provided function does not accept two arguments")

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.

Trying to check the length of a generator raises a TypeError, which means this entire block is a no-op. Replacing the generator expression with a list comprehension would solve that error, but then trying to access sig.POSITIONAL_ONLY and sig.VAR_POSITIONAL would raise an AttributeError.

@mhvk

mhvk commented Oct 17, 2025

Copy link
Copy Markdown
Contributor

I'm very busy teaching until December, and will not be able to review as many PRs as usual; I hope it will be possible to pass this one to someone else, ideally someone with more interest/experience in typing.

@nstarman
nstarman self-requested a review October 17, 2025 16:44
@eerovaher eerovaher modified the milestones: v7.2.0, v8.0.0 Oct 29, 2025
@eerovaher
eerovaher marked this pull request as draft October 29, 2025 15:27
@eerovaher

Copy link
Copy Markdown
Member Author

At this point it's quite clear that this isn't going to get merged to v7.2. I do not want this merged right after the feature freeze. The annotations can be made much more informative with PEP 695 syntax, which we can start using when we drop support for Python 3.11.

@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.

Largely LGTM. One comment about imports in TYPE_CHECKING blocks, which we should also address later in other parts of the code.

from astropy.coordinates.typing import Matrix3x3

if TYPE_CHECKING:
from astropy.coordinates.baseframe import BaseCoordinateFrame

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.

Based on eg https://github.com/xarray/xarray/issues/9581
We should probably be moving towards full path forward annotations, until Python 3.14 is our minimum version.

import astropy.coordinates

@github-actions github-actions Bot added the Close? Tell stale bot that this issue/PR is stale label Mar 16, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Hi humans 👋 - this pull request hasn't had any new commits for approximately 4 months. I plan to close this in 30 days if the pull request doesn't have any new commits by then.

In lieu of a stalled pull request, please consider closing this and open an issue instead if a reminder is needed to revisit in the future. Maintainers may also choose to add keep-open label to keep this PR open but it is discouraged unless absolutely necessary.

If this PR still needs to be reviewed, as an author, you can rebase it to reset the clock.

If you believe I commented on this pull request incorrectly, please report this here.

@nstarman nstarman added keep-open and removed Close? Tell stale bot that this issue/PR is stale labels Mar 19, 2026
@astrofrog astrofrog modified the milestones: v8.0.0, v8.1.0 May 5, 2026
@pllim pllim removed the keep-open label Oct 14, 2026
@github-actions github-actions Bot added the Close? Tell stale bot that this issue/PR is stale label Oct 15, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Hi humans 👋 - this pull request hasn't had any new commits for approximately a year. I plan to close this in a month if the pull request doesn't have any new commits by then.

In lieu of a stalled pull request, please consider closing this and open an issue instead if a reminder is needed to revisit in the future. Maintainers may also choose to add keep-open label to keep this PR open but it is discouraged unless absolutely necessary.

If this PR still needs to be reviewed, as an author, you can rebase it to reset the clock.

If you believe I commented on this pull request incorrectly, please report this here.

This branch has not been deployed

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

Labels

Close? Tell stale bot that this issue/PR is stale coordinates no-changelog-entry-needed typing related to type annotations

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants