From 914b994fe4ebf886a6d52991d6bb3bbf82adae0f Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Mon, 1 Jan 2024 19:28:15 -0800 Subject: [PATCH 1/2] Make `blank_line_after_nested_stub_class` work for methods Fixes #4113 Authored by dhruvmanila --- src/black/lines.py | 8 ++++---- tests/data/cases/nested_stub.py | 27 ++++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/black/lines.py b/src/black/lines.py index b544c5e0035..9eb5785da57 100644 --- a/src/black/lines.py +++ b/src/black/lines.py @@ -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: diff --git a/tests/data/cases/nested_stub.py b/tests/data/cases/nested_stub.py index b81549ec115..ef13c588ce6 100644 --- a/tests/data/cases/nested_stub.py +++ b/tests/data/cases/nested_stub.py @@ -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 @@ -41,4 +53,17 @@ def f1(self) -> str: ... def function_definition(self): ... assignment = 1 - def f2(self) -> str: ... \ No newline at end of file + def f2(self) -> str: ... + +class TopLevel: + class Nested1: + foo: int + def bar(self): ... + + field = 1 + + class Nested2: + def bar(self): ... + foo: int + + field = 1 From 5ae52390fe4be254299cba80e7bc1ba906b3b81c Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Mon, 1 Jan 2024 19:37:36 -0800 Subject: [PATCH 2/2] . --- CHANGES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.md b/CHANGES.md index 0e3ba58014b..b1940e9187f 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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