Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make blank_line_after_nested_stub_class work for methods #4141

Merged
merged 2 commits into from Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Expand Up @@ -20,6 +20,7 @@
- Remove empty lines before docstrings in async functions (#4132)
- Address a missing case in the change to allow empty lines at the beginning of all
blocks, except immediately before a docstring (#4130)
- For stubs, fix logic to enforce empty line after nested classes with bodies (#4141)

### Configuration

Expand Down
8 changes: 4 additions & 4 deletions src/black/lines.py
Expand Up @@ -640,15 +640,15 @@ def _maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
if previous_def is not None:
assert self.previous_line is not None
if self.mode.is_pyi:
if depth and not current_line.is_def and self.previous_line.is_def:
# Empty lines between attributes and methods should be preserved.
before = 1 if user_had_newline else 0
elif (
if (
Preview.blank_line_after_nested_stub_class in self.mode
and previous_def.is_class
and not previous_def.is_stub_class
):
before = 1
elif depth and not current_line.is_def and self.previous_line.is_def:
# Empty lines between attributes and methods should be preserved.
before = 1 if user_had_newline else 0
elif depth:
before = 0
else:
Expand Down
27 changes: 26 additions & 1 deletion tests/data/cases/nested_stub.py
Expand Up @@ -18,6 +18,18 @@ def function_definition(self): ...
assignment = 1
def f2(self) -> str: ...


class TopLevel:
class Nested1:
foo: int
def bar(self): ...
field = 1

class Nested2:
def bar(self): ...
foo: int
field = 1

# output

import sys
Expand All @@ -41,4 +53,17 @@ def f1(self) -> str: ...
def function_definition(self): ...
assignment = 1

def f2(self) -> str: ...
def f2(self) -> str: ...

class TopLevel:
class Nested1:
foo: int
def bar(self): ...

field = 1

class Nested2:
def bar(self): ...
foo: int

field = 1