question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can Bowler rename the variable in the format string literals?

See original GitHub issue

Hi. 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:open
  • Created 4 years ago
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
thatchcommented, May 28, 2019

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

1reaction
Omar-Elrefaeicommented, Aug 27, 2020

@sangwoo-joh Appreciate it. ❤️

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found