From 604b8053279b01e6781ba7d27740a115c95ee9d8 Mon Sep 17 00:00:00 2001 From: Yilei Yang Date: Tue, 4 Oct 2022 20:39:24 -0700 Subject: [PATCH] Fix an issue where empty lines were added between comments inside decorators. --- src/black/lines.py | 2 ++ tests/data/preview/comments9.py | 2 ++ 2 files changed, 4 insertions(+) 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