Commits

Anonymous committed b43a593

Fixing problems for removing unused imports when functions of the same name exists

Comments (0)

Files changed (5)

docs/workingon.txt

 Move a module or package to another package
 ===========================================
 
-- Moving packages
+- Problems when organizing imports in `ropetests.codeanalyzetest` module
 
 * Refactoring `_Mover` hierarchy
 * Refactor `importutils` module
-* Problems when organizing imports in `ropetests.codeanalyzetest` module
 * Function decorators and renaming attributes of self
 
 

rope/importutils.py

         pyobject = self.pyobject.get_module().get_scope().\
                    get_inner_scope_for_line(node.lineno).pyobject
         visitor = _LocalUnboundNameFinder(pyobject, self)
-        for child in pyobject._get_ast().getChildNodes():
+        for child in node.getChildNodes():
             compiler.walk(child, visitor)
     
     def visitFunction(self, node):

rope/refactor/move.py

             module_with_imports.add_import(new_import)
         return module_with_imports.get_changed_source()
     
+    def _remove_old_pyname_imports(self, pymodule):
+        old_source = pymodule.source_code
+        module_with_imports = self.import_tools.get_module_with_imports(pymodule)
+        def can_select(name):
+            try:
+                if name == self.old_name and \
+                   pymodule.get_attribute(name).get_object() == self.old_pyname.get_object():
+                    return False
+            except rope.exceptions.AttributeNotFoundException:
+                pass
+            return True
+        module_with_imports.filter_names(can_select)
+        new_source = module_with_imports.get_changed_source()
+        if old_source != new_source:
+            return new_source
+        return None
+
 
 class _GlobalMover(_Mover):
     
         
         self.old_name = self.old_pyname.get_object()._get_ast().name
         pymodule = self.old_pyname.get_object().get_module()
-        self.resource = pymodule.get_resource()
+        self.source = pymodule.get_resource()
         self.new_import = self.import_tools.get_import_for_module(
             self.pycore.resource_to_pyobject(self.destination))
-        self.new_imported_name = self.new_import.names_and_aliases[0][0] + '.' + self.old_name
+        self.new_name = self.new_import.names_and_aliases[0][0] + '.' + self.old_name
 
     def _check_exceptional_conditions(self):
         if self.old_pyname is None or \
         return changes
     
     def _change_source_module(self, changes):
-        source = self.resource.read()
+        source = self.source.read()
         uses_moving = False
         # Changing occurances
-        pymodule = self.pycore.resource_to_pyobject(self.resource)
-        source = self._rename_in_module(pymodule, self.new_imported_name)
+        pymodule = self.pycore.resource_to_pyobject(self.source)
+        source = self._rename_in_module(pymodule, self.new_name)
         if source is None:
             source = pymodule.source_code
         else:
         source = source[:start] + source[end + 1:]
         
         if uses_moving:
-            pymodule = self.pycore.get_string_module(source, self.resource)
+            pymodule = self.pycore.get_string_module(source, self.source)
             # Adding new import
             source = self._add_imports_to_module(pymodule, [self.new_import])
         
-        changes.add_change(ChangeFileContents(self.resource, source))
+        changes.add_change(ChangeFileContents(self.source, source))
     
     def _change_destination_module(self, changes):
         source = self.destination.read()
     
     def _get_moving_element_with_imports(self):
         moving = self._get_moving_element()
-        module_with_imports = self._get_module_with_imports(moving, self.resource)
+        module_with_imports = self._get_module_with_imports(moving, self.source)
         for import_info in self._get_used_imports_by_the_moving_element():
             module_with_imports.add_import(import_info)
-        source_pymodule = self.pycore.resource_to_pyobject(self.resource)
+        source_pymodule = self.pycore.resource_to_pyobject(self.source)
         new_import = self.import_tools.get_from_import_for_module(source_pymodule, '*')
         module_with_imports.add_import(new_import)
         
         pymodule = self.pycore.get_string_module(
-            module_with_imports.get_changed_source(), self.resource)
+            module_with_imports.get_changed_source(), self.source)
         source = self.import_tools.transform_relative_imports_to_absolute(pymodule)
         if source is not None:
-            pymodule = self.pycore.get_string_module(source, self.resource)
+            pymodule = self.pycore.get_string_module(source, self.source)
         source = self.import_tools.transform_froms_to_normal_imports(pymodule)
-        module_with_imports = self._get_module_with_imports(source, self.resource)
+        module_with_imports = self._get_module_with_imports(source, self.source)
         imports = [import_stmt.import_info 
                    for import_stmt in module_with_imports.get_import_statements()]
         start = 1
         return self.import_tools.get_module_with_imports(pymodule)
 
     def _get_moving_element(self):
-        lines = rope.codeanalyze.SourceLinesAdapter(self.resource.read())
+        lines = rope.codeanalyze.SourceLinesAdapter(self.source.read())
         scope = self.old_pyname.get_object().get_scope()
         start = lines.get_line_start(scope.get_start())
         end = lines.get_line_end(scope.get_end())
-        moving = self.resource.read()[start:end]
+        moving = self.source.read()[start:end]
         return moving
         
     def _get_used_imports_by_the_moving_element(self):
-        pymodule = self.pycore.resource_to_pyobject(self.resource)
+        pymodule = self.pycore.resource_to_pyobject(self.source)
         module_with_imports = self.import_tools.get_module_with_imports(pymodule)
         return module_with_imports.get_used_imports(self.old_pyname.get_object())
     
     def _change_other_modules(self, changes):
         for file_ in self.pycore.get_python_files():
-            if file_ in (self.resource, self.destination):
+            if file_ in (self.source, self.destination):
                 continue
             # Changing occurances
             pymodule = self.pycore.resource_to_pyobject(file_)
-            source = self._rename_in_module(pymodule, self.new_imported_name)
+            source = self._rename_in_module(pymodule, self.new_name)
             if source is None:
                 continue
             # Removing out of date imports
             pymodule = self.pycore.get_string_module(source, file_)
             source = self._remove_old_pyname_imports(pymodule)
+            if source is None:
+                source = pymodule.source_code
+            else:
+                pymodule = self.pycore.get_string_module(source, file_)
             # Adding new import
-            pymodule = self.pycore.get_string_module(source, file_)
             source = self._add_imports_to_module(pymodule, [self.new_import])
             
             changes.add_change(ChangeFileContents(file_, source))
             new_name, replace_primary=True, imports=False)
         return rename_in_module.get_changed_module(pymodule=pymodule)
     
-    def _remove_old_pyname_imports(self, pymodule):
-        module_with_imports = self.import_tools.get_module_with_imports(pymodule)
-        def can_select(name):
-            try:
-                if name == self.old_name and \
-                   pymodule.get_attribute(name).get_object() == self.old_pyname.get_object():
-                    return False
-            except rope.exceptions.AttributeNotFoundException:
-                pass
-            return True
-        module_with_imports.filter_names(can_select)
-        return module_with_imports.get_changed_source()
-
 
 class _ModuleMover(_Mover):
     
 
     def _change_other_modules(self, changes):
         for module in self.pycore.get_python_files():
+            if module in (self.source, self.destination):
+                continue
             is_changed = False
             should_import = False
             pymodule = self.pycore.resource_to_pyobject(module)
             if is_changed:
                 changes.add_change(ChangeFileContents(module, source))
     
-    def _remove_old_pyname_imports(self, pymodule):
-        old_source = pymodule.source_code
-        module_with_imports = self.import_tools.get_module_with_imports(pymodule)
-        def can_select(name):
-            try:
-                if name == self.old_name and \
-                   pymodule.get_attribute(name).get_object() == self.old_pyname.get_object():
-                    return False
-            except rope.exceptions.AttributeNotFoundException:
-                pass
-            return True
-        module_with_imports.filter_names(can_select)
-        new_source = module_with_imports.get_changed_source()
-        if old_source != new_source:
-            return new_source
-        return None
-

ropetest/codeanalyzetest.py

     def test_function_calls(self):
         word_finder = WordRangeFinder('sample_function()')
         self.assertEquals('sample_function', word_finder.get_statement_at(10))
-    
+
     def test_attribute_accesses(self):
         word_finder = WordRangeFinder('a_var.an_attr')
         self.assertEquals('a_var.an_attr', word_finder.get_primary_at(10))
         word_finder = WordRangeFinder('"a string".split()')
         self.assertEquals('"a string".split', word_finder.get_primary_at(14))
 
-    def test_function_calls(self):
+    def test_function_calls2(self):
         word_finder = WordRangeFinder('file("afile.txt").read()')
         self.assertEquals('file("afile.txt").read',
                           word_finder.get_primary_at(18))
         found_pyname = name_finder.get_pyname_at(code.index('mod1') + 1)
         self.assertEquals(mod1_pyobject, found_pyname.get_object())
 
-    def test_relative_modules_after_from_statements(self):
+    def test_relative_modules_after_from_statements2(self):
         mod1 = self.pycore.create_module(self.project.get_root_folder(), 'mod1')
         pkg1 = self.pycore.create_package(self.project.get_root_folder(), 'pkg1')
         pkg2 = self.pycore.create_package(pkg1, 'pkg2')

ropetest/importutilstest.py

         self.assertEquals('import pkg1.mod1\npkg1.mod1.a_func()',
                           module_with_imports.get_changed_source())
     
+    def test_removing_unused_imports_and_functions_of_the_same_name(self):
+        self.mod.write('def a_func():\n    pass\ndef a_func():\n    pass\n')
+        pymod = self.pycore.get_module('mod')
+        module_with_imports = self.import_tools.get_module_with_imports(pymod)
+        module_with_imports.remove_unused_imports()
+        self.assertEquals('def a_func():\n    pass\ndef a_func():\n    pass\n',
+                          module_with_imports.get_changed_source())
+        
     def test_adding_imports(self):
         self.mod.write('\n')
         pymod = self.pycore.get_module('mod')