Preserve blank lines before a closing fmt: on comment - #5300
Open
alliasgher wants to merge 2 commits into
Open
Conversation
The blank run that terminates a fmt: off region lives in the prefix of the following '# fmt: on' standalone comment, not in the verbatim block that _handle_regular_fmt_block produces. _maybe_empty_lines therefore capped it to max_allowed like any ordinary blank run, silently reformatting lines the user had opted out of. Skip the cap for a '# fmt: on' comment that closes a converted block.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Blank lines immediately before a closing
# fmt: onare collapsed to the usual maximum, even though they are inside the opted-out region:comes back with two. Reproduced on current
main(74371e2).convert_one_fmt_off_pair→_handle_regular_fmt_blockcollapses the region into a singleSTANDALONE_COMMENTwhosehidden_valueis verbatim, which is why blanks between statements survive. But the blank run that terminates the region is not part of that value — it lives in the prefix of the following# fmt: oncomment, which is owned by the normal blank-line machinery. So_maybe_empty_linesappliesbefore = min(before, max_allowed)to it like any ordinary blank run.I confirmed the mechanism by instrumenting
EmptyLineTracker._maybe_empty_linesrather than reading it off: for the input above the# fmt: online logsraw_nl=4 -> before=2.The loss is one-shot — the output is stable on a second pass — which matches the report of a fresh diff every time the file is regenerated.
Scope
The issue bundles two defects and this takes only the first. The second symptom, blank lines lost at end of file, is a different mechanism (end-of-file newline normalisation, in
generate_ignored_nodes) and is separately tracked as still-open #496.tests/util.read_dataalso strips the final newline, so it cannot be covered by atests/data/casesfixture as-is. I did not want to bury an unrelated fix in this PR.Fixes the mid-file half of #2877.
Checklist
tests/data/cases/fmtonoff9.py, no# outputsection, so it asserts the input is left untouched)CHANGES.mdTesting
I checked the fixture is not vacuous: with
src/black/lines.pyreverted,test_simple_format[fmtonoff9]fails; with it applied, it passes.Covered in the fixture: three and four blank lines, nested inside a
def,# yapf: disable/# yapf: enable, and the no-space# fmt:off/# fmt:onspelling. I also checked separately that a bare# fmt: onwith no preceding# fmt: offis still capped normally — those blanks are not in an opted-out region, so they should be.Full suite: 478 passed, 3 skipped. No existing fixture depended on the old behaviour (7
fmtonoff*, 3line_ranges_fmt_off*, 20+fmtskip*all unchanged).black --checkon the two changed files is clean.