From b6ea8c26856f805a3b7cff998709a117636df7ab Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Thu, 28 Dec 2023 19:20:32 -0600 Subject: [PATCH 1/3] Fix comment handling when parenthesising conditional expressions Fixes #3555 --- src/black/linegen.py | 4 +++ tests/data/cases/conditional_expression.py | 42 ++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/src/black/linegen.py b/src/black/linegen.py index 0972cf432e1..427449e77b4 100644 --- a/src/black/linegen.py +++ b/src/black/linegen.py @@ -170,8 +170,12 @@ def visit_test(self, node: Node) -> Iterator[Line]: ) if not already_parenthesized: + # Similar to logic in wrap_in_parentheses lpar = Leaf(token.LPAR, "") rpar = Leaf(token.RPAR, "") + prefix = node.prefix + node.prefix = "" + lpar.prefix = prefix node.insert_child(0, lpar) node.append_child(rpar) diff --git a/tests/data/cases/conditional_expression.py b/tests/data/cases/conditional_expression.py index c30cd76c791..fdbd7ced5c8 100644 --- a/tests/data/cases/conditional_expression.py +++ b/tests/data/cases/conditional_expression.py @@ -67,6 +67,28 @@ def something(): else ValuesListIterable ) + +def foo(wait: bool = True): + # This full comment and the next one after it will get wrapped into + # the next one-liner unexpectedly + + # even if this comment is separated + time.sleep(1) if wait else None + time.sleep(1) if wait else None + + # With newline above + time.sleep(1) if wait else None + # Without newline above + time.sleep(1) if wait else None + + +a = "".join( + ( + "", # comment + "" if True else "", + ) +) + # output long_kwargs_single_line = my_function( @@ -159,3 +181,23 @@ def something(): if named else FlatValuesListIterable if flat else ValuesListIterable ) + + +def foo(wait: bool = True): + # This full comment and the next one after it will get wrapped into + # the next one-liner unexpectedly + + # even if this comment is separated + time.sleep(1) if wait else None + time.sleep(1) if wait else None + + # With newline above + time.sleep(1) if wait else None + # Without newline above + time.sleep(1) if wait else None + + +a = "".join(( + "", # comment + "" if True else "", +)) From 35811db91abcd8fd89cb0748f17bc86913af2412 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Thu, 28 Dec 2023 19:21:44 -0600 Subject: [PATCH 2/3] . --- CHANGES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.md b/CHANGES.md index a6587cc5ceb..38653fb7acf 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -14,6 +14,7 @@ +- Fix comment handling when parenthesising conditional expressions (#4134) - 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) From 9218d9538c8ea75c7b6be981ddd59f4bfbe9573d Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Fri, 29 Dec 2023 02:03:33 -0600 Subject: [PATCH 3/3] . --- tests/data/cases/conditional_expression.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/data/cases/conditional_expression.py b/tests/data/cases/conditional_expression.py index fdbd7ced5c8..76251bd9318 100644 --- a/tests/data/cases/conditional_expression.py +++ b/tests/data/cases/conditional_expression.py @@ -69,10 +69,10 @@ def something(): def foo(wait: bool = True): - # This full comment and the next one after it will get wrapped into - # the next one-liner unexpectedly + # This comment is two + # lines long - # even if this comment is separated + # This is only one time.sleep(1) if wait else None time.sleep(1) if wait else None @@ -184,10 +184,10 @@ def something(): def foo(wait: bool = True): - # This full comment and the next one after it will get wrapped into - # the next one-liner unexpectedly + # This comment is two + # lines long - # even if this comment is separated + # This is only one time.sleep(1) if wait else None time.sleep(1) if wait else None