diff --git a/CHANGES.md b/CHANGES.md index fca88612afe..e520544dceb 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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) diff --git a/src/black/lines.py b/src/black/lines.py index 4d4f47a44e8..0c3f1666d1f 100644 --- a/src/black/lines.py +++ b/src/black/lines.py @@ -24,7 +24,6 @@ TEST_DESCENDANTS, child_towards, is_docstring, - is_funcdef, is_import, is_multiline_string, is_one_sequence_between, @@ -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) ) ) diff --git a/src/black/nodes.py b/src/black/nodes.py index 8e0f27e3ded..d45d40c01c2 100644 --- a/src/black/nodes.py +++ b/src/black/nodes.py @@ -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} diff --git a/tests/data/cases/preview_allow_empty_first_line.py b/tests/data/cases/preview_allow_empty_first_line.py index daf78344ad7..4269987305d 100644 --- a/tests/data/cases/preview_allow_empty_first_line.py +++ b/tests/data/cases/preview_allow_empty_first_line.py @@ -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, @@ -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,