Skip to content

Fix # fmt: skip on the opening bracket of an if/while/for/with header - #5406

Closed
anandghegde wants to merge 2 commits into
psf:mainfrom
anandghegde:fix-fmt-skip-compound-header-split
Closed

anandghegde wants to merge 2 commits into
psf:mainfrom
anandghegde:fix-fmt-skip-compound-header-split

Conversation

@anandghegde

Copy link
Copy Markdown

Description

Fixes #5402.

On main, if ( # fmt: skip followed by a multi-line condition fails with InvalidInput: cannot parse. while (, for i in ( and with ( fail the same way. 26.5.1 formats all of these.

When # fmt: skip sits on the opening bracket, the whole if ( ... ) gets turned into one STANDALONE_COMMENT leaf, and the line is just that leaf plus :. right_hand_split finds nothing to split, so the line falls through to _force_standalone_comment_split from #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: skip only 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: skip on 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 ...

  • Implement any code style changes under the --preview style, following the stability policy? (No style change. This fixes a crash on valid input.)
  • Add an entry in CHANGES.md if necessary?
  • Add / update tests if necessary? (tests/data/cases/fmtskip_compound_header_open_bracket.py fails on main and passes with the fix.)
  • Add new / update outdated documentation? (Not needed.)

🤖 Generated with Claude Code

https://claude.ai/code/session_01MA8JGs7pCoXFL2W6jEJ8sD

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
anandghegde force-pushed the fix-fmt-skip-compound-header-split branch from ba864a9 to 161c31c Compare September 15, 2026 16:39
@github-actions

Copy link
Copy Markdown
Contributor

diff-shades results comparing this PR (161c31c) to main (20622e1):

--preview style: no changes

--stable style: no changes


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

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.

# fmt: skip on an opening bracket alone makes if, while, for and with headers fail to parse

1 participant