Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unstable format involving backslash + whitespace at beginning of file #948

Merged
merged 3 commits into from
Aug 4, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions black.py
Original file line number Diff line number Diff line change
Expand Up @@ -736,8 +736,11 @@ def format_str(src_contents: str, *, mode: FileMode) -> FileContent:
for _ in range(after):
dst_contents.append(str(empty_line))
before, after = elt.maybe_empty_lines(current_line)
for _ in range(before):
dst_contents.append(str(empty_line))
# Black should not insert empty lines at the beginning
# of the file
if len(dst_contents) > 0:
Jma353 marked this conversation as resolved.
Show resolved Hide resolved
for _ in range(before):
dst_contents.append(str(empty_line))
for line in split_line(
current_line, line_length=mode.line_length, features=split_line_features
):
Expand Down
12 changes: 12 additions & 0 deletions tests/data/beginning_backslash.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
\





print("hello, world")

# output


print("hello, world")
8 changes: 8 additions & 0 deletions tests/test_black.py
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,14 @@ def test_tuple_assign(self) -> None:
black.assert_equivalent(source, actual)
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:
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"
Expand Down