rope icon indicating copy to clipboard operation
rope copied to clipboard

ImportOrganizer incorrectly determines unbound names in class contexts that inherit from a super class with attributes that have the same name

Open bkad opened this issue 6 years ago • 1 comments

To repro have these files:

main.py

from rope.refactor import ImportOrganizer
from rope.base.project import Project

def main(filename):
    project = Project(".")
    resource = project.get_resource(filename)
    organizer = ImportOrganizer(project)
    changes = organizer.organize_imports(resource)
    print(changes and changes.get_description())

if __name__ == "__main__":
    main("refactor_this.py")

refactor_this.py

from foo import bar

class Parent:
    bar = 1

class RefactorMe(Parent):
    baz = bar

foo.py

bar = 10

Then run main.py. It makes the following suggestion:

--- a/refactor_this.py
+++ b/refactor_this.py
@@ -1,5 +1,3 @@
-from foo import bar
-
 class Parent:
     bar = 1

Which would cause running the file to fail with a NameError. It seems like unbound name detection relies on the Class scope which includes all attributes set by the super class which is the wrong set of names to be used by the ImportOrganizer.

bkad avatar Jun 18 '18 23:06 bkad

Thanks for reporting. This might be a bit of a tricky one to track down.

soupytwist avatar Jun 25 '18 17:06 soupytwist