Commits

Anonymous committed a3b427d

move: back importing underlined names

  • Participants
  • Parent commits 206c326

Comments (0)

Files changed (3)

File rope/refactor/importutils/__init__.py

     def get_from_import(self, resource, name):
         """The from import statement for `name` in `resource`"""
         module_name = get_module_name(self.pycore, resource)
-        return FromImport(module_name, 0, ((name, None),))
+        names = []
+        if isinstance(name, list):
+            names = [(imported, None) for imported in name]
+        else:
+            names = [(name, None),]
+        return FromImport(module_name, 0, tuple(names))
 
     def get_module_imports(self, module, imports_filter=None):
         return module_imports.ModuleImports(self.pycore, module,

File rope/refactor/move.py

 def _get_moving_element_with_imports(pycore, resource, source, imports):
     import_tools = importutils.ImportTools(pycore)
     new_imports = list(imports)
-    new_imports.append(import_tools.get_from_import(resource, '*'))
+    pymodule = pycore.get_string_module(source, resource)
 
-    pymodule = pycore.get_string_module(source, resource)
+    back_names = []
+    for name in pycore.resource_to_pyobject(resource):
+        if name not in pymodule:
+            back_names.append(name)
+    new_imports.append(import_tools.get_from_import(resource, back_names))
+
     source = _add_imports_to_module(import_tools, pymodule, new_imports)
     pymodule = pycore.get_string_module(source, resource)
 
     source = import_tools.relatives_to_absolutes(pymodule)
     pymodule = pycore.get_string_module(source, resource)
-    source = import_tools.expand_stars(pymodule)
+    source = import_tools.organize_imports(pymodule, selfs=False)
     pymodule = pycore.get_string_module(source, resource)
+
+    # extracting imports after changes
     module_with_imports = import_tools.get_module_imports(pymodule)
     imports = [import_stmt.import_info
                for import_stmt in module_with_imports.get_import_statements()]
     if module_with_imports.get_import_statements():
         start = module_with_imports.get_import_statements()[-1].end_line
     lines = codeanalyze.SourceLinesAdapter(source)
+    while start < lines.length() and not lines.get_line(start).strip():
+        start += 1
     moving = source[lines.get_line_start(start):]
     return moving, imports
 

File ropetest/refactor/movetest.py

 
     def test_moving_used_imports_to_destination_module(self):
         self.mod3.write('a_var = 10')
-        self.mod1.write('import mod3\nfrom mod3 import a_var\n' \
-                        'def a_func():\n    print(mod3, a_var)\n')
-        self._move(self.mod1, self.mod1.read().index('a_func') + 1,
-                   self.mod2)
-        self.assertEquals('import mod3\nfrom mod3 import a_var\n\n\n' \
-                          'def a_func():\n    print(mod3, a_var)\n',
-                          self.mod2.read())
+        code = 'import mod3\n' \
+               'from mod3 import a_var\n' \
+               'def a_func():\n' \
+               '    print(mod3, a_var)\n'
+        self.mod1.write(code)
+        self._move(self.mod1, code.index('a_func') + 1, self.mod2)
+        expected = 'import mod3\n' \
+                   'from mod3 import a_var\n\n\n' \
+                   'def a_func():\n    print(mod3, a_var)\n'
+        self.assertEquals(expected, self.mod2.read())
 
     def test_moving_used_names_to_destination_module2(self):
-        self.mod1.write('a_var = 10\n' \
-                        'def a_func():\n    print(a_var)\n')
-        self._move(self.mod1, self.mod1.read().index('a_func') + 1,
-                   self.mod2)
+        code = 'a_var = 10\n' \
+               'def a_func():\n' \
+               '    print(a_var)\n'
+        self.mod1.write(code)
+        self._move(self.mod1, code.index('a_func') + 1, self.mod2)
         self.assertEquals('a_var = 10\n', self.mod1.read())
-        self.assertEquals('from mod1 import a_var\n\n\ndef a_func():\n    print(a_var)\n',
-                          self.mod2.read())
+        expected = 'from mod1 import a_var\n\n\n' \
+                   'def a_func():\n' \
+                   '    print(a_var)\n'
+        self.assertEquals(expected, self.mod2.read())
+
+    def test_moving_used_underlined_names_to_destination_module(self):
+        code = '_var = 10\n' \
+               'def a_func():\n' \
+               '    print(_var)\n'
+        self.mod1.write(code)
+        self._move(self.mod1, code.index('a_func') + 1, self.mod2)
+        expected = 'from mod1 import _var\n\n\n' \
+                   'def a_func():\n' \
+                   '    print(_var)\n'
+        self.assertEquals(expected, self.mod2.read())
 
     def test_moving_and_used_relative_imports(self):
         code = 'import mod5\n' \