Skip to content

Remove redundant generator parentheses - #5304

Merged
cobaltt7 merged 3 commits into
psf:mainfrom
Boulea7:fix/2943-generator-parentheses
Aug 17, 2026
Merged

Remove redundant generator parentheses#5304
cobaltt7 merged 3 commits into
psf:mainfrom
Boulea7:fix/2943-generator-parentheses

Conversation

@Boulea7

@Boulea7 Boulea7 commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Description

Closes #2943.

Under --preview, this removes redundant extra parentheses around generator expressions, including standalone expressions, tuple/list/dict values, nested calls, and call arguments where Python still requires one visible pair.

Checklist - did you ...

  • Implement any code style changes under the --preview style, following the stability policy?
  • Add an entry in CHANGES.md if necessary?
  • Add / update tests if necessary?
  • Add new / update outdated documentation?

Tests

  • .venv/bin/python -m pytest -n 4 -q (551 passed, 2 skipped, 8 subtests passed)
  • .venv/bin/python -m pytest tests/test_format.py -q (233 passed)
  • .venv/bin/pre-commit run --all-files
  • .venv/bin/sphinx-build -a -b html -W docs/ docs/_build/
  • Direct and extra-parenthesized generator cases before attribute, subscript, and call trailers

@cobaltt7

Copy link
Copy Markdown
Collaborator

Thanks!

Would it be simple to extend this to all structures? Here's some examples I found:

((item.is_valid() for item in items))  # alone at top level
(((item.is_valid() for item in items)),)  # tuple
[((item.is_valid() for item in items)), ""]  # array (it's already handled when it's the only element)
{"foo": ((item.is_valid() for item in items))}  # dictionary value

@github-actions

github-actions Bot commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

diff-shades results comparing this PR (53f837a) to main (74371e2):

--preview style (View full diff):
╭─────────────────────── Summary ────────────────────────╮
│ 1 projects & 2 files changed / 14 changes [+6/-8]      │
│                                                        │
│ ... out of 3 036 322 lines, 14 116 files & 22 projects │
╰────────────────────────────────────────────────────────╯

Differences found.

--stable style: no changes


What is this? | Workflow run | diff-shades documentation

@Boulea7

Boulea7 commented Aug 13, 2026

Copy link
Copy Markdown
Contributor Author

Yes, that fit cleanly. I expanded this to the structures you listed and added regression cases for top-level expressions, tuple/list/dict values, nested calls, keyword/starred call args, and multi-arg calls where one pair is still required.

Latest checks:

  • .venv/bin/python -m pytest tests/test_format.py -q
  • in-memory AST/idempotence checks for nested, walrus, async, attribute/subscript, starred, keyword, and multi-arg cases

@Boulea7
Boulea7 force-pushed the fix/2943-generator-parentheses branch from 5206b1e to b216ddd Compare August 13, 2026 03:12
@cobaltt7
cobaltt7 merged commit bd622ed into psf:main Aug 17, 2026
57 checks passed
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.

Remove redundant parentheses from generator expression inside function call

2 participants