-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Enforce ruff/flake8-comprehensions rules (C4) #9724
base: main
Are you sure you want to change the base?
Conversation
@@ -1167,7 +1167,7 @@ def _validate_and_autodetect_region(self, ds: Dataset) -> Dataset: | |||
region = self._write_region | |||
|
|||
if region == "auto": | |||
region = {dim: "auto" for dim in ds.dims} | |||
region = dict.fromkeys(ds.dims, "auto") |
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.
Why is this form better?
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.
Good question. I expect that if the rule exists, someone has measured run times and found this form more efficient. However, this has proven false at least once for UP038 (see astral-sh/ruff#7871).
From the C420 rule documentation:
Prefer
dict.fromkeys(iterable)
over{value: None for value in iterable}
, asdict.fromkeys
is more readable and efficient.
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 guess I'm familiar with the existing one, but support doing what is standard.
To the extent it's not yet a standard (that issue suggests it might not be yet), I'd vote with a low weight to push this off...
C409 Unnecessary `tuple` literal passed to `tuple()` (remove the outer call to `tuple()`) C409 Unnecessary list comprehension passed to `tuple()` (rewrite as a generator)
C414 Unnecessary `tuple` call within `sorted()`
C416 Unnecessary `dict` comprehension (rewrite using `dict()`) C416 Unnecessary `list` comprehension (rewrite using `list()`)
C417 Unnecessary `map` usage (rewrite using a generator expression)
C419 Unnecessary list comprehension
C420 Unnecessary dict comprehension for iterable; use `dict.fromkeys` instead
bdc5191
to
b7dcb26
Compare
I have not enforced:
list
comprehension)set
comprehension)list
comprehension (rewrite as aset
comprehension)list
literal (rewrite as aset
literal)dict
call (rewrite as a literal)The reason is that maintainers are often less fond of these rules. I am happy to apply them if there's interest.