Skip to content

Commit

Permalink
Remove empty lines before docstrings in async functions (#4132)
Browse files Browse the repository at this point in the history
  • Loading branch information
hauntsaninja committed Jan 2, 2024
1 parent b9ad4da commit 8e0a9de
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
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:
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

0 comments on commit 8e0a9de

Please sign in to comment.