From 8e0a9dee1bc5afa1ac089e1cffe2b4417f9a719c Mon Sep 17 00:00:00 2001 From: Shantanu <12621235+hauntsaninja@users.noreply.github.com> Date: Mon, 1 Jan 2024 16:56:39 -0800 Subject: [PATCH] Remove empty lines before docstrings in async functions (#4132) --- CHANGES.md | 1 + src/black/lines.py | 10 ++-------- src/black/nodes.py | 4 ---- .../cases/preview_allow_empty_first_line.py | 20 +++++++++++++++++++ 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 360319a..0e3ba58 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 4d4f47a..0c3f166 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 8e0f27e..d45d40c 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 daf7834..4269987 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,