Skip to content

Commit

Permalink
Fix a bug where two empty lines were added before class's leading com…
Browse files Browse the repository at this point in the history
…ment but after decorators.
  • Loading branch information
yilei committed Oct 5, 2022
1 parent 2e9d824 commit 7695ab7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/black/lines.py
Expand Up @@ -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:
Expand Down
32 changes: 32 additions & 0 deletions tests/data/preview/comments9.py
Expand Up @@ -40,6 +40,7 @@ class MyClassAfterAnotherClassWithDocstring:
@deco2(with_args=True)
# leading 3
@deco3
# leading 4
def decorated():
pass

Expand All @@ -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

Expand Down Expand Up @@ -139,6 +154,7 @@ class MyClassAfterAnotherClassWithDocstring:
@deco2(with_args=True)
# leading 3
@deco3
# leading 4
def decorated():
pass

Expand All @@ -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

Expand Down

0 comments on commit 7695ab7

Please sign in to comment.