Repository navigation
Fix # fmt: skip on the last line of a multi-line conditional expression in brackets - #5647
Merged
Merged
Conversation
When a conditional expression in a collection spans several lines and
`# fmt: skip` is on its last line, only the code on that line is hidden,
and the hidden code also takes the comma after the expression. The
STANDALONE_COMMENT holding it then ends the `test` node, so the invisible
parentheses that `visit_test` adds closed after the comma, and the output
turned the collection into a call or didn't parse:
x = (
a
if c else d, # fmt: skip
e,
)
Don't wrap the expression in invisible parentheses when its last leaf is
code hidden by `# fmt: skip`, like the existing check for when hidden code
starts it.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
b84330808
force-pushed
the
fix-fmt-skip-multiline-ternary
branch
from
November 1, 2026 10:05
e1a4568 to
b427ba8
Compare
cobaltt7
reviewed
Nov 1, 2026
Collaborator
|
Thanks! |
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
When a conditional expression inside brackets spans several lines and
# fmt: skipis on its last line, Black produces invalid code:With
--fastit's written out silently; a variant likea # comment/+ b if c else d, # fmt: skip/(e),turns the tuple into a call:(a ... else d, # fmt: skip\n)(e).# fmt: skiphides only the code on its own line (if c else d,), so that hidden code ends thetestnode and also takes the comma after it.visit_testthen wraps the node in invisible parentheses and the closing one lands after the comma. #5464 fixed the case where the whole ternary is on the skipped line; this is the multi-line one.visit_testnow skips the invisible parentheses when the node's last leaf is code hidden by# fmt: skip, like the existing check for hidden code at the start. Three multi-line cases added tofmtskip_conditional_in_brackets.py; they fail onmainand pass with the change.The same thing still happens under
--previewfor a dict value viawrap_long_dict_values_in_parens; I left that alone since #5627 is reworking that code.Checklist - did you ...
--previewstyle, following the stability policy?CHANGES.mdif necessary?I used Claude Code to write this change.