Repository navigation
fix: ruff c408 in astropy/units - #15664
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.
|
|
Hi, @paverett One of the purposes of #14818 is to suggest very simple tasks to new contributors so that they can familiarize themselves with the basic workflow and tools that we use to develop |
I understand, sorry about that @eerovaher. Yeah I can take a look at PT012! I looked and didn't find an issue regarding PT012. Should I create one or did I miss one that has already been created? |
mhvk
left a comment
There was a problem hiding this comment.
Looking at this, I actually rather dislike the changes, in particular for the usage of dict - when used to set kwargs, I find it rather nice that it looks like passing on arguments to a function call.
So, I'd suggest to keep ignoring this particular ruff rule, and only change the cases where it is an empty tuple, list, or dict (where not parametrized). I marked the cases I think are fine to change.
| neginf = x._to_own_unit(neginf) | ||
| return ( | ||
| (x.view(np.ndarray),), | ||
| dict(copy=True, nan=nan, posinf=posinf, neginf=neginf), |
There was a problem hiding this comment.
I do not like this: Here, kwargs is set and I find it much clearer if it looks like arguments to a function.
There was a problem hiding this comment.
This is a helper function for a numpy operation so speed is very important.
Given that, this is a good improvement.
There was a problem hiding this comment.
The speed advantage is completely negligible compared to the overhead of even this function itself. I made a conscious decision to write it as it is here, and I don't think this change is good.
But if you want to speed it up further and keep the intent clear, I'm fine with changing the code to
return (x.view(np.ndarray), copy, nan, posinf, neginf), {}, x.unit, None
(I guess that would continue the now common practice to rewrite code just to avoid black formatting...)
There was a problem hiding this comment.
I wonder what the speed of constructing a NamedTuple is.
| usemask=usemask, | ||
| asrecarray=asrecarray, | ||
| ), | ||
| { |
There was a problem hiding this comment.
Same, please exclude this one.
There was a problem hiding this comment.
This is a helper function for a numpy operation so speed is very important.
Given that, this is a good improvement.
There was a problem hiding this comment.
As above. Can "solve" the same way too if we really want to follow ruff gospel.
| self.data = equiv_list | ||
| self.name = [name] | ||
| self.kwargs = [kwargs] if kwargs is not None else [dict()] | ||
| self.kwargs = [kwargs] if kwargs is not None else [{}] |
|
|
||
| except (TypeError, ValueError, AttributeError) as e: | ||
| out_normalized = kwargs.get("out", tuple()) | ||
| out_normalized = kwargs.get("out", ()) |
| assert e1.name == ["pixel_scale", "temperature_energy"] | ||
| assert isinstance(e1.kwargs, list) | ||
| assert e1.kwargs == [{"pixscale": 10 * u.arcsec / u.pix}, dict()] | ||
| assert e1.kwargs == [{"pixscale": 10 * u.arcsec / u.pix}, {}] |
|
|
||
|
|
||
| @pytest.mark.parametrize("multiplicand", [list(), 42, 0, -1]) | ||
| @pytest.mark.parametrize("multiplicand", [[], 42, 0, -1]) |
|
@mhvk, there are 2 good reasons why using the built-in dict constructor
In sum, |
| usemask=usemask, | ||
| asrecarray=asrecarray, | ||
| ), | ||
| { |
There was a problem hiding this comment.
This is a helper function for a numpy operation so speed is very important.
Given that, this is a good improvement.
|
|
||
|
|
||
| @pytest.mark.parametrize("multiplicand", [list(), 42, 0, -1]) | ||
| @pytest.mark.parametrize("multiplicand", [[], 42, 0, -1]) |
| @pytest.mark.parametrize( | ||
| "kwargs", | ||
| ( | ||
| dict(return_index=True, return_inverse=True), |
There was a problem hiding this comment.
All these test cases are very specifically written this way to make them look like arguments to functions. I really see no reason to decrease readability because of some ruff "rule".
|
When I added C408 to the list of suggested rules I didn't expect it to be controversial, and it has been applied in quite a few sub-packages without provoking discussion. But if the sub-package maintainers don't want the rule to be enforced in all cases then for now it's best to just apply it where they are happy with it. @paverett asked:
I don't see any value in opening issues for that rule. You can open pull requests without having to refer to any specific issue. |
|
@mhvk, would you be interested in turning on Ruff's |
Yes, that would make sense, so that means for |
|
I'm fine with that. We can return to the question of dicts later / never. Some refactoring might make the question moot. |
8b7c3af to
f390995
Compare
mhvk
left a comment
There was a problem hiding this comment.
Sorry to be a bit slow - this one is now all OK. Thanks!
|
Failure unrelated, so merging. Thanks again! |


Description
This pull request is to address violations to Ruff's rule C408, which is listed explicitly in issue #14818. Only the units sub module is addressed in this PR.
Solves part of #14818.