Commits

Anonymous committed 4b3874f

fixed renaming imported names that are aliased

Comments (0)

Files changed (2)

rope/base/codeanalyze.py

             return False
         return self._find_import_pair_end(from_names) >= offset
 
+    def is_from_aliased(self, offset):
+        if not self.is_a_name_after_from_import(offset):
+            return False
+        try:
+            # XXX: what if as is inside multi-line parens
+            end = self._find_word_end(offset)
+            as_ = self._find_word_end(end + 1)
+            if self.source[as_ - 1:as_ + 1] == 'as':
+                return True
+        except ValueError:
+            return False
+
+    def get_from_aliased(self, offset):
+        try:
+            end = self._find_word_end(offset)
+            as_ = self._find_word_end(end + 1)
+            alias = self._find_word_end(as_ + 1)
+            start = self._find_word_start(alias)
+            return self.source[start:alias + 1]
+        except ValueError:
+            pass
+
     def is_function_keyword_parameter(self, offset):
         word_end = self._find_word_end(offset)
         if word_end + 1 == len(self.source):
             module = self.word_finder.get_primary_at(offset)
             module_pyname = self._find_module(module)
             return (None, module_pyname)
-        name = self.word_finder.get_primary_at(offset)
+        if self.word_finder.is_from_aliased(offset):
+            name = self.word_finder.get_from_aliased(offset)
+        else:
+            name = self.word_finder.get_primary_at(offset)
         return evaluate.get_primary_and_pyname_in_scope(holding_scope, name)
 
     def get_enclosing_function(self, offset):

ropetest/codeanalyzetest.py

         found_pyname = name_finder.get_pyname_at(code.index('mod') + 1)
         self.assertEquals(mod_pyobject, found_pyname.get_object())
 
+    def test_renaming_functions_with_from_import_and_parens(self):
+        mod1 = testutils.create_module(self.project, 'mod1')
+        mod1.write('def afunc():\n    pass\n')
+        code = 'from mod1 import (\n    afunc as func)\n'
+        scope = self.pycore.get_string_scope(code)
+        name_finder = ScopeNameFinder(scope.pyobject)
+        mod_pyobject = self.pycore.resource_to_pyobject(mod1)
+        afunc = mod_pyobject.get_attribute('afunc')
+        found_pyname = name_finder.get_pyname_at(code.index('afunc') + 1)
+        self.assertEquals(afunc.get_object(), found_pyname.get_object())
+
     @testutils.run_only_for_25
     def test_relative_modules_after_from_statements(self):
         pkg1 = testutils.create_package(self.project, 'pkg1')