Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e11eaf2

Browse files
authored
Make blank_line_after_nested_stub_class work for methods (psf#4141)
Fixes psf#4113 Authored by dhruvmanila
1 parent b1d17ef commit e11eaf2

3 files changed

Lines changed: 31 additions & 5 deletions

File tree

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
- Remove empty lines before docstrings in async functions (#4132)
2222
- Address a missing case in the change to allow empty lines at the beginning of all
2323
blocks, except immediately before a docstring (#4130)
24+
- For stubs, fix logic to enforce empty line after nested classes with bodies (#4141)
2425

2526
### Configuration
2627

src/black/lines.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -640,15 +640,15 @@ def _maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
640640
if previous_def is not None:
641641
assert self.previous_line is not None
642642
if self.mode.is_pyi:
643-
if depth and not current_line.is_def and self.previous_line.is_def:
644-
# Empty lines between attributes and methods should be preserved.
645-
before = 1 if user_had_newline else 0
646-
elif (
643+
if (
647644
Preview.blank_line_after_nested_stub_class in self.mode
648645
and previous_def.is_class
649646
and not previous_def.is_stub_class
650647
):
651648
before = 1
649+
elif depth and not current_line.is_def and self.previous_line.is_def:
650+
# Empty lines between attributes and methods should be preserved.
651+
before = 1 if user_had_newline else 0
652652
elif depth:
653653
before = 0
654654
else:

tests/data/cases/nested_stub.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ def function_definition(self): ...
1818
assignment = 1
1919
def f2(self) -> str: ...
2020

21+
22+
class TopLevel:
23+
class Nested1:
24+
foo: int
25+
def bar(self): ...
26+
field = 1
27+
28+
class Nested2:
29+
def bar(self): ...
30+
foo: int
31+
field = 1
32+
2133
# output
2234

2335
import sys
@@ -41,4 +53,17 @@ def f1(self) -> str: ...
4153
def function_definition(self): ...
4254
assignment = 1
4355

44-
def f2(self) -> str: ...
56+
def f2(self) -> str: ...
57+
58+
class TopLevel:
59+
class Nested1:
60+
foo: int
61+
def bar(self): ...
62+
63+
field = 1
64+
65+
class Nested2:
66+
def bar(self): ...
67+
foo: int
68+
69+
field = 1

0 commit comments

Comments
 (0)