Skip to content

Commit eb7661f

Browse files
Fix another case where we format dummy implementation for non-functions/classes (#4103)
1 parent 0c98999 commit eb7661f

File tree

4 files changed

+23
-13
lines changed

4 files changed

+23
-13
lines changed

CHANGES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
docstring (#4060)
2222
- Fix crash in preview mode when using a short `--line-length` (#4086)
2323
- Keep suites consisting of only an ellipsis on their own lines if they are not
24-
functions or class definitions (#4066)
24+
functions or class definitions (#4066) (#4103)
2525

2626
### Configuration
2727

src/black/linegen.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
is_atom_with_invisible_parens,
4343
is_docstring,
4444
is_empty_tuple,
45+
is_function_or_class,
4546
is_lpar_token,
4647
is_multiline_string,
4748
is_name_token,
@@ -299,11 +300,12 @@ def visit_simple_stmt(self, node: Node) -> Iterator[Line]:
299300
wrap_in_parentheses(node, child, visible=False)
300301
prev_type = child.type
301302

302-
is_suite_like = node.parent and node.parent.type in STATEMENT
303-
if is_suite_like:
304-
if (
305-
self.mode.is_pyi or Preview.dummy_implementations in self.mode
306-
) and is_stub_body(node):
303+
if node.parent and node.parent.type in STATEMENT:
304+
if Preview.dummy_implementations in self.mode:
305+
condition = is_function_or_class(node.parent)
306+
else:
307+
condition = self.mode.is_pyi
308+
if condition and is_stub_body(node):
307309
yield from self.visit_default(node)
308310
else:
309311
yield from self.line(+1)

src/black/nodes.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -736,15 +736,18 @@ def is_funcdef(node: Node) -> bool:
736736
return node.type == syms.funcdef
737737

738738

739+
def is_function_or_class(node: Node) -> bool:
740+
return node.type in {syms.funcdef, syms.classdef, syms.async_funcdef}
741+
742+
739743
def is_stub_suite(node: Node, mode: Mode) -> bool:
740744
"""Return True if `node` is a suite with a stub body."""
741-
if node.parent is not None:
742-
if Preview.dummy_implementations in mode and node.parent.type not in (
743-
syms.funcdef,
744-
syms.async_funcdef,
745-
syms.classdef,
746-
):
747-
return False
745+
if (
746+
node.parent is not None
747+
and Preview.dummy_implementations in mode
748+
and not is_function_or_class(node.parent)
749+
):
750+
return False
748751

749752
# If there is a comment, we want to keep it.
750753
if node.prefix.strip():

tests/data/cases/preview_dummy_implementations.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ def has_comment():
5656
if some_condition:
5757
...
5858

59+
if already_dummy: ...
60+
5961
# output
6062

6163
from typing import NoReturn, Protocol, Union, overload
@@ -116,3 +118,6 @@ def has_comment(): ... # still a dummy
116118

117119
if some_condition:
118120
...
121+
122+
if already_dummy:
123+
...

0 commit comments

Comments
 (0)