TYP: simplify type annotation int | float to float#17392
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.
|
|
👋 Thank you for your draft pull request! Do you know that you can use |
int | float to float
There was a problem hiding this comment.
This pull request should not have been merged in this state, but because of how little time this was open I didn't have a chance to comment before merging. I have already opened a pull request to undo the changes to units.
| a: ArrayLike, | ||
| bins: int | ||
| | list[int | float] | ||
| | list[float] |
There was a problem hiding this comment.
list is a mutable collection, so it is invariant, not covariant (see PEP 483 (The Theory of Type Hints)). This means that although int is a subtype of float, list[int] is not a subtype of list[float]. Using a covariant type such as Sequence[float] would be better, but it might be even better to look up annotations from np.histogram_bin_edges(). The same goes for histogram() too.
Description
Noticed this while working on #17391
follow up to #16562
Unfortunately I can't remember the source for this piece of advice (maybe that's actually specific to mypy), but type checkers treat
intas a subtype offloat, so the unionint | floatcan be simplified tofloatin annotations.