Skip to content

Cosmo: type annotate the optimization functions#15853

Merged
eerovaher merged 3 commits into
astropy:mainfrom
nstarman:cosmo-typing-optimize
Dec 13, 2024
Merged

Cosmo: type annotate the optimization functions#15853
eerovaher merged 3 commits into
astropy:mainfrom
nstarman:cosmo-typing-optimize

Conversation

@nstarman
Copy link
Copy Markdown
Member

Followup to #15852

Plz squash.

@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

Need Unpack from typing_extensions.

Comment thread astropy/cosmology/funcs/optimize.py Outdated
@nstarman nstarman force-pushed the cosmo-typing-optimize branch from 8e1a839 to c7b3fd6 Compare January 11, 2024 20:30
@nstarman nstarman force-pushed the cosmo-typing-optimize branch from c7b3fd6 to de274d3 Compare January 20, 2024 04:23
@astrofrog astrofrog modified the milestones: v6.1.0, v7.0.0 Apr 4, 2024
@mhvk mhvk added the typing related to type annotations label May 30, 2024
@github-actions github-actions Bot added the Close? Tell stale bot that this issue/PR is stale label Jun 18, 2024
@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 Jun 19, 2024
@nstarman nstarman force-pushed the cosmo-typing-optimize branch from de274d3 to 7969a18 Compare October 9, 2024 20:08
@saimn saimn modified the milestones: v7.0.0, v7.1.0 Oct 26, 2024
@nstarman nstarman force-pushed the cosmo-typing-optimize branch 2 times, most recently from e6e537b to 544c68c Compare December 4, 2024 21:57
@nstarman
Copy link
Copy Markdown
Member Author

nstarman commented Dec 4, 2024

I'm hesitant to add the types ZAtValueKWArgs and CustomMethod to Cosmology's public API.
Private type hints are still useful to IDEs, just not the docs.
@eerovaher WDYK? Is there a balance that can be struck?

@nstarman nstarman requested a review from eerovaher December 5, 2024 21:31
@eerovaher
Copy link
Copy Markdown
Member

In a greenfield project I would insist on documenting all types that are used in the public API, but astropy is not a greenfield project. Here it is better to follow what I've seen referred to as the Scout rule, i.e. our pull requests should leave the code in a better state than it was before. Introducing the new types doesn't make the existing docstrings any worse, but it does make the code friendlier towards IDEs and typing-aware developers.

Comment thread astropy/cosmology/funcs/optimize.py
Comment thread astropy/cosmology/funcs/optimize.py Outdated
Comment thread astropy/cosmology/units.py Outdated
@nstarman nstarman force-pushed the cosmo-typing-optimize branch from 544c68c to b9cfdc7 Compare December 9, 2024 01:35
@nstarman nstarman marked this pull request as ready for review December 9, 2024 01:50
@nstarman nstarman requested a review from a team as a code owner December 9, 2024 01:50
@nstarman
Copy link
Copy Markdown
Member Author

nstarman commented Dec 9, 2024

Test failures unrelated.

@nstarman nstarman requested a review from eerovaher December 9, 2024 01:50
Comment thread astropy/cosmology/funcs/optimize.py Outdated


# TODO: it would be nice to upstream this to scipy and then use it here.
class _CustomMethod(Protocol):
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 is this implemented as a Protocol with a __call__() method instead of something like

_CustomMethod: TypeAlias = Callable[..., OptimizeResult]

And is _CustomMethod the best name for this?

Copy link
Copy Markdown
Member Author

@nstarman nstarman Dec 9, 2024

Choose a reason for hiding this comment

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

Because the structure of _CustomMethod is more specific than that. It has to have a signature like
(fun: Callable[..., Any], args: tuple[Any, ...], **kwargs: Any) -> OptimizeResult.
But I agree about a better name!

@nstarman nstarman force-pushed the cosmo-typing-optimize branch from b9cfdc7 to 011c6c9 Compare December 9, 2024 23:03
@nstarman nstarman requested a review from eerovaher December 9, 2024 23:03
Comment thread astropy/cosmology/units.py Outdated
from typing import Any, Literal
from typing import Literal

from typing_extensions import Unpack
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.

Unpack is used in a few places to unpack **atzkw. That functionality was added to Unpack in Python 3.12 (see PEP 692 – Using TypedDict for more precise **kwargs typing). I'm willing to guess that the reason for importing Unpack from typing_extensions instead of typing is to make that functionality available in Python 3.11. But apparently typing_extensions.Unpack only backports the changes to Unpack.__repr__(), not the ability to unpack **kwargs, which means that the new type annotations are valid for Python 3.12 and later, but not for Python 3.11.

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.

Unpack has been in typing since 3.11.

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 Python 3.11 typing.Unpack can only be used to unpack tuples.

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.

Then does it come through as an Any? My mypy isn't flagging any issues.

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 haven't tested this with Python 3.11.

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.

So what's the step forward? Merge as-is and wait for mypy checking of cosmology?

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.

It is possible that I am misunderstanding the typing_extensions documentation and this is actually not a problem. Someone should type check this with Python 3.11 to find out for sure.

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.

I looked into it more. It doesn't do it. Sigh. I kind of don't want to change it, but should. Just defeats the point of adding _ZAtValueKWArgs

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.

Working around it the gross way.

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.

That's not the prettiest code, but it has no effect at runtime, we only need it until we drop support for Python 3.11 and Ruff will not let us forget about it when we do.

@nstarman nstarman requested a review from eerovaher December 12, 2024 16:13
@eerovaher eerovaher merged commit ca602e3 into astropy:main Dec 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done
Status: Done

Development

Successfully merging this pull request may close these issues.

5 participants