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

Conversation

spagh-eddie
Copy link
Contributor

@spagh-eddie spagh-eddie commented Oct 1, 2023

Description

Closes #3756

馃憢 First PR c:

in this section:

black/src/black/trans.py

Lines 940 to 944 in e7c3368

if leaf.type == token.STRING:
string_leaf = Leaf(token.STRING, LL[idx].value)
LL[lpar_or_rpar_idx].remove() # Remove lpar.
replace_child(LL[idx], string_leaf)
new_line.append(string_leaf)

StringParenStripper was getting an input that looks like this:

Line(leaves=[Leaf(LSQB, '['),
             Leaf(LPAR, '('),
             # 猬囷笍 this leaf's ID is the key of the comment
             Leaf(STRING, '"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"'), 
             Leaf(RPAR, ')'),
             Leaf(COMMA, ','),
             Leaf(RSQB, ']')],
     comments={4348688720: [Leaf(COMMENT, '# aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')]},
     ...
)

and outputting this:

Line(leaves=[Leaf(LSQB, '['),
             # 猬囷笍 this is a new leaf with a different ID generated by
             # StringParenStripper, but it didn't update the comment key!
             Leaf(STRING, '"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"'),
             Leaf(COMMA, ','),
             Leaf(RSQB, ']')],
     comments={4348688720: [Leaf(COMMENT, '# aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')]},
     ...
)

Checklist - did you ...

  • Add an entry in CHANGES.md if necessary?
  • Add / update tests if necessary?
  • Add new / update outdated documentation?

@github-actions
Copy link

github-actions bot commented Oct 1, 2023

diff-shades reports zero changes comparing this PR (59e02d3) to main (e7c3368).


What is this? | Workflow run | diff-shades documentation

Since the id of the old leaf may be
the key to comments, the new leaf
must adopt the old comments
Copy link
Collaborator

@JelleZijlstra JelleZijlstra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@JelleZijlstra JelleZijlstra merged commit a91eb73 into psf:main Oct 1, 2023
41 checks passed
@hauntsaninja
Copy link
Collaborator

Nice fix, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

comment next to string wrapped in parentheses in list removed in preview mode
3 participants