Repository navigation
Fix # fmt: skip on the opening bracket of an if/while/for/with header - #5406
Closed
anandghegde wants to merge 2 commits into
Closed
anandghegde wants to merge 2 commits into
anandghegde wants to merge 2 commits into
Conversation
The last-resort `_force_standalone_comment_split` added in psf#5144 split after every standalone comment. Code converted by `# fmt: skip` is also stored as a STANDALONE_COMMENT, and for `if ( # fmt: skip` it ends in `)`, not a comment. Splitting there moved the header's `:` onto its own line, so the second pass raised InvalidInput. Only split after a standalone comment whose last line holds a comment. Fixes psf#5402
anandghegde
force-pushed
the
fix-fmt-skip-compound-header-split
branch
from
September 15, 2026 16:39
ba864a9 to
161c31c
Compare
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
Fixes #5402.
On
main,if ( # fmt: skipfollowed by a multi-line condition fails withInvalidInput: cannot parse.while (,for i in (andwith (fail the same way. 26.5.1 formats all of these.When
# fmt: skipsits on the opening bracket, the wholeif ( ... )gets turned into oneSTANDALONE_COMMENTleaf, and the line is just that leaf plus:.right_hand_splitfinds nothing to split, so the line falls through to_force_standalone_comment_splitfrom #5144. That function splits after every standalone comment, which puts the:on its own line, and the second pass can't parse it.The split after a standalone comment only matters when the comment would swallow the code after it. A real standalone comment always does. Code converted by
fmt: skiponly does if its last line ends in a comment, and here it ends in). So the split after a standalone comment now only happens when its last line contains#. Code with a#inside a string on that last line still gets split, so those cases behave exactly as they do today.The paired form from #5397 (
# fmt: skipon both the opening line and the):line) still fails after this change. It's a different path, and #5401 covers it.Implemented with Claude Code; I reviewed and tested the change.
Checklist - did you ...
--previewstyle, following the stability policy? (No style change. This fixes a crash on valid input.)CHANGES.mdif necessary?tests/data/cases/fmtskip_compound_header_open_bracket.pyfails onmainand passes with the fix.)🤖 Generated with Claude Code
https://claude.ai/code/session_01MA8JGs7pCoXFL2W6jEJ8sD