Commits

Anonymous committed 5b4a9ee

inline: removing old froms in inline method

Comments (0)

Files changed (2)

rope/refactor/inline.py

                 if result is not None:
                     result = _add_imports(self.pycore, result,
                                           file, self.imports)
+                    result = _remove_from(self.pycore, self.pyname,
+                                          result, file)
                     changes.add_change(ChangeContents(file, result))
             job_set.finished_job()
         return changes
                                            self.pyname, imports=False)
         changed = rename.rename_in_module(
             finder, self.imported, resource=resource, replace_primary=True)
+        if changed:
+            changed = _remove_from(self.pycore, self.pyname, changed, resource)
         return changed
 
     def get_kind(self):
 
     def occurred_outside_skip(self, change_collector, occurrence):
         start, end = occurrence.get_primary_range()
+        # we remove out of date imports later
+        if occurrence.is_in_import_statement():
+            return
+        # the function is referenced outside an import statement
         if not occurrence.is_called():
             raise rope.base.exceptions.RefactoringError(
                 'Reference to inlining function other than function call'
     if isinstance(pyname, pynames.ImportedName):
         pyname = pyname._get_imported_pyname()
     return pyname
+
+def _remove_from(pycore, pyname, source, resource):
+    pymodule = pycore.get_string_module(source, resource)
+    module_import = importutils.get_module_imports(pycore, pymodule)
+    module_import.remove_pyname(pyname)
+    return module_import.get_changed_source()

ropetest/refactor/inlinetest.py

                    'print(sys.argv)\n'
         self.assertEquals(expected, self.mod2.read())
 
+    def test_inlining_variables_and_removing_old_froms(self):
+        self.mod.write('var = 1\n')
+        self.mod2.write('from mod import var\nprint(var)\n')
+        self._inline2(self.mod2, self.mod2.read().rindex('var'))
+        self.assertEquals('print(1)\n', self.mod2.read())
+
+    def test_inlining_method_and_removing_old_froms(self):
+        self.mod.write('def f():    return 1\n')
+        self.mod2.write('from mod import f\nprint(f())\n')
+        self._inline2(self.mod2, self.mod2.read().rindex('f'))
+        self.assertEquals('print(1)\n', self.mod2.read())
+
 
 def suite():
     result = unittest.TestSuite()