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

Use comprehensions -- ruff check --select=C4 #83

Merged
merged 1 commit into from
Mar 1, 2024

Conversation

cclauss
Copy link
Contributor

@cclauss cclauss commented Feb 29, 2024

https://docs.astral.sh/ruff/rules/#flake8-comprehensions-c4

% ruff --select=C4 --statistics

 37	C408	[*] Unnecessary `dict` call (rewrite as a literal)
 8	C405	[*] Unnecessary `list` literal (rewrite as a `set` literal)
 8	C417	[*] Unnecessary `map` usage (rewrite using a `list` comprehension)
 1	C416	[*] Unnecessary `list` comprehension (rewrite using `list()`)

% ruff --select=C4 --fix --unsafe-fixes

Found 54 errors (54 fixed, 0 remaining).

% ruff rule C408

unnecessary-collection-call (C408)

Derived from the flake8-comprehensions linter.

Fix is always available.

What it does

Checks for unnecessary dict, list or tuple calls that can be
rewritten as empty literals.

Why is this bad?

It's unnecessary to call e.g., dict() as opposed to using an empty
literal ({}). The former is slower because the name dict must be
looked up in the global scope in case it has been rebound.

Examples

dict()
dict(a=1, b=2)
list()
tuple()

Use instead:

{}
{"a": 1, "b": 2}
[]
()

Fix safety

This rule's fix is marked as unsafe, as it may occasionally drop comments
when rewriting the call. In most cases, though, comments will be preserved.

Options

  • lint.flake8-comprehensions.allow-dict-calls-with-keyword-arguments

@cclauss cclauss changed the title Use comprehensions Use comprehensions -- ruff check --select=C4 Feb 29, 2024
@jcjones
Copy link
Collaborator

jcjones commented Mar 1, 2024

I broke this somehow in a merge; I'll have to get it in the morning.

@cclauss
Copy link
Contributor Author

cclauss commented Mar 1, 2024

I broke this somehow in a merge; I'll have to get it in the morning.

😄 I could not sleep so I rebased it. Ready for your review.

@jcjones jcjones merged commit 965110f into letsencrypt:main Mar 1, 2024
2 checks passed
@cclauss cclauss deleted the comprehensions branch March 1, 2024 02:29
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