From 23dd1c4b40d48e739aa04e091b614a07bef89dbf Mon Sep 17 00:00:00 2001 From: Yilei Yang Date: Tue, 4 Oct 2022 19:26:17 -0700 Subject: [PATCH] Fix a bug where two empty lines were added before class's leading comment but after decorators. --- src/black/lines.py | 7 +++---- tests/data/preview/comments9.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/black/lines.py b/src/black/lines.py index 2570d1627ac..2fa1998235c 100644 --- a/src/black/lines.py +++ b/src/black/lines.py @@ -506,10 +506,9 @@ def maybe_empty_lines(self, current_line: Line) -> LinesBlock: # Maintain the semantic_leading_comment state. if current_line.is_comment: - if ( - self.semantic_leading_comment is None - or self.previous_line is None - or (before and not self.previous_line.is_decorator) + if self.previous_line is None or ( + not self.previous_line.is_decorator + and (self.semantic_leading_comment is None or before) ): self.semantic_leading_comment = block elif not current_line.is_decorator: diff --git a/tests/data/preview/comments9.py b/tests/data/preview/comments9.py index e70b1209195..18966c16ff2 100644 --- a/tests/data/preview/comments9.py +++ b/tests/data/preview/comments9.py @@ -40,6 +40,7 @@ class MyClassAfterAnotherClassWithDocstring: @deco2(with_args=True) # leading 3 @deco3 +# leading 4 def decorated(): pass @@ -52,6 +53,20 @@ def decorated(): # leading 3 that already has an empty line @deco3 +# leading 4 +def decorated_with_split_leading_comments(): + pass + + +some = statement +# leading 1 +@deco1 +# leading 2 +@deco2(with_args=True) +# leading 3 +@deco3 + +# leading 4 that already has an empty line def decorated_with_split_leading_comments(): pass @@ -139,6 +154,7 @@ class MyClassAfterAnotherClassWithDocstring: @deco2(with_args=True) # leading 3 @deco3 +# leading 4 def decorated(): pass @@ -153,6 +169,22 @@ def decorated(): # leading 3 that already has an empty line @deco3 +# leading 4 +def decorated_with_split_leading_comments(): + pass + + +some = statement + + +# leading 1 +@deco1 +# leading 2 +@deco2(with_args=True) +# leading 3 +@deco3 + +# leading 4 that already has an empty line def decorated_with_split_leading_comments(): pass