Add blank line after constants in stub file (#360)

Fixes #340
This commit is contained in:
Jelle Zijlstra 2018-06-16 11:53:45 -07:00 committed by Łukasz Langa
parent af8fd24068
commit 9db828c3de
2 changed files with 54 additions and 38 deletions

View File

@ -1390,10 +1390,31 @@ def _maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
before = 0 if depth else 1
else:
before = 1 if depth else 2
is_decorator = current_line.is_decorator
if is_decorator or current_line.is_def or current_line.is_class:
if not is_decorator:
self.previous_defs.append(depth)
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)
if (
self.previous_line
and self.previous_line.is_import
and not current_line.is_import
and depth == self.previous_line.depth
):
return (before or 1), 0
if (
self.previous_line
and self.previous_line.is_class
and current_line.is_triple_quoted_string
):
return before, 1
return before, 0
def _maybe_empty_lines_for_class_or_def(
self, current_line: Line, before: int
) -> Tuple[int, int]:
if not current_line.is_decorator:
self.previous_defs.append(current_line.depth)
if self.previous_line is None:
# Don't insert empty lines before the first line in the file.
return 0, 0
@ -1418,9 +1439,13 @@ def _maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
newlines = 1
elif current_line.is_class or self.previous_line.is_class:
if current_line.is_stub_class and self.previous_line.is_stub_class:
# No blank line between classes with an emty body
newlines = 0
else:
newlines = 1
elif current_line.is_def and not self.previous_line.is_def:
# Blank line between a block of functions and a block of non-functions
newlines = 1
else:
newlines = 0
else:
@ -1429,23 +1454,6 @@ def _maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
newlines -= 1
return newlines, 0
if (
self.previous_line
and self.previous_line.is_import
and not current_line.is_import
and depth == self.previous_line.depth
):
return (before or 1), 0
if (
self.previous_line
and self.previous_line.is_class
and current_line.is_triple_quoted_string
):
return before, 1
return before, 0
@dataclass
class LineGenerator(Visitor[Line]):

View File

@ -1,3 +1,7 @@
X: int
def f(): ...
class C:
...
@ -16,6 +20,10 @@ def g():
def h(): ...
# output
X: int
def f(): ...
class C: ...
class B: ...