Skip to content

Commit

Permalink
Fix comment handling when parenthesising conditional expressions
Browse files Browse the repository at this point in the history
Fixes psf#3555
  • Loading branch information
hauntsaninja committed Dec 29, 2023
1 parent db9c592 commit b6ea8c2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/black/linegen.py
Expand Up @@ -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)

Expand Down
42 changes: 42 additions & 0 deletions tests/data/cases/conditional_expression.py
Expand Up @@ -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(
Expand Down Expand Up @@ -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 "",
))

0 comments on commit b6ea8c2

Please sign in to comment.