Fix comments getting removed from inside parenthesized strings (#3909)

Since the id of the old leaf may be
the key to comments, the new leaf
must adopt the old comments
This commit is contained in:
Eddie Darling 2023-10-01 15:35:42 -07:00 committed by GitHub
parent e7c3368c13
commit a91eb73064
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 0 deletions

View File

@ -17,6 +17,8 @@
<!-- Changes that affect Black's preview style -->
- Fix comments getting removed from inside parenthesized strings (#3909)
### Configuration
<!-- Changes to how Black can be configured -->

View File

@ -942,6 +942,9 @@ def _transform_to_new_line(
LL[lpar_or_rpar_idx].remove() # Remove lpar.
replace_child(LL[idx], string_leaf)
new_line.append(string_leaf)
# replace comments
old_comments = new_line.comments.pop(id(LL[idx]), [])
new_line.comments.setdefault(id(string_leaf), []).extend(old_comments)
else:
LL[lpar_or_rpar_idx].remove() # This is a rpar.

View File

@ -131,6 +131,18 @@ def test_fails_invalid_post_data(
square = Square(4) # type: Optional[Square]
# Regression test for https://github.com/psf/black/issues/3756.
[
(
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" # aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
),
]
[
( # aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" # aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
),
]
# output
from .config import (
@ -282,3 +294,15 @@ def test_fails_invalid_post_data(
square = Square(4) # type: Optional[Square]
# Regression test for https://github.com/psf/black/issues/3756.
[
( # aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
),
]
[
( # aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" # aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
),
]