diff --git a/src/black/lines.py b/src/black/lines.py index 9f0471f9413..f7c8b5fec13 100644 --- a/src/black/lines.py +++ b/src/black/lines.py @@ -513,6 +513,8 @@ def maybe_empty_lines(self, current_line: Line) -> LinesBlock: if current_line.is_comment: if self.previous_line is None or ( not self.previous_line.is_decorator + # `or before` means this comment already has an empty line before + and (not self.previous_line.is_comment or before) and (self.semantic_leading_comment is None or before) ): self.semantic_leading_comment = block diff --git a/tests/data/preview/comments9.py b/tests/data/preview/comments9.py index 1919a97fe45..359d1412158 100644 --- a/tests/data/preview/comments9.py +++ b/tests/data/preview/comments9.py @@ -37,6 +37,7 @@ class MyClassAfterAnotherClassWithDocstring: # leading 1 @deco1 # leading 2 +# leading 2 extra @deco2(with_args=True) # leading 3 @deco3 @@ -158,6 +159,7 @@ class MyClassAfterAnotherClassWithDocstring: # leading 1 @deco1 # leading 2 +# leading 2 extra @deco2(with_args=True) # leading 3 @deco3