-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Make several argument defaults immutable (and annotate them) #17496
base: main
Are you sure you want to change the base?
Conversation
Mutable function argument defaults can be modified persistently (see Ruff rule B006). In several functions mutable default empty sets or lists can easily be replaced with immutable empty tuples, which makes it obvious that these defaults are not modified.
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.
This looks good to me, with one request for adding a comment (for non-typing gurus like me), and a suggestion for a tightening.
Probably best for @nstarman to have the final say.
@@ -2262,13 +2262,34 @@ class CompositeUnit(UnitBase): | |||
|
|||
_decomposed_cache: CompositeUnit | None = None | |||
|
|||
@overload |
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.
Maybe add a comment along the lines of
# The typing overloads here refect that with `_error_check=False`,
# we can count on the scale and power being sanitized already.
p.s. Just to be clear, I'm fine with the change to empty tuples; it works fine here. |
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 agree about adding the comment. Otherwise LGTM! Nice to get rid of some mutable defaults.
I remember looking at the full set of B006 violations in astropy — it's VERY large.
Description
The first commit adds annotations to a few functions in
units
, all of which have a parameter with the typeCollection[UnitBase]
1. In several functions with such a parameter its default value is an emptyset
or an emptylist
, both of which are mutable. In Python it is possible to permanently modify mutable function parameter defaults, which can cause bugs (see also Ruff rule B006 (mutable-argument-default)). It is better to use an immutable default such as an emptytuple
because then it is obvious just from reading the function signature that calling the function does not modify the default argument.Usually I prefer to avoid changing code in type annotation pull requests, but in this case the annotations make it obvious that the sets and lists can be replaced with tuples without changing code behavior, so adding the annotations and changing the code are conceptually related.
Footnotes
I find it somewhat difficult to understand why the type is
Collection[UnitBase]
instead ofCollection[NamedUnit]
, but there is a test that asserts thatNamedUnit
is too restrictive: https://github.com/astropy/astropy/blob/24d3dd96015839ecf87899b02d403a9002bd3ef6/astropy/units/tests/test_units.py#L749-L755 ↩