Skip to content

Keep nested parens around lambda/ternary comprehension iterables (preview)#5200

Open
sarathfrancis90 wants to merge 1 commit into
psf:mainfrom
sarathfrancis90:keep-nested-parens-lambda-comprehension
Open

Keep nested parens around lambda/ternary comprehension iterables (preview)#5200
sarathfrancis90 wants to merge 1 commit into
psf:mainfrom
sarathfrancis90:keep-nested-parens-lambda-comprehension

Conversation

@sarathfrancis90

Copy link
Copy Markdown
Contributor

While poking at wrap_comprehension_in after #5176, I hit a case it doesn't cover. #5176 keeps the parens around a lambda or ternary used as a comprehension's iterable so the trailing for/if clauses aren't absorbed into it. But when those parens are redundantly nested, e.g.

[x for x in ((lambda: 0))]

maybe_make_parens_invisible_in_atom recurses and strips both pairs, leaving [x for x in lambda: 0] — invalid code, which crashes with an ASTSafetyError at the default line length. Same for a nested ternary and for generator/dict/set comprehensions, and at deeper nesting.

The guard only inspected the atom's direct child, so a paren-wrapped atom slipped past it. I made it look through the redundant nested parens before deciding whether the iterable is a lambda/ternary, so at least one pair is kept (matching what the stable style does with such input).

Added the nested lambda/ternary cases to the existing preview_wrap_comprehension_in fixture — they crashed before and pass now. The full test suite, mypy, flake8 and the self-format check are green.

…view)

`wrap_comprehension_in` keeps the parentheses around a lambda or conditional
expression used as a comprehension's iterable, otherwise the trailing
`for`/`if` clauses get absorbed into it and the result is invalid. psf#5176
handled the single-paren case, but `maybe_make_parens_invisible_in_atom`
recurses through the atom, so a redundantly nested pair like
`[x for x in ((lambda: 0))]` still had *both* layers stripped, producing
`[x for x in lambda: 0]` and crashing with an ASTSafetyError.

Look through redundant nested parens before deciding whether the atom wraps a
lambda or ternary, so at least one pair is kept.
@sarathfrancis90 sarathfrancis90 force-pushed the keep-nested-parens-lambda-comprehension branch from e592afd to 00e1441 Compare June 26, 2026 07:03
@github-actions

Copy link
Copy Markdown
Contributor

diff-shades results comparing this PR (00e1441) to main (8140f6e):

--preview style: no changes

--stable style: no changes


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

@cobaltt7

cobaltt7 commented Jul 3, 2026

Copy link
Copy Markdown
Collaborator

Thanks for this fix! Would it be non-trivial to continue stripping the outer parentheses, only leaving one pair always? If that'd be a lot of extra work, I'd be fine deferring that and merging the PR as-is, but I'd appreciate that change if possible.

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