Skip to content

Preserve multiline headers with fmt skip#5117

Open
17X61 wants to merge 4 commits intopsf:mainfrom
17X61:fix-fmt-skip-class-header
Open

Preserve multiline headers with fmt skip#5117
17X61 wants to merge 4 commits intopsf:mainfrom
17X61:fix-fmt-skip-class-header

Conversation

@17X61
Copy link
Copy Markdown

@17X61 17X61 commented Apr 27, 2026

Fixes #5112.

Summary

  • Handle # fmt: skip on compound statement colon lines before same-line backtracking.
  • Preserve multiline class/function headers as a whole when the skip comment is on the colon line.
  • Add regression coverage for multiline class, function, async function, and parenthesized if headers.

Test Plan

  • python3 -m pytest tests/test_format.py -k fmtskip_class_header
  • Previously ran the full formatting suite on this branch before the extra edge-case fixture additions.

Copilot AI review requested due to automatic review settings April 27, 2026 15:15
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a regression in Black’s # fmt: skip handling where multiline class/function headers could be collapsed when the skip comment appears on the compound statement’s colon line (e.g. class ...): # fmt: skip).

Changes:

  • Prioritize detection of # fmt: skip on compound-statement colon lines before the generic “same physical line” backtracking logic to preserve multiline headers intact.
  • Keep special handling for async statements when collecting ignored nodes for colon-line skips.
  • Add a regression fixture covering a multiline class base list with # fmt: skip on the colon line.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
tests/data/cases/fmtskip_class_header.py Adds a regression case ensuring multiline class headers remain unchanged when # fmt: skip is on the colon line.
src/black/comments.py Adjusts ignored-node generation ordering so colon-line # fmt: skip preserves entire multiline compound headers.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@cobaltt7
Copy link
Copy Markdown
Collaborator

cobaltt7 commented Apr 27, 2026

@17X61 Please expand the tests to include more edge cases

@github-actions
Copy link
Copy Markdown
Contributor

diff-shades results comparing this PR (41bd05c) to main (9fd9ea2):

--preview style: no changes

--stable style: no changes


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

@17X61
Copy link
Copy Markdown
Author

17X61 commented May 4, 2026

Added more regression coverage for multiline colon-line # fmt: skip cases: function headers, async function headers, and parenthesized if headers, in addition to the original class header case.

Validation run locally:

python3 -m pytest tests/test_format.py -k fmtskip_class_header

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 regression

3 participants