From ab0895c454eebf3a03099eba9239b64664e196e0 Mon Sep 17 00:00:00 2001 From: Yilei Yang Date: Fri, 30 Sep 2022 12:25:21 -0700 Subject: [PATCH] Fix a bug where empty line is added before eof. --- src/black/__init__.py | 2 ++ .../simple_cases/docstring_no_extra_empty_line_before_eof.py | 4 ++++ 2 files changed, 6 insertions(+) create mode 100644 tests/data/simple_cases/docstring_no_extra_empty_line_before_eof.py diff --git a/src/black/__init__.py b/src/black/__init__.py index 42b56a52d2b..0dd2ec385b7 100644 --- a/src/black/__init__.py +++ b/src/black/__init__.py @@ -1085,6 +1085,8 @@ def _format_str_once(src_contents: str, *, mode: Mode) -> str: current_line, mode=mode, features=split_line_features ): block.content_lines.append(str(line)) + if dst_blocks: + dst_blocks[-1].after = 0 dst_contents = [] for block in dst_blocks: dst_contents.extend(block.all_lines()) diff --git a/tests/data/simple_cases/docstring_no_extra_empty_line_before_eof.py b/tests/data/simple_cases/docstring_no_extra_empty_line_before_eof.py new file mode 100644 index 00000000000..6fea860adf6 --- /dev/null +++ b/tests/data/simple_cases/docstring_no_extra_empty_line_before_eof.py @@ -0,0 +1,4 @@ +# Make sure when the file ends with class's docstring, +# It doesn't add extra blank lines. +class ClassWithDocstring: + """A docstring."""