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

Remove empty lines before docstrings in async functions #4132

Merged
merged 4 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 1 addition & 7 deletions src/black/lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
TEST_DESCENDANTS,
child_towards,
is_docstring,
is_funcdef,
is_import,
is_multiline_string,
is_one_sequence_between,
Expand Down Expand Up @@ -709,12 +708,7 @@ def _maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
Preview.allow_empty_first_line_in_block in current_line.mode
and (
not is_docstring(current_line.leaves[0], current_line.mode)
or (
self.previous_line
and self.previous_line.leaves[0]
and self.previous_line.leaves[0].parent
and not is_funcdef(self.previous_line.leaves[0].parent)
)
or (self.previous_line and not self.previous_line.is_def)
)
)

Expand Down
4 changes: 0 additions & 4 deletions src/black/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -742,10 +742,6 @@ def is_multiline_string(leaf: Leaf) -> bool:
return has_triple_quotes(leaf.value) and "\n" in leaf.value


def is_funcdef(node: Node) -> bool:
return node.type == syms.funcdef


def is_function_or_class(node: Node) -> bool:
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is suspicious since many async defs don't have async_funcdef, but this can be subject for different PR

edit: #4133

return node.type in {syms.funcdef, syms.classdef, syms.async_funcdef}

Expand Down
20 changes: 20 additions & 0 deletions tests/data/cases/preview_allow_empty_first_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ def method(self):

pass


async def async_fn():

"""Docstring."""


@decorated
async def async_fn():

"""Docstring."""

# output

def foo():
Expand Down Expand Up @@ -126,3 +137,12 @@ class Cls:
def method(self):

pass


async def async_fn():
"""Docstring."""


@decorated
async def async_fn():
"""Docstring."""