Preserve blank lines before fmt-off classes#5238
Conversation
|
What logic normally handles newlines before classes? Why isn't it triggering here? Would it make morse sense to update it? |
|
Good question — I dug into where this comes from. The logic that normally handles blank lines before classes is It doesn't trigger here because On updating the normal logic instead: I considered teaching That said, if you'd rather see this handled inside |
|
Sounds good. Thank you for investigating & fixing this! |
|
Whoops, wrong button |
Description
Fixes #5225.
When a top-level class starts inside a
# fmt: offblock after an import, theconverted 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
classtoken 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 -qenv -u NO_COLOR .venv/bin/pytest -qenv -u NO_COLOR .venv/bin/pre-commit run mypy --files src/black/lines.py src/black/mode.pyenv -u NO_COLOR .venv/bin/tox -e run_self.venv/bin/tox -e generate_schemaenv -u NO_COLOR .venv/bin/pre-commit run --all-filespassed every hook exceptrepository-wide mypy, which reports an unchanged
action/main.pytomllibignore/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 ...
--previewstyle, following the stability policy?CHANGES.mdif necessary?