Can Bowler rename the variable in the format string literals?
See original GitHub issueHi.
I have some code that uses format string literals and .format()
like below:
def foo(bar, zar):
print(f'bar is {bar}, not {zar}')
print('bar is {bar}, not {zar}'.format(bar=bar, zar=zar))
I want to rename the variable name bar
to bar_new
so the rename.py
script is ready:
from bowler import Query
Query('.').select_var('bar').rename('bar_new').idiff()
I hope Bowler recognize the placeholder in string literal, {bar}
, but it wasn’t:
--- ./format_string.py
+++ ./format_string.py
@@ -1,3 +1,3 @@
-def foo(bar, zar):
+def foo(bar_new, zar):
print(f'bar is {bar}, not {zar}')
- print('bar is {bar}, not {zar}'.format(bar=bar, zar=zar))
+ print('bar is {bar}, not {zar}'.format(bar_new=bar_new, zar=zar))
Apply this hunk [y,N,q,a,d,?]?
I want Bowler to rename
f'bar is {bar}, not {zar}'
→f'bar is {bar_new}, not {zar}'
.format(bar=bar, zar=zar)
→'.format(bar=bar_new, zar=zar)
and this is what exactly the Refactor → Rename function does in PyCharm.
Is there a way to refactor my code like this? Thank you for your great tool.
Issue Analytics
- State:
- Created 4 years ago
- Comments:11 (3 by maintainers)
Top Results From Across the Web
Renaming a captured variable in a formatted string doesn't work
I can't rename the world variable in the formatted string. When I try to use Rename Symbol in VSCode on it, it gives...
Read more >Rename dialog for a variable - GoLand - JetBrains
Use this dialog to rename a class or an interface. In addition to renaming the class or the interface itself, GoLand can also...
Read more >Python: Formatting a string using variable names placeholders
I find this method quite confusing, but I don't know any way to construct a string using in-line local variables. How can I...
Read more >first name bunch of numbers
First Name Bunch Of Numbers, de (moves cursor to start of the line, finds the first ". In essence, the FIND function gets...
Read more >4.1. Renaming Elements - Eclipse Cookbook [Book] - O'Reilly
To rename all uses of this msg variable, highlight the variable and select Refactor→ Rename, or right-click the variable and select Refactor→ Rename,...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The first part – renaming the variable within an f-string – is not currently supported, but I would be glad to review a PR if you feel like adding it. @jreese is that second part a bug, renaming the kwarg in a call? The docs are unclear at https://pybowler.io/docs/api-selectors#select-var
@sangwoo-joh Appreciate it. ❤️