Skip to content

Commit

Permalink
Fix an issue where an empty line was added right after a bare class def.
Browse files Browse the repository at this point in the history
  • Loading branch information
yilei committed Oct 5, 2022
1 parent 7695ab7 commit 52e5e1c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/black/lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,7 @@ class LinesBlock:

mode: Mode
previous_block: Optional["LinesBlock"]
is_class: bool # Whether the original line is a class def.
before: int = 0
content_lines: List[str] = field(default_factory=list)
after: int = 0
Expand Down Expand Up @@ -500,7 +501,11 @@ def maybe_empty_lines(self, current_line: Line) -> LinesBlock:
if self.previous_line is None
else before - previous_after
)
block = LinesBlock(mode=self.mode, previous_block=self.previous_block)
block = LinesBlock(
mode=self.mode,
previous_block=self.previous_block,
is_class=current_line.is_class,
)
block.before = before
block.after = after

Expand Down Expand Up @@ -623,6 +628,7 @@ def _maybe_empty_lines_for_class_or_def(
in current_line.mode
and slc is not None
and slc.previous_block is not None
and not slc.previous_block.is_class
and slc.before <= 1
):
comment_to_add_newlines = slc
Expand Down
14 changes: 14 additions & 0 deletions tests/data/preview/comments9.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ def top_level_quote_inline_after_else():
pass


class MyClass:
# First method has no empty lines between bare class def.
# More comments.
def first_method(self):
pass


# output


Expand Down Expand Up @@ -222,3 +229,10 @@ def another_top_level_quote_inline_inline():
# More leading comments
def top_level_quote_inline_after_else():
pass


class MyClass:
# First method has no empty lines between bare class def.
# More comments.
def first_method(self):
pass

0 comments on commit 52e5e1c

Please sign in to comment.