Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix comments getting removed from inside parenthesized strings #3909

Merged
merged 4 commits into from Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Expand Up @@ -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 -->
Expand Down
3 changes: 3 additions & 0 deletions src/black/trans.py
Expand Up @@ -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.

Expand Down
24 changes: 24 additions & 0 deletions tests/data/preview/comments7.py
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
),
]