rope icon indicating copy to clipboard operation
rope copied to clipboard

Keep from xx import yy style when moving

Open hirorogithub opened this issue 2 years ago • 0 comments

When I try to use rope as a libaray to move module A.C to B.C, I found that code like "from A.C import foo;foo()" will be changed to "A.C.foo()", which will extremely enlarge my code when foo is in modulde like "from A.C.D.E.G"

I want to keep my origin style like "from B.C import foo;foo()", which only change import statment

After single debug and read the source code, I think the core control of such case may in rope.refactor.move,replace from NormalImport to FromImport may works?

I actually just attempted to make the following modifications :

# in https://github.com/python-rope/rope/blob/master/rope/refactor/move.py#L553
    def _new_import(self, dest):
        # origin code
        # return importutils.NormalImport([(self._new_modname(dest), None)])
        # What I changed
        return importutils.FromImport(libutils.modname(dest),0, [(self.old_name, None)])
# in https://github.com/python-rope/rope/blob/master/rope/refactor/move.py#L571
        # origin code
        # new_name = self._new_modname(dest)
        # What I changed
        new_name = self.old_name

when I run it on my project, it seems work only when src module`s deepth is 1,when src module is like A.C move will change the code in wrong way like:

a.py
import A.C

move A.C -> B.D.C

a.py
import C
from B.D import C

May I request the project team to include this feature as a new feature in the project? Thank you for your work and effort.

hirorogithub avatar Jun 02 '23 07:06 hirorogithub