Fix comment handling when parenthesising conditional expressions (#4134)

Fixes #3555
This commit is contained in:
Shantanu 2024-01-01 17:55:11 -08:00 committed by GitHub
parent 4ceed0b958
commit b1d17ef9a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 0 deletions

View File

@ -14,6 +14,7 @@
<!-- Changes that affect Black's preview style --> <!-- Changes that affect Black's preview style -->
- Fix comment handling when parenthesising conditional expressions (#4134)
- Format module docstrings the same as class and function docstrings (#4095) - Format module docstrings the same as class and function docstrings (#4095)
- Fix bug where spaces were not added around parenthesized walruses in subscripts, - Fix bug where spaces were not added around parenthesized walruses in subscripts,
unlike other binary operators (#4109) unlike other binary operators (#4109)

View File

@ -170,8 +170,12 @@ def visit_test(self, node: Node) -> Iterator[Line]:
) )
if not already_parenthesized: if not already_parenthesized:
# Similar to logic in wrap_in_parentheses
lpar = Leaf(token.LPAR, "") lpar = Leaf(token.LPAR, "")
rpar = Leaf(token.RPAR, "") rpar = Leaf(token.RPAR, "")
prefix = node.prefix
node.prefix = ""
lpar.prefix = prefix
node.insert_child(0, lpar) node.insert_child(0, lpar)
node.append_child(rpar) node.append_child(rpar)

View File

@ -67,6 +67,28 @@ def something():
else ValuesListIterable else ValuesListIterable
) )
def foo(wait: bool = True):
# This comment is two
# lines long
# This is only one
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 # output
long_kwargs_single_line = my_function( long_kwargs_single_line = my_function(
@ -159,3 +181,23 @@ def something():
if named if named
else FlatValuesListIterable if flat else ValuesListIterable else FlatValuesListIterable if flat else ValuesListIterable
) )
def foo(wait: bool = True):
# This comment is two
# lines long
# This is only one
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 "",
))