Check for ast.Attributes when finding occurrences in f-strings #751
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
, theabc
in the return statement is correctly renamed, while theabc
in the f-string is not. This is because_search_in_f_string()
only looks for ast.Names, whileself.abc
is 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_offset
for an ast.Attribute points to the start ofself.abc
rather 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):