From 06eac466f0c3d477dfc817fa9e3a27bf90916998 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Thu, 28 Dec 2023 17:43:44 -0600 Subject: [PATCH 1/3] Remove empty lines before docstrings in async functions --- src/black/lines.py | 8 +------- src/black/nodes.py | 4 ---- .../cases/preview_allow_empty_first_line.py | 20 +++++++++++++++++++ 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/black/lines.py b/src/black/lines.py index 8d02267a85b..334713bf7b2 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, @@ -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) ) ) 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 3e14fa15250..97539d133fe 100644 --- a/tests/data/cases/preview_allow_empty_first_line.py +++ b/tests/data/cases/preview_allow_empty_first_line.py @@ -62,6 +62,17 @@ def method(self): pass + +async def async_fn(): + + """Docstring.""" + + +@decorated +async def async_fn(): + + """Docstring.""" + # output def foo(): @@ -126,3 +137,12 @@ class Cls: def method(self): pass + + +async def async_fn(): + """Docstring.""" + + +@decorated +async def async_fn(): + """Docstring.""" From e696b366bb0679f5923a3995d8d055603c56eb5a Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Thu, 28 Dec 2023 17:47:20 -0600 Subject: [PATCH 2/3] changes --- CHANGES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.md b/CHANGES.md index a6587cc5ceb..3e17e7304bd 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) ### Configuration From fad3df26ca7481b68ebce7bcbf72958e04af2dc5 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Thu, 28 Dec 2023 19:05:00 -0600 Subject: [PATCH 3/3] use a property --- src/black/lines.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/black/lines.py b/src/black/lines.py index 334713bf7b2..e86185c2279 100644 --- a/src/black/lines.py +++ b/src/black/lines.py @@ -707,7 +707,7 @@ 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) + not current_line.is_docstring or (self.previous_line and not self.previous_line.is_def) ) )