Skip to content

Commit 9aea976

Browse files
Only use dummy implementation logic for functions and classes (#4066)
Fixes #4063
1 parent 67b23d7 commit 9aea976

File tree

4 files changed

+32
-5
lines changed

4 files changed

+32
-5
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
- Allow empty lines at the beginning of all blocks, except immediately before a
2121
docstring (#4060)
2222
- Fix crash in preview mode when using a short `--line-length` (#4086)
23+
- Keep suites consisting of only an ellipsis on their own lines if they are not
24+
functions or class definitions (#4066)
2325

2426
### Configuration
2527

src/black/linegen.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ def visit_suite(self, node: Node) -> Iterator[Line]:
286286
"""Visit a suite."""
287287
if (
288288
self.mode.is_pyi or Preview.dummy_implementations in self.mode
289-
) and is_stub_suite(node):
289+
) and is_stub_suite(node, self.mode):
290290
yield from self.visit(node.children[2])
291291
else:
292292
yield from self.visit_default(node)
@@ -314,7 +314,7 @@ def visit_simple_stmt(self, node: Node) -> Iterator[Line]:
314314
if (
315315
not (self.mode.is_pyi or Preview.dummy_implementations in self.mode)
316316
or not node.parent
317-
or not is_stub_suite(node.parent)
317+
or not is_stub_suite(node.parent, self.mode)
318318
):
319319
yield from self.line()
320320
yield from self.visit_default(node)

src/black/nodes.py

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

738738

739-
def is_stub_suite(node: Node) -> bool:
739+
def is_stub_suite(node: Node, mode: Mode) -> bool:
740740
"""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
741748

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

tests/data/cases/preview_dummy_implementations.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
# flags: --preview
22
from typing import NoReturn, Protocol, Union, overload
33

4+
class Empty:
5+
...
46

57
def dummy(a): ...
6-
def other(b): ...
8+
async def other(b): ...
79

810

911
@overload
@@ -48,13 +50,22 @@ def b(arg: Union[int, str, object]) -> Union[int, str]:
4850
raise TypeError
4951
return arg
5052

53+
def has_comment():
54+
... # still a dummy
55+
56+
if some_condition:
57+
...
58+
5159
# output
5260

5361
from typing import NoReturn, Protocol, Union, overload
5462

5563

64+
class Empty: ...
65+
66+
5667
def dummy(a): ...
57-
def other(b): ...
68+
async def other(b): ...
5869

5970

6071
@overload
@@ -98,3 +109,10 @@ def b(arg: Union[int, str, object]) -> Union[int, str]:
98109
if not isinstance(arg, (int, str)):
99110
raise TypeError
100111
return arg
112+
113+
114+
def has_comment(): ... # still a dummy
115+
116+
117+
if some_condition:
118+
...

0 commit comments

Comments
 (0)