Skip to content

Preserve blank lines before fmt-off classes#5238

Open
sjh9714 wants to merge 1 commit into
psf:mainfrom
sjh9714:codex/20260715-5225-fmt-off-blank-lines
Open

Preserve blank lines before fmt-off classes#5238
sjh9714 wants to merge 1 commit into
psf:mainfrom
sjh9714:codex/20260715-5225-fmt-off-blank-lines

Conversation

@sjh9714

@sjh9714 sjh9714 commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

Description

Fixes #5225.

When a top-level class starts inside a # fmt: off block after an import, the
converted block looks like a standalone comment to the empty-line tracker. This caused
Black to keep only one blank line instead of the two required before a top-level class.

Preserve the converted block's original class token when deciding import separation.
The change is limited to a new preview feature, so stable formatting remains unchanged,
and a focused fixture covers the reported input.

Testing

  • .venv/bin/pytest tests/test_format.py -k preview_fmt_off_class_blank_lines -q
  • .venv/bin/pytest tests/test_format.py -q
  • env -u NO_COLOR .venv/bin/pytest -q
  • env -u NO_COLOR .venv/bin/pre-commit run mypy --files src/black/lines.py src/black/mode.py
  • env -u NO_COLOR .venv/bin/tox -e run_self
  • .venv/bin/tox -e generate_schema

env -u NO_COLOR .venv/bin/pre-commit run --all-files passed every hook except
repository-wide mypy, which reports an unchanged action/main.py tomllib
ignore/stub mismatch under Python 3.14. The targeted mypy hook above passes for both
changed Python modules.

Note: I used Codex while preparing this change, reviewed the final diff, and ran the
checks listed above locally.

Checklist - did you ...

  • Implement any code style changes under the --preview style, following the stability policy?
  • Add an entry in CHANGES.md if necessary?
  • Add / update tests if necessary?
  • Add new / update outdated documentation?

@github-actions

Copy link
Copy Markdown
Contributor

diff-shades results comparing this PR (7371f49) to main (51abf53):

--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.

Inconsistent number of lines after import

1 participant