Skip to content

Commit

Permalink
empty lines
Browse files Browse the repository at this point in the history
  • Loading branch information
hauntsaninja committed Jul 22, 2023
1 parent 9a10436 commit c38a470
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions src/black/lines.py
Expand Up @@ -5,6 +5,7 @@
from typing import (
Callable,
Dict,
Final,
Iterator,
List,
Optional,
Expand Down Expand Up @@ -165,6 +166,13 @@ def is_def(self) -> bool:
and second_leaf.value == "def"
)

@property
def is_stub_def(self) -> bool:
"""Is this line a function definition with a body consisting only of "..."?"""
return self.is_def and self.leaves[-3:] == [
Leaf(token.DOT, ".") for _ in range(3)
]

@property
def is_class_paren_empty(self) -> bool:
"""Is this a class with no base classes but using parentheses?
Expand Down Expand Up @@ -578,6 +586,8 @@ def _maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
first_leaf.prefix = ""
else:
before = 0

user_hint_before: Final = before
depth = current_line.depth

previous_def = None
Expand Down Expand Up @@ -624,7 +634,9 @@ def _maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
before = 2

if current_line.is_decorator or current_line.is_def or current_line.is_class:
return self._maybe_empty_lines_for_class_or_def(current_line, before)
return self._maybe_empty_lines_for_class_or_def(
current_line, before, user_hint_before
)

if (
self.previous_line
Expand All @@ -648,8 +660,8 @@ def _maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
return 0, 0
return before, 0

def _maybe_empty_lines_for_class_or_def(
self, current_line: Line, before: int
def _maybe_empty_lines_for_class_or_def( # noqa: C901
self, current_line: Line, before: int, user_hint_before: int
) -> Tuple[int, int]:
if not current_line.is_decorator:
self.previous_defs.append(current_line)
Expand Down Expand Up @@ -714,7 +726,14 @@ def _maybe_empty_lines_for_class_or_def(
else:
newlines = 0
else:
newlines = 1 if current_line.depth else 2
if (
Preview.dummy_implementations in self.mode
and self.previous_line.is_stub_def
and (current_line.is_stub_def or current_line.is_decorator)
):
newlines = user_hint_before
else:
newlines = 1 if current_line.depth else 2
if comment_to_add_newlines is not None:
previous_block = comment_to_add_newlines.previous_block
if previous_block is not None:
Expand Down

0 comments on commit c38a470

Please sign in to comment.