Skip to content

Fix crash on fmt: skip at the opening bracket of a compound statement header - #5403

Closed
itzzdev09 wants to merge 1 commit into
psf:mainfrom
itzzdev09:fix-fmt-skip-compound-header-bracket
Closed

itzzdev09 wants to merge 1 commit into
psf:mainfrom
itzzdev09:fix-fmt-skip-compound-header-bracket

Conversation

@itzzdev09

Copy link
Copy Markdown

Description

Fixes #5402.

The last-resort split added in #5144 (_force_standalone_comment_split) breaks the line after every standalone comment leaf. With # fmt: skip on the opening bracket of a parenthesized if/while/for/with header, the whole if ( ... ) becomes a single standalone comment leaf whose text ends in ), not in a comment. Splitting after it moved the : onto its own line, so Black failed on its own output:

if (  # fmt: skip
    cond
)
:

The fix only splits after a standalone comment leaf when its last line actually contains a comment, so the : stays with the header. Real trailing comments still force the split as before.

Added fmtskip_compound_header_open_bracket.py covering the if, while, for and with forms from the issue. Full test suite passes locally (482 passed, 3 skipped).

AI disclosure: this change was made with Claude Code under my supervision; I reviewed the fix and the test and ran the suite locally.

Checklist - did you ...

  • Implement any code style changes under the --preview style, following the stability policy? (crash fix, no style change)
  • Add an entry in CHANGES.md if necessary?
  • Add / update tests if necessary?
  • Add new / update outdated documentation? (not needed)

🤖 Generated with Claude Code

The last-resort split added in psf#5144 broke after every standalone
comment leaf. A '# fmt: skip' on the opening bracket of an if/while/
for/with header turns 'if ( ... )' into a single standalone comment
leaf that ends in ')', not a comment, so splitting after it put the
':' on its own line and Black failed on its own output.

Only split after a standalone comment leaf whose last line has a
comment in it.

Fixes psf#5402

Co-Authored-By: Claude Opus 5 <[email protected]>
@github-actions

Copy link
Copy Markdown
Contributor

diff-shades results comparing this PR (ec89c04) 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