Skip to content

Black removes empty lines in fmt:off sections #2877

Description

@amyreese

Describe the bug

For files with a #fmt:off blocks, black will still remove empty newlines in these otherwise-unformatted sections. This is kind of pedantic, but it defies expectations that black would change anything at all here.

To Reproduce

Given the source file (note especially the trailing newlines at the end):

something = [1,2,3]


# fmt:off
other = [1,2,3
        ]



# fmt:on
another = [1,2,3]



# fmt:off
last = [1,2,3
        ]

And run it with these arguments:

$ black --diff foo.py
--- foo.py      2022-02-10 21:51:33.647821 +0000
+++ foo.py      2022-02-10 21:54:13.748438 +0000
@@ -1,19 +1,15 @@
-something = [1,2,3]
+something = [1, 2, 3]


 # fmt:off
 other = [1,2,3
         ]


-
 # fmt:on
-another = [1,2,3]
-
+another = [1, 2, 3]


 # fmt:off
 last = [1,2,3
         ]
-
-
would reformat foo.py
All done! ✨ 🍰 ✨
1 file would be reformatted.

Expected behavior

Black should leave everything as-is inside #fmt:off blocks, including whitespace and newlines.

Environment

  • Black's version:
(venv39) jreese@butterfree ~/scratch » black --version
black.macos, version 21.4b2
  • OS and Python version: macOS 12.1, Python 3.9.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    F: fmtofffmt: off implementationT: bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions