Check for ast.Attributes when finding occurrences in f-strings#751
Merged
lieryan merged 5 commits intopython-rope:masterfrom Mar 5, 2024
Merged
Check for ast.Attributes when finding occurrences in f-strings#751lieryan merged 5 commits intopython-rope:masterfrom
lieryan merged 5 commits intopython-rope:masterfrom
Conversation
Author
|
Hello, any updates on this? Is there anything else I should do? |
Member
|
Thanks for the contribution @sandratsy, this is great. I've added some test cases and type hints but the fix looks sensible. |
Member
|
@all-contributors please add @sandratsy for code |
Contributor
|
I've put up a pull request to add @sandratsy! 🎉 |
Author
|
Thanks v much! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Given the following piece of code:
If I try to Rename attribute/property
abc, theabcin the return statement is correctly renamed, while theabcin the f-string is not. This is because_search_in_f_string()only looks for ast.Names, whileself.abcis an ast.Attribute. I have updated_search_in_f_string()to look for ast.Attributes as well.(Ofc, this is not fool-proof, since devs can put whatever they like in f-strings, but I suppose devs are more likely to put Names and Attributes in f-strings than entire function calls.)
The
node.col_offsetfor an ast.Attribute points to the start ofself.abcrather thanabc, however. To get around this, I've taken the liberty to change_search_in_f_string()to return an offset instead of the node.node.end_col_offset - len(self.name)will correctly return the index ofabc.I used
node.end_col_offset - len(self.name)instead ofnode.col_offset + f_string.index(self.name)in case the attribute is namedabc.abc. We want the offset of the 2ndabc, not the 1st.Checklist (delete if not relevant):