facebookincubator / Bowler

Safe code refactoring for modern Python.
https://pybowler.io/
MIT License
1.53k stars 91 forks source link

how to handle moved function? #115

Open GliderGeek opened 4 years ago

GliderGeek commented 4 years ago

first of all thanks for this nice tool.

i was wondering if bowler could be used to handle a moved function/class? maybe if it is not easily done using the current implemented helper functions, but the callbacks make it very flexible and would probably enable a custom implementation i would say?

an example is the following i would like to move the function foo from module1 to module2:

script which should be changed:

# script.py
from module1 import foo
from module1 import bar

The following query correctly rename the foo import, but will also change the bar import:

q = Query('script.py').select_module('module1').rename('module2')
# script.py
from module2 import foo
from module2 import bar  # wrongly renamed

I would like to somehow select the foo function, then return to the module and perform the rename. To illustrate what i would like to achieve (probably not the way to go):

q = Query('script.py').select_module('module1').select_function('foo').back_to_module().rename('module2')
GliderGeek commented 4 years ago

looking at it further i can imagine i have to write my own modify callback? is there an example for a custom modify callback implementation?

def change_module_source(matched_element, captured_elements, filename):
    # perform rename

q = Query('script.py').select_module('module1').select_function('foo').modify(callback=change_module_source)