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

Only use dummy implementation logic for functions and classes #4066

Merged
merged 6 commits into from
Dec 11, 2023
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
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
- Additional cases of immediately nested tuples, lists, and dictionaries are now
indented less (#4012)
- Fix crash in preview mode when using a short `--line-length` (#4086)
- Keep suites consisting of only an ellipsis on their own lines if they are not
functions or class definitions (#4066)

### Configuration

Expand Down
4 changes: 2 additions & 2 deletions src/black/linegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ def visit_suite(self, node: Node) -> Iterator[Line]:
"""Visit a suite."""
if (
self.mode.is_pyi or Preview.dummy_implementations in self.mode
) and is_stub_suite(node):
) and is_stub_suite(node, self.mode):
yield from self.visit(node.children[2])
else:
yield from self.visit_default(node)
Expand Down Expand Up @@ -314,7 +314,7 @@ def visit_simple_stmt(self, node: Node) -> Iterator[Line]:
if (
not (self.mode.is_pyi or Preview.dummy_implementations in self.mode)
or not node.parent
or not is_stub_suite(node.parent)
or not is_stub_suite(node.parent, self.mode)
):
yield from self.line()
yield from self.visit_default(node)
Expand Down
9 changes: 8 additions & 1 deletion src/black/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -736,8 +736,15 @@ def is_funcdef(node: Node) -> bool:
return node.type == syms.funcdef


def is_stub_suite(node: Node) -> bool:
def is_stub_suite(node: Node, mode: Mode) -> bool:
"""Return True if `node` is a suite with a stub body."""
if node.parent is not None:
if Preview.dummy_implementations in mode and node.parent.type not in (
syms.funcdef,
syms.async_funcdef,
syms.classdef,
):
return False

# If there is a comment, we want to keep it.
if node.prefix.strip():
Expand Down
22 changes: 20 additions & 2 deletions tests/data/cases/preview_dummy_implementations.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# flags: --preview
from typing import NoReturn, Protocol, Union, overload

class Empty:
...

def dummy(a): ...
def other(b): ...
async def other(b): ...


@overload
Expand Down Expand Up @@ -48,13 +50,22 @@ def b(arg: Union[int, str, object]) -> Union[int, str]:
raise TypeError
return arg

def has_comment():
... # still a dummy

if some_condition:
...

# output

from typing import NoReturn, Protocol, Union, overload


class Empty: ...


def dummy(a): ...
def other(b): ...
async def other(b): ...


@overload
Expand Down Expand Up @@ -98,3 +109,10 @@ def b(arg: Union[int, str, object]) -> Union[int, str]:
if not isinstance(arg, (int, str)):
raise TypeError
return arg


def has_comment(): ... # still a dummy


if some_condition:
...