-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Add type annotations to stats
subpackage
#16562
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
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.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Welcome to Astropy 👋 and congratulations on your first pull request! 🎉
A project member will respond to you as soon as possible; in the meantime, please have a look over the Checklist for Contributed Code and make sure you've addressed as many of the questions there as possible.
If you feel that this pull request has not been responded to in a timely manner, please send a message directly to the development mailing list. If the issue is urgent or sensitive in nature (e.g., a security vulnerability) please send an e-mail directly to the private e-mail feedback@astropy.org.
👋 Thank you for your draft pull request! Do you know that you can use |
Awesome. I'm game to review this but I might as well wait until it's undrafted. @jeffjennings CI is all green, is there anything else you're specifically waiting for / need a hand with? |
Thanks Clément! No if it looks ready for review, go for it. |
Awesome. I might give it a look later today if I get stable enough wifi on the train ! |
docs/changes/stats/16562.feature.rst
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have no been adding change log for such changes because they are not visible to users in terms of functionality change (hopefully). Please remove change log and squash it out. Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok I see, it would be nice to update the developer docs to make that clear. E.g. at https://docs.astropy.org/en/latest/development/workflow/development_workflow.html#add-changelog, "Add a changelog fragment briefly describing the change you made..." to "If your change is user-facing, add a changelog fragment briefly describing the change you made...", maybe in #16561
Who performs the squash, me or a reviewer? I assume we wait until a PR is ready to be merged to squash (and include in the squash minor changes addressing reviews)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jeffjennings - I have updated the changelog instructions in #16561 to add For changes that impact user-facing functionality, ...
.
@pllim - I think this change is user-facing. When a user is working with this code, if it has type annotations then their IDE or notebook will magically be more helpful.. So I would say 100% this is user-facing. It also is nice as a developer to get credit for improving astropy with a changelog entry. (As a side-note, I spent at least 16 hours on #16561 and maybe that deserves a changelog entry too so that developers know about it and so I get credit??)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you @taldcroft!
Just to offer my opinion (to you and @pllim), I definitely agree that developers should always get credit for contributions, even those that are internal, and especially when the amount of time and work contributed is large. I think docs contributions (and type annotations) should be a part of this and have changelog entries; the docs are a critical part of the software and are used heavily by both users and developers (e.g. they just taught me the unique steps about contributing to Astropy ahead of my first PR).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change log is not a form of "credit" so I am a little confused. We have script at release time to properly add contributors to list to be credited and that has nothing to do with change logs.
All the other typing PRs do not have change log, so I don't know why this one for stats needs special exception but I'll defer to @eerovaher and @nstarman and @mhvk who have been involved with a lot of typing related PRs lately.
Change log situation for #16561 should be discussed separately there. I have not looked at that diff carefully yet but I understand the argument (also not related to typing changes).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Yes I see your point. Although in cases of retroactively adding type annotations, the person adding the annotations in the call signature lines would be git blamed (which makes sense to me), while the content of the function (and docstring) would in general still be git blamed to the original author (not hiding their contributions). I'd say things like type annotations are real functionality changes, as Tom proposed. Adding them is definitely more manual than auto-formatting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pllim - the word "credit" was maybe not perfect, but I meant that the work being done is noticeable in the release notes for people that check change logs. These things matter for both for perception and for informing the community. I'll open an issue now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For typing in particular, it is a sticky situation because it is tough to do this retroactively and it is not fair if we do not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I mention in #16569, I think this warrants not just a changelog entry, but also a what's new. Several users have asked for astropy to be typed, and it is relevant to them that one subpackage now is, and that we are now on a path to include typing more generally.
I don't think any of the previous typing changes were as user-visible, except perhaps cosmology
- but that can also get a what's new entry if it is completed.
@jeffjennings this is really great! I've left some comments, mostly about further type narrowing. |
Thanks everyone for the review, especially @nstarman! I left review conversations open, but I think they're all addressed (except for squashing). Please resolve any you feel are addressed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good! I noted a few more errant NDArray
and Callable
astropy/stats/spatial.py
Outdated
self, | ||
data: NDArray, | ||
radii: NDArray, | ||
mode: Literal["none", "translation", "ohser", "var-width", "ripley"] = "none", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm. Seems like something Sphinx should have a better fix for. When a type annotation gets very long, as in sigma_clipping.py
it's helpful to condense using aliases.
@jeffjennings, if you don't mind, could you try a private alias, like _ModeOps: TypeAlias = Literal["none", "translation", "ohser", "var-width", "ripley"]
?
Then we'll know whether some consolidation can happen, which might help readability in the return annotations in sigma_clipping.py
docs/changes/stats/16562.feature.rst
Outdated
@@ -0,0 +1 @@ | |||
Adds type annotations to all function (public and private, including class methods) call signatures in ``stats``. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a bit TBD, but this should perhaps be 16562.other.rst
? Though I can also see the case for .feature
... See #16569 (comment) - better wait for discussion before changing it!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd suggest a what's new entry. Maybe something like
|
Thanks Marten! That's a nice idea. Added in ee02387 |
Thank you again to all the reviewers. I've just addressed all outstanding review points. |
Four days later: the wifi was not, in fact, stable |
haha no worries! |
84b0098
to
f80d561
Compare
I've rebased on main to resolve a merge conflict (edits to |
I think this is good to go! @eerovaher or @nstarman if you are happy with this, since you both have been doing a fair amount of typing work, feel free to merge this. |
I really think that it's the sub-package maintainers who should do the final review and merge. I certainly don't want any type annotations to be added to |
Agreed, that's what I was waiting on as well. |
That makes sense of course. @larrybradley, as we talked about on Slack a few weeks ago, I've added type annotations to all of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not an expert on type annotations, so I'm relying on the thorough reviews of @nstarman , @neutrinoceros , @eerovaher, and @adrn. Thanks again, @jeffjennings, for your work on this and for your first contribution to astropy
! 🎉
[ci skip]
I took the liberty to do minor edit to what's new entry, so not to give people false hope just on the title by making it more explicit about stats only. If you are okay with it, I will Squash & Merge. Thanks, all! |
@pllim The "what's new" edit looks fine to me. |
Thanks a lot @larrybradley, I appreciate it! |
Let me reiterate that I trust other reviewers and am available to provide another review if needed but I did not thoroughly review this (yet) ! |
Whoops I missed that this was just merged, so clearly you guys don't need my review here 😅 |
Thankz Clément! |
@eerovaher Yes agreed! Sorry, there was out-of-band discussion with @larrybradley -- I didn't mean to suggest we overrule him here :). |
Thanks @jeffjennings! A great leap forward for typing in Astropy. |
Thank you @nstarman again for reviewing! Especially for a such a big PR. I learned more about typing thanks to your ideas and comments. |
* bayesian_blocks: add type hints * biweight: add type hints * circstats: add type hints * funcs: add type hints * histogram: add type hints * info_theory: add type hints * jackknife: add type hints * sigma_clipping: add type hints * spatial: add type hints * setup_package: add type hints * nitpick-exceptions: add ArrayLike type for sphinx * bayesian_blocks.bayesian_blocks: restrict type Co-authored-by: Adrian Price-Whelan <adrianmpw@gmail.com> * bayesian_blocks.bayesian_blocks: better constrain type hint Co-authored-by: Nathaniel Starkman <nstarman@users.noreply.github.com> * bayesian_blocks.FitnessFunc: better type hint Co-authored-by: Nathaniel Starkman <nstarman@users.noreply.github.com> * bayesian_blocks: better constrain type hints * stats: TODO for future type hinting * biweight: add dtype to NDArray type hints * spatial.RipleysKEstimator: add dtype to NDArray type hints * sigma_clipping.SigmaClip: add masked array dtype * jackknife: add generic DType to NDArray type hints * nitpick-exceptions: add np.ma.MaskedArray * stats: type hint syntax for numpy float * docs: add typing entry to whatsnew * stats.funcs: type hint specify float * stats.spatial: add private TypeAlias * stats.biweight._stat_functions: type hint * stats typing: update changelog entry to 'other' * stats.spatial: add typing TODO * stats: NDArray type hint dtype * whatsnew: refine stats typing entry --------- Co-authored-by: Adrian Price-Whelan <adrianmpw@gmail.com> Co-authored-by: Nathaniel Starkman <nstarman@users.noreply.github.com> Co-authored-by: P. L. Lim <2090236+pllim@users.noreply.github.com>
Description
Adds type annotations to all modules in
stats
. Specifically:*args
or**kwargs**
in call signaturesBeyond this, there are ~10 lines with very minor changes to docstrings (adding a missing parameter or return value, updating the type based on the code in the function).
Because not much of the codebase is typed, there may be some errors in type annotations which are hard to determine now. Local tests and doc build pass.
This is my first Astropy PR; I read the developer docs on how to make a contribution, but I appreciate any advice. Thanks!
Related issues (does not fully address / fix them): #15170
Relevant PRs: astropy/astropy-APEs#94 proposes a typing APE