You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
empty lines at end of file trimmed even with fmt: off #496
The problem here appears to happen in convert_one_fmt_off_pair, where the node enters as # fmt: off\n\nprint("hello")\n\n and exits as # fmt: off\n\nprint("hello")\n. I am continuing to investigate.
The function generate_ignored_nodes creates a list of nodes that is just # fmt: off\n\nprint("hello")\n. This is where the final newline is lost.
Unfortunately, I've reached the end of my experience/time to look at this today. I hope this has given someone more versed in the project a better starting place for fixing this bug.
EDIT: also, because of how test files are read in read_data in test_black.py, it will be difficult to write a test for this since the final \n will be removed.
I reproduced this on the current development branch (26.5.2.dev98+g8947c48ef, CPython 3.12.3).
Minimal input:
# fmt: offprint("hello")
Running black file.py removes the final blank line and produces:
# fmt: offprint("hello")
The issue is that the parser stores trailing blank lines after the final statement in the END marker prefix, while fmt-off conversion currently discards that prefix. I have a focused fix that preserves the END marker prefix for an unclosed # fmt: off block at EOF, together with a regression test through Black’s file-formatting path.
Activity
zsol commentedon Sep 7, 2018
Sounds like a bug. Want to take a look at fixing it?
scolby33 commentedon Dec 17, 2018
The problem here appears to happen in
convert_one_fmt_off_pair, where the node enters as# fmt: off\n\nprint("hello")\n\nand exits as# fmt: off\n\nprint("hello")\n. I am continuing to investigate.scolby33 commentedon Dec 17, 2018
The function
generate_ignored_nodescreates a list of nodes that is just# fmt: off\n\nprint("hello")\n. This is where the final newline is lost.Unfortunately, I've reached the end of my experience/time to look at this today. I hope this has given someone more versed in the project a better starting place for fixing this bug.
EDIT: also, because of how test files are read in
read_dataintest_black.py, it will be difficult to write a test for this since the final\nwill be removed.felix-hilden commentedon Jan 29, 2022
At least our documentation states that
fmt: offshould be followed by afmt: on, but supporting a single off switch is a reasonable request.tagadearpit commentedon Aug 21, 2026
I reproduced this on the current development branch (26.5.2.dev98+g8947c48ef, CPython 3.12.3).
Minimal input:
Running black file.py removes the final blank line and produces:
The issue is that the parser stores trailing blank lines after the final statement in the END marker prefix, while fmt-off conversion currently discards that prefix. I have a focused fix that preserves the END marker prefix for an unclosed # fmt: off block at EOF, together with a regression test through Black’s file-formatting path.
Preserve trailing blank lines at EOF in unclosed fmt: off blocks (psf…
Preserve trailing blank lines at EOF in unclosed fmt: off blocks (#496)…
Preserve trailing blank lines at EOF in unclosed fmt: off blocks (psf…