From 7424593e10dcc32788d147bffe247283016a0eca Mon Sep 17 00:00:00 2001 From: Yilei Yang Date: Tue, 20 Dec 2022 15:46:32 -0800 Subject: [PATCH 1/2] Fix an issue where extra empty lines are added. --- CHANGES.md | 2 ++ src/black/lines.py | 3 ++- tests/data/preview/comments9.py | 48 +++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index e2c5adfda35..c07d81d1320 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -23,6 +23,8 @@ regular and f-strings start with an empty span (#3463) - Fix a crash in preview advanced string processing where a standalone comment is placed before a dict's value (#3469) +- Fix an issue where extra empty lines are added when a decorator has `# fmt: skip` + applied or there is a standalone comment between decorators (#3470) - Do not put the closing quotes in a docstring on a separate line, even if the line is too long (#3430) - Long values in dict literals are now wrapped in parentheses; correspondingly diff --git a/src/black/lines.py b/src/black/lines.py index 08281bcf370..2aa675c3b31 100644 --- a/src/black/lines.py +++ b/src/black/lines.py @@ -520,7 +520,8 @@ def maybe_empty_lines(self, current_line: Line) -> LinesBlock: and (self.semantic_leading_comment is None or before) ): self.semantic_leading_comment = block - elif not current_line.is_decorator: + # `or before` means this decorator already has an empty line before + elif not current_line.is_decorator or before: self.semantic_leading_comment = None self.previous_line = current_line diff --git a/tests/data/preview/comments9.py b/tests/data/preview/comments9.py index 449612c037a..3162bce29cf 100644 --- a/tests/data/preview/comments9.py +++ b/tests/data/preview/comments9.py @@ -114,6 +114,30 @@ def first_method(self): pass +# Regression test for https://github.com/psf/black/issues/3454. +def foo(): + pass + # Trailing comment that belongs to this function + + +@decorator1 +@decorator2 # fmt: skip +def bar(): + pass + + +# Regression test for https://github.com/psf/black/issues/3454. +def foo(): + pass + # Trailing comment that belongs to this function + + +@decorator1 +# A standalone comment +def bar(): + pass + + # output @@ -252,3 +276,27 @@ class MyClass: # More comments. def first_method(self): pass + + +# Regression test for https://github.com/psf/black/issues/3454. +def foo(): + pass + # Trailing comment that belongs to this function + + +@decorator1 +@decorator2 # fmt: skip +def bar(): + pass + + +# Regression test for https://github.com/psf/black/issues/3454. +def foo(): + pass + # Trailing comment that belongs to this function + + +@decorator1 +# A standalone comment +def bar(): + pass From aae8b4d893351e7ae88225251193bf0bd70b8128 Mon Sep 17 00:00:00 2001 From: Yilei Yang Date: Tue, 20 Dec 2022 15:50:53 -0800 Subject: [PATCH 2/2] Tweak test case to show one blank line also works. --- tests/data/preview/comments9.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/data/preview/comments9.py b/tests/data/preview/comments9.py index 3162bce29cf..77b25556e74 100644 --- a/tests/data/preview/comments9.py +++ b/tests/data/preview/comments9.py @@ -129,8 +129,9 @@ def bar(): # Regression test for https://github.com/psf/black/issues/3454. def foo(): pass - # Trailing comment that belongs to this function - + # Trailing comment that belongs to this function. + # NOTE this comment only has one empty line below, and the formatter + # should enforce two blank lines. @decorator1 # A standalone comment @@ -293,7 +294,9 @@ def bar(): # Regression test for https://github.com/psf/black/issues/3454. def foo(): pass - # Trailing comment that belongs to this function + # Trailing comment that belongs to this function. + # NOTE this comment only has one empty line below, and the formatter + # should enforce two blank lines. @decorator1