Skip to content

Enforce ruff/flake8-comprehensions rules (C4) - #4785

Open
DimitriPapadopoulos wants to merge 9 commits into
pypa:mainfrom
DimitriPapadopoulos:C4
Open

DimitriPapadopoulos wants to merge 9 commits into
pypa:mainfrom
DimitriPapadopoulos:C4

Conversation

@DimitriPapadopoulos

@DimitriPapadopoulos DimitriPapadopoulos commented Jan 1, 2025 •

Copy link
Copy Markdown
Contributor

Summary of changes

Pull Request Checklist

@mergify

mergify Bot commented Jan 1, 2025

Copy link
Copy Markdown
Contributor

⚠️ The sha of the head commit of this PR conflicts with #4386. Mergify cannot evaluate rules on this PR. ⚠️

1 similar comment
@mergify

mergify Bot commented Jan 5, 2025

Copy link
Copy Markdown
Contributor

⚠️ The sha of the head commit of this PR conflicts with #4386. Mergify cannot evaluate rules on this PR. ⚠️

@Avasam

Avasam commented Mar 11, 2025 •

Copy link
Copy Markdown
Contributor

I think you'll want to enable allow-dict-calls-with-keyword-arguments
I'd have to find it back, but I believe that's a stylistic decision @jaraco has previously weighed in.

@DimitriPapadopoulos
DimitriPapadopoulos force-pushed the C4 branch 3 times, most recently from bb995d6 to 5e77363 Compare March 11, 2025 20:26
@DimitriPapadopoulos

DimitriPapadopoulos commented Mar 12, 2025 •

Copy link
Copy Markdown
Contributor Author

After adding allow-dict-calls-with-keyword-arguments, most occurrences of C408 have disappeared. A few instances of dict() → {} have been left behind. Should I discard them as well and disable C408?

@Avasam

Avasam commented Mar 12, 2025

Copy link
Copy Markdown
Contributor

I personally think those make sense, it's mainly changing generators of tuples into dict comprehensions.

@DimitriPapadopoulos
DimitriPapadopoulos force-pushed the C4 branch 2 times, most recently from ef18ee2 to e525809 Compare April 14, 2025 08:56
@DimitriPapadopoulos
DimitriPapadopoulos force-pushed the C4 branch 2 times, most recently from 412819f to 36fa684 Compare May 23, 2025 08:13
@DimitriPapadopoulos

Copy link
Copy Markdown
Contributor Author

Rebased.

@DimitriPapadopoulos

Copy link
Copy Markdown
Contributor Author

@Avasam

Avasam commented May 23, 2025

Copy link
Copy Markdown
Contributor

I'm not sure your rebase is correct. I see refurb related changes and the change dict(foo=bar) --> {"foo": bar} which was previously mentioned as unwanted.

PyPy tests have also been flaky: #4940

@DimitriPapadopoulos

Copy link
Copy Markdown
Contributor Author

I had to rerun C401 and C408 because rebasing was impossible. I guess I somehow got the options wrong. Will try again.

@DimitriPapadopoulos

DimitriPapadopoulos commented May 23, 2025 •

Copy link
Copy Markdown
Contributor Author

Reapplied C408 after making sure allow-dict-calls-with-keyword-arguments is true.

C400 Unnecessary generator (rewrite as a `list` comprehension)
C401 Unnecessary generator (rewrite as a `set` comprehension)
C402 Unnecessary generator (rewrite as a `dict` comprehension)
C408 Unnecessary `dict()` call (rewrite as a literal)
C405 Unnecessary `list` literal (rewrite as a `set` literal)
C413 Unnecessary `list` call around `sorted()`
C414 Unnecessary `list` call within `sorted()`
C417 Unnecessary `map` usage (rewrite using a generator expression)
C420 Unnecessary dict comprehension for iterable; use `dict.fromkeys` instead
Avasam referenced this pull request in pypa/distutils Aug 3, 2026
Updates style to match Ruff 0.16 rules.
Avasam referenced this pull request Aug 3, 2026
Retain the dict(a=1) call style as an elegant alternative to
string-keyed literals rather than have Ruff 0.16's new default rewrite
it. Ref jaraco/skeleton#210.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
jaraco added a commit to jaraco/skeleton that referenced this pull request Aug 6, 2026
Switch from ignoring C408 (unnecessary-collection-call) entirely to
enabling flake8-comprehensions allow-dict-calls-with-keyword-arguments,
which retains the preferred dict(a=1) keyword-call style while C408 still
flags dict() and dict([...]). Suggested by @Avasam; matches the
configuration in pypa/setuptools#4785. Ref #210.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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