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 all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- Format module docstrings the same as class and function docstrings (#4095)
- Fix bug where spaces were not added around parenthesized walruses in subscripts,
unlike other binary operators (#4109)
- 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)

Expand Down
10 changes: 2 additions & 8 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 @@ -708,13 +707,8 @@ def _maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
is_empty_first_line_ok = (
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)
)
not current_line.is_docstring
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

@hauntsaninja hauntsaninja Dec 28, 2023

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 @@ -63,6 +63,17 @@ def method(self):
pass


async def async_fn():

"""Docstring."""


@decorated
async def async_fn():

"""Docstring."""


def top_level(
a: int,
b: str,
Expand Down Expand Up @@ -138,6 +149,15 @@ def method(self):
pass


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


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


def top_level(
a: int,
b: str,
Expand Down