Keep parens around lambda iterables in comprehensions (preview)#5176
Merged
cobaltt7 merged 1 commit intoJun 15, 2026
Merged
Conversation
wrap_comprehension_in made the visible parens around the iterable after `in` invisible whenever the inner expression was a low-precedence test, but the guard that preserves them for ternaries only checked for syms.test. A lambda iterable such as `[x for x in (lambda: 0) if x]` fell through, so the parens were dropped and the trailing comprehension clauses were swallowed into the lambda body, producing unparsable output and an ASTSafetyError crash. Extend the guard to syms.lambdef as well.
c17fc45 to
f0ce134
Compare
sarathfrancis90
added a commit
to sarathfrancis90/black
that referenced
this pull request
Jun 26, 2026
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
On
--preview, this code crashes Black with anASTSafetyError:wrap_comprehension_inmakes the parens around a comprehension's iterableinvisible when the inner expression is low-precedence. There's already a guard
that keeps them for ternaries, but it only checks for
syms.test, so a lambdaiterable slips through. Dropping the parens lets the trailing
if/forclausesget parsed as part of the lambda body (
lambda: 0 if x), which isn't valid, soBlack emits unparsable output and bails out with an internal error. A bare
[x for x in (lambda y: y)]is broken the same way.The fix extends the existing guard to also cover
syms.lambdef. Long lambdaiterables still get wrapped, just with the parens kept. Hit a few comprehension
shapes (list/set/dict/genexpr, trailing
ifandfor) while checking it.Checklist - did you ...
--previewstyle, following the stability policy?CHANGES.mdif necessary?