Commits

Anonymous committed 5535464

adding instance argument to default restructuring wildcard

Comments (0)

Files changed (4)

 ===========
 
 
+- Checking isinstance in restructurings : January 11, 2008
+
+
 - Better handling of one-liners : January 10, 2008
 
 

rope/refactor/restructure.py

 
       pattern ${pyobject}.get_attribute(${name})
       goal ${pyobject}[${name}]
-      args pyobject: type=rope.base.pyobjects.PyObject
+      args pyobject: instance=rope.base.pyobjects.PyObject
 
     Example #2::
 
       pattern ${name} in ${pyobject}.get_attributes()
       goal ${name} in {pyobject}
-      args pyobject: type=rope.base.pyobjects.PyObject
+      args pyobject: instance=rope.base.pyobjects.PyObject
 
     Example #3::
 

rope/refactor/wildcards.py

-from rope.base import ast, evaluate, builtins
+from rope.base import ast, evaluate, builtins, pyobjects
 from rope.refactor import patchedast, occurrences
 
 
                 return False
         kind = None
         expected = None
-        for check in ['name', 'object', 'type']:
+        for check in ['name', 'object', 'type', 'instance']:
             if check in args:
                 kind = check
                 expected = args[check]
             return self._same_pyname(self.expected, pyname)
         else:
             pyobject = pyname.get_object()
+            if self.kind == 'object':
+                objects = [pyobject]
             if self.kind == 'type':
-                pyobject = pyobject.get_type()
-            return self._same_pyobject(self.expected.get_object(), pyobject)
+                objects = [pyobject.get_type()]
+            if self.kind == 'instance':
+                objects = [pyobject]
+                objects.extend(self._get_super_classes(pyobject))
+                objects.extend(self._get_super_classes(pyobject.get_type()))
+            for pyobject in objects:
+                if self._same_pyobject(self.expected.get_object(), pyobject):
+                    return True
+            return False
+
+    def _get_super_classes(self, pyobject):
+        result = []
+        if isinstance(pyobject, pyobjects.AbstractClass):
+            for superclass in pyobject.get_superclasses():
+                result.append(superclass)
+                result.extend(self._get_super_classes(superclass))
+        return result
 
     def _same_pyobject(self, expected, pyobject):
         return expected == pyobject

ropetest/refactor/similarfindertest.py

                                          {'a': 'type=mod1.A'}))
         self.assertEquals(1, len(result))
 
+    def test_checking_instance_of_an_ass_name_node(self):
+        self.mod1.write('class A(object):\n    pass\n'
+                        'class B(A):\n    pass\nb = B()\n')
+        pymodule = self.pycore.resource_to_pyobject(self.mod1)
+        finder = similarfinder.SimilarFinder(pymodule)
+        result = list(finder.get_matches('${a} = ${assigned}',
+                                         {'a': 'instance=mod1.A'}))
+        self.assertEquals(1, len(result))
+
     def test_checking_equality_of_imported_pynames(self):
         mod2 = testutils.create_module(self.project, 'mod2')
         mod2.write('class A(object):\n    pass\n')