Fix unstable format involving backslash + whitespace at beginning of file (#948)
This commit is contained in:
parent
65ea568e33
commit
c7495b9aa0
8
black.py
8
black.py
@ -1480,7 +1480,13 @@ def maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
|
|||||||
lines (two on module-level).
|
lines (two on module-level).
|
||||||
"""
|
"""
|
||||||
before, after = self._maybe_empty_lines(current_line)
|
before, after = self._maybe_empty_lines(current_line)
|
||||||
before -= self.previous_after
|
before = (
|
||||||
|
# Black should not insert empty lines at the beginning
|
||||||
|
# of the file
|
||||||
|
0
|
||||||
|
if self.previous_line is None
|
||||||
|
else before - self.previous_after
|
||||||
|
)
|
||||||
self.previous_after = after
|
self.previous_after = after
|
||||||
self.previous_line = current_line
|
self.previous_line = current_line
|
||||||
return before, after
|
return before, after
|
||||||
|
12
tests/data/beginning_backslash.py
Normal file
12
tests/data/beginning_backslash.py
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
\
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
print("hello, world")
|
||||||
|
|
||||||
|
# output
|
||||||
|
|
||||||
|
|
||||||
|
print("hello, world")
|
@ -639,6 +639,14 @@ def test_tuple_assign(self) -> None:
|
|||||||
black.assert_equivalent(source, actual)
|
black.assert_equivalent(source, actual)
|
||||||
black.assert_stable(source, actual, black.FileMode())
|
black.assert_stable(source, actual, black.FileMode())
|
||||||
|
|
||||||
|
@patch("black.dump_to_file", dump_to_stderr)
|
||||||
|
def test_beginning_backslash(self) -> None:
|
||||||
|
source, expected = read_data("beginning_backslash")
|
||||||
|
actual = fs(source)
|
||||||
|
self.assertFormatEqual(expected, actual)
|
||||||
|
black.assert_equivalent(source, actual)
|
||||||
|
black.assert_stable(source, actual, black.FileMode())
|
||||||
|
|
||||||
def test_tab_comment_indentation(self) -> None:
|
def test_tab_comment_indentation(self) -> None:
|
||||||
contents_tab = "if 1:\n\tif 2:\n\t\tpass\n\t# comment\n\tpass\n"
|
contents_tab = "if 1:\n\tif 2:\n\t\tpass\n\t# comment\n\tpass\n"
|
||||||
contents_spc = "if 1:\n if 2:\n pass\n # comment\n pass\n"
|
contents_spc = "if 1:\n if 2:\n pass\n # comment\n pass\n"
|
||||||
|
Loading…
Reference in New Issue
Block a user