Skip to content
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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

DimitriPapadopoulos
Copy link
Contributor

I have not enforced:

  • C400 Unnecessary generator (rewrite as a list comprehension)
  • C401 Unnecessary generator (rewrite as a set comprehension)
  • C403 Unnecessary list comprehension (rewrite as a set comprehension)
  • C405 Unnecessary list literal (rewrite as a set literal)
  • C408 C408 Unnecessary 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.

@@ -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")
Copy link
Collaborator

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?

Copy link
Contributor Author

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}, as dict.fromkeys is more readable and efficient.

Copy link
Collaborator

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants