rope icon indicating copy to clipboard operation
rope copied to clipboard

Rename a python class name with a script

Open MichaelSchneeberger opened this issue 2 years ago • 0 comments

Describe the bug Renaming a python class with rope.refactor.rename does not update the import statement in other files of the project.

To Reproduce Steps to reproduce the behavior:

  1. Code before refactoring:

Given the following the project structure

proja
-- __init__.py
-- city.py
-- citycapitol.py

city.py

class City:
    pass

citycapitol.py

from pcka.city import City

class CityCapitol(City):
    pass
  1. Describe the refactoring you want to do
import rope.refactor.rename
import rope.refactor.restructure

proj = rope.base.project.Project('pcka')
res = proj.get_module('city').get_resource()
offset = res.read().index(f'class City') + 6

change = rope.refactor.rename.Rename(
    proj, 
    res, 
    offset,
).get_changes('City2')
print(change.get_description())
  1. Expected code after refactoring:

city.py

class City2:
    pass

citycapitol.py

from pcka.city import City2

class CityCapitol(City2):
    pass
  1. Describe the error or unexpected result that you are getting

The import statement in citycapitol.py does not get adapted.

citycapitol.py

from pcka.city import City

class CityCapitol(City):
    pass

Screenshots

The change.get_description() returns the following

Renaming <City> to <City2>:

--- a/city.py
+++ b/city.py
@@ -1,2 +1,2 @@
-class City:
+class City2:
     pass

Editor information (please complete the following information):

  • Project Python version: 3.10.0
  • Rope Python version: 0.23.0

MichaelSchneeberger avatar Mar 29 '22 09:03 MichaelSchneeberger