Commits

Anonymous committed d9d1377

extract: reporting extracting a method from a method with no args

Comments (0)

Files changed (2)

rope/refactor/extract.py

     def _get_function_signature(self, args):
         args = list(args)
         prefix = ''
-        if self.info.method and not self.info.make_global and \
-                _get_function_kind(self.info.scope) == 'method':
+        if self._extracting_method():
             self_name = self._get_self_name()
+            if self_name is None:
+                raise RefactoringError('Extracting a method from a function '
+                                       'with no self argument.')
             if self_name in args:
                 args.remove(self_name)
             args.insert(0, self_name)
         return prefix + self.info.new_name + \
                '(%s)' % self._get_comma_form(args)
 
+    def _extracting_method(self):
+        return self.info.method and not self.info.make_global and \
+               _get_function_kind(self.info.scope) == 'method'
+
     def _get_self_name(self):
         param_names = self.info.scope.pyobject.get_param_names()
         if param_names:

ropetest/refactor/extracttest.py

                    '        print(self)\n'
         self.assertEquals(expected, refactored)
 
+    @testutils.assert_raises(rope.base.exceptions.RefactoringError)
+    def test_extract_method_with_no_self_as_argument(self):
+        code = 'class AClass(object):\n' \
+               '    def a_func():\n' \
+               '        print(1)\n'
+        start, end = self._convert_line_range_to_offset(code, 3, 3)
+        refactored = self.do_extract_method(code, start, end, 'new_func')
+
     def test_extract_method_with_multiple_methods(self):
         code = 'class AClass(object):\n' \
                '    def a_func(self):\n' \
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.