Skip to content

Commit

Permalink
Fix comments getting removed from inside parenthesized strings (#3909)
Browse files Browse the repository at this point in the history
Since the id of the old leaf may be
the key to comments, the new leaf
must adopt the old comments
  • Loading branch information
spagh-eddie authored Oct 1, 2023
1 parent e7c3368 commit a91eb73
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
),
]

0 comments on commit a91eb73

Please sign in to comment.