Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Templates for common refactorings #109

Open
tsoernes opened this issue Sep 23, 2019 · 4 comments
Open

Templates for common refactorings #109

tsoernes opened this issue Sep 23, 2019 · 4 comments

Comments

@tsoernes
Copy link

For example, changing from foo import baz to from bar import baz, a move refactoring seems quite difficult to execute with Bowler, as `select_module('baz¨) also returns usages of . It would be nice to have templates, i.e. functions returning queries, for common refactorings.

@mbanders
Copy link

This would be great. Just a handful of common examples would be very helpful to start.

@Levitanus
Copy link

Also, a bit confused at the very beginning:
for example, here is the diff of two files:

from bowler import Query, Filename

query = Query(
    '.',
    # [lambda name: True if name in ('test.py', 'test2.py') else False],
)
selector = query.select_root('.').select_module('test').select_class('MyClass')
fixer = selector.rename('NewMyClass')
fixer.diff()
--- ./test.py
+++ ./test.py
@@ -1,4 +1,4 @@
-class MyClass:
+class NewMyClass:
 
     def method(self, arg: str) -> int:
         return int(arg)
--- ./test2.py
+++ ./test2.py
@@ -1,7 +1,7 @@
 from test import MyClass as GoodClass
 
 
-class MyClass(GoodClass):
+class NewMyClass(GoodClass):
     pass
 
 
--- ./test2.py
+++ ./test2.py
@@ -11,4 +11,4 @@
 
 cls_ = GoodClass()
 val = cls_.method('43')
-mcls = MyClass()
+mcls = NewMyClass()
[Finished in 0.5s]

I'm confused how to match the class (in the example) across modules, while one-file selection leads to renaming only inside it.

@thatch
Copy link
Contributor

thatch commented Mar 20, 2020

@jreese I think this is a support question for you...

@amyreese
Copy link
Contributor

Unfortunately, you can't chain selectors like that; you either add filters/modifiers after each selector, or the next selector will just override the previous one. This is a limitation of how they are built on top of lib2to3's matching syntax.

Dealing with classes/functions/variables with shared names across multiple modules is where lib2to3 does not make this easy, and Bowler isn't (yet) smart enough to track the origin of each name to know the difference. The most correct way is to use Bowler, and interactively keep or discard individual hunks of the diff based on whether it's referring to the class you want to rename or not. Bowler will at least be sure find all the references to that name for you though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants