python-rope / rope

a python refactoring library
GNU Lesser General Public License v3.0
1.93k stars 161 forks source link

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

Open bkad opened 6 years ago

bkad commented 6 years ago

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.

soupytwist commented 6 years ago

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