diff options
| author | Claudiu Popa <pcmanticore@gmail.com> | 2016-02-25 00:16:45 +0000 |
|---|---|---|
| committer | Claudiu Popa <pcmanticore@gmail.com> | 2016-02-25 00:16:45 +0000 |
| commit | 53aca0ec839637e63d0a0c537a62597d066bce47 (patch) | |
| tree | 6e6b49ca9c65cfb6a7c694cc69a096191c76c302 | |
| parent | 04765ad0756196f43e06efecb0e2688da3d53754 (diff) | |
| download | astroid-git-53aca0ec839637e63d0a0c537a62597d066bce47.tar.gz | |
Unbound methods don't occur anymore on Python 3
Instead, we're inferring FunctionDefs where an unbound access
to a method was found.
Close #257
| -rw-r--r-- | ChangeLog | 6 | ||||
| -rw-r--r-- | astroid/interpreter/objectmodel.py | 14 | ||||
| -rw-r--r-- | astroid/interpreter/objects.py | 30 | ||||
| -rw-r--r-- | astroid/tests/unittest_inference.py | 11 | ||||
| -rw-r--r-- | astroid/tests/unittest_object_model.py | 8 | ||||
| -rw-r--r-- | astroid/tests/unittest_objects.py | 58 | ||||
| -rw-r--r-- | astroid/tests/unittest_scoped_nodes.py | 4 | ||||
| -rw-r--r-- | astroid/tree/scoped_nodes.py | 11 |
8 files changed, 107 insertions, 35 deletions
@@ -3,6 +3,12 @@ Change log for the astroid package (used to be astng) -- + * Unbound methods don't occur anymore on Python 3 + + Instead, we're inferring FunctionDefs where an unbound access + to a method was found. + Closes issue #257 + * wildcard_imported_names() got replaced by public_names() Our understanding of wildcard imports through __all__ was diff --git a/astroid/interpreter/objectmodel.py b/astroid/interpreter/objectmodel.py index ac38275e..7c32e6fc 100644 --- a/astroid/interpreter/objectmodel.py +++ b/astroid/interpreter/objectmodel.py @@ -212,6 +212,10 @@ class ModuleModel(ObjectModel): class FunctionModel(ObjectModel): @property + def py__class__(self): + return util.object_type(self._instance) + + @property def py__name__(self): return node_classes.Const(value=self._instance.name, parent=self._instance) @@ -305,8 +309,7 @@ class FunctionModel(ObjectModel): func.decorators, func.returns) # Build a proper bound method that points to our newly built function. - proxy = objects.UnboundMethod(new_func) - yield objects.BoundMethod(proxy=proxy, bound=cls) + yield objects.BoundMethod(proxy=new_func, bound=cls) return DescriptorBoundMethod(proxy=self._instance, bound=self._instance) @@ -333,7 +336,6 @@ class FunctionModel(ObjectModel): py__init__ = py__ne__ py__dir__ = py__ne__ py__call__ = py__ne__ - py__class__ = py__ne__ py__closure__ = py__ne__ py__code__ = py__ne__ @@ -462,8 +464,12 @@ class UnboundMethodModel(ObjectModel): class BoundMethodModel(FunctionModel): @property + def py__class__(self): + return util.object_type(self._instance) + + @property def py__func__(self): - return self._instance._proxied._proxied + return self._instance._proxied @property def py__self__(self): diff --git a/astroid/interpreter/objects.py b/astroid/interpreter/objects.py index 00ee945b..383ea8ac 100644 --- a/astroid/interpreter/objects.py +++ b/astroid/interpreter/objects.py @@ -175,7 +175,7 @@ class BaseInstance(Proxy): for inferred in attr.infer_call_result(self, context): yield inferred else: - yield BoundMethod(attr, self) + yield BoundMethod(attr._proxied, self) elif hasattr(attr, 'name') and attr.name == '<lambda>': # This is a lambda function defined at class level, # since its scope is the underlying _proxied class. @@ -278,11 +278,7 @@ class Instance(BaseInstance): node=self, index=index, context=context)) -@util.register_implementation(runtimeabc.UnboundMethod) -class UnboundMethod(Proxy): - """a special node representing a method not bound to an instance""" - - special_attributes = util.lazy_descriptor(lambda: objectmodel.UnboundMethodModel()) +class Method(Proxy): def __repr__(self): frame = self._proxied.parent.frame() @@ -290,9 +286,6 @@ class UnboundMethod(Proxy): self._proxied.name, frame.qname(), id(self)) - def is_bound(self): - return False - def getattr(self, name, context=None): if name in self.special_attributes: return [self.special_attributes.lookup(name)] @@ -303,6 +296,16 @@ class UnboundMethod(Proxy): return iter((self.special_attributes.lookup(name), )) return self._proxied.igetattr(name, context) + def bool_value(self): + return True + + +@util.register_implementation(runtimeabc.UnboundMethod) +class UnboundMethod(Method): + """a special node representing a method not bound to an instance""" + + special_attributes = util.lazy_descriptor(lambda: objectmodel.UnboundMethodModel()) + def infer_call_result(self, caller, context): # If we're unbound method __new__ of builtin object, the result is an # instance of the class given as first argument. @@ -312,15 +315,13 @@ class UnboundMethod(Proxy): return ((x is util.Uninferable and x or Instance(x)) for x in infer) return self._proxied.infer_call_result(caller, context) - def bool_value(self): - return True + def is_bound(self): + return False @util.register_implementation(runtimeabc.BoundMethod) class BoundMethod(UnboundMethod): """a special node representing a method bound to an instance""" - # __func__ and __self__ are method-only special attributes, the - # rest are general function special attributes. special_attributes = util.lazy_descriptor(lambda: objectmodel.BoundMethodModel()) @@ -338,9 +339,6 @@ class BoundMethod(UnboundMethod): context.boundnode = self.bound return super(BoundMethod, self).infer_call_result(caller, context) - def bool_value(self): - return True - @util.register_implementation(runtimeabc.Generator) class Generator(BaseInstance): diff --git a/astroid/tests/unittest_inference.py b/astroid/tests/unittest_inference.py index 328b813b..fbfaddc8 100644 --- a/astroid/tests/unittest_inference.py +++ b/astroid/tests/unittest_inference.py @@ -293,7 +293,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): def test_unbound_method_inference(self): inferred = self.ast['m_unbound'].infer() meth1 = next(inferred) - self.assertIsInstance(meth1, UnboundMethod) + self.assertIsInstance(meth1, UnboundMethod if six.PY2 else nodes.FunctionDef) self.assertEqual(meth1.name, 'meth1') self.assertEqual(meth1.parent.frame().name, 'C') self.assertRaises(StopIteration, partial(next, inferred)) @@ -1223,6 +1223,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(propinferred.name, 'SendMailController') self.assertEqual(propinferred.root().name, __name__) + @unittest.skipUnless(six.PY2, "Tests UnboundMethod's im_func, which don't exist in Python 3.") def test_im_func_unwrap(self): code = ''' class EnvBasedTC: @@ -1449,7 +1450,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): callfunc = next(ast.nodes_of_class(nodes.Call)) func = callfunc.func inferred = func.inferred()[0] - self.assertIsInstance(inferred, UnboundMethod) + self.assertIsInstance(inferred, UnboundMethod if six.PY2 else nodes.FunctionDef) def test_instance_binary_operations(self): code = """ @@ -3285,7 +3286,7 @@ class GetattrTest(unittest.TestCase): self.assertEqual(first.bound.name, 'A') second = next(ast_nodes[1].infer()) - self.assertIsInstance(second, UnboundMethod) + self.assertIsInstance(second, UnboundMethod if six.PY2 else nodes.FunctionDef) self.assertIsInstance(second.parent, nodes.ClassDef) self.assertEqual(second.parent.name, 'A') @@ -3293,12 +3294,12 @@ class GetattrTest(unittest.TestCase): self.assertIsInstance(third, BoundMethod) # Bound to E, but the provider is B. self.assertEqual(third.bound.name, 'E') - self.assertEqual(third._proxied._proxied.parent.name, 'B') + self.assertEqual(third.parent.name, 'B') fourth = next(ast_nodes[3].infer()) self.assertIsInstance(fourth, BoundMethod) self.assertEqual(fourth.bound.name, 'E') - self.assertEqual(third._proxied._proxied.parent.name, 'B') + self.assertEqual(third.parent.name, 'B') fifth = next(ast_nodes[4].infer()) self.assertIsInstance(fifth, BoundMethod) diff --git a/astroid/tests/unittest_object_model.py b/astroid/tests/unittest_object_model.py index af4bd1a6..a058c998 100644 --- a/astroid/tests/unittest_object_model.py +++ b/astroid/tests/unittest_object_model.py @@ -104,6 +104,7 @@ class BoundMethodModelTest(unittest.TestCase): class UnboundMethodModelTest(unittest.TestCase): + @unittest.skipUnless(six.PY2, "Unbound methods are available in Python 2 only.") def test_unbound_method_model(self): ast_nodes = test_utils.extract_node(''' class A: @@ -119,10 +120,7 @@ class UnboundMethodModelTest(unittest.TestCase): cls = next(ast_nodes[0].infer()) self.assertIsInstance(cls, astroid.ClassDef) - if six.PY2: - unbound = BUILTINS.locals[types.MethodType.__name__][0] - else: - unbound = BUILTINS.locals[types.FunctionType.__name__][0] + unbound = BUILTINS.locals[types.MethodType.__name__][0] self.assertIs(cls, unbound) func = next(ast_nodes[1].infer()) @@ -286,7 +284,7 @@ class FunctionModelTest(unittest.TestCase): ''') bound = next(bound.infer()) self.assertIsInstance(bound, astroid.BoundMethod) - self.assertEqual(bound._proxied._proxied.name, 'test') + self.assertEqual(bound.name, 'test') result = next(result.infer()) self.assertIsInstance(result, astroid.Const) self.assertEqual(result.value, 42) diff --git a/astroid/tests/unittest_objects.py b/astroid/tests/unittest_objects.py index 1f5d0780..afcf76d3 100644 --- a/astroid/tests/unittest_objects.py +++ b/astroid/tests/unittest_objects.py @@ -518,5 +518,63 @@ class SuperTests(unittest.TestCase): self.assertEqual(inferred.value, 42)
+class MethodTest(unittest.TestCase):
+
+ def test_unbound_function_method_difference(self):
+ node = test_utils.extract_node('''
+ class A:
+ def test(self): pass
+ A.test
+ ''')
+ inferred = next(node.infer())
+ if six.PY2:
+ self.assertIsInstance(inferred, objects.UnboundMethod)
+ else:
+ self.assertIsInstance(inferred, nodes.FunctionDef)
+
+ def test_unbound_function_from_classmethods(self):
+ node = test_utils.extract_node('''
+ class A:
+ @classmethod
+ def test(cls): return cls.b
+ def b(self): return self
+ A.test()
+ ''')
+ inferred = next(node.infer())
+ if six.PY2:
+ self.assertIsInstance(inferred, objects.UnboundMethod)
+ else:
+ self.assertIsInstance(inferred, nodes.FunctionDef)
+
+ def test_static_method(self):
+ node = test_utils.extract_node('''
+ class A:
+ @staticmethod
+ def test(self): pass
+ A.test
+ ''')
+ inferred = next(node.infer())
+ self.assertIsInstance(inferred, nodes.FunctionDef)
+
+ @unittest.skipUnless(six.PY2, "Unbound methods are available only on Python 2")
+ def test_underlying_proxied_unbound_method(self):
+ node = test_utils.extract_node('''
+ class A:
+ def test(self): pass
+ A.test
+ ''')
+ inferred = next(node.infer())
+ self.assertIsInstance(inferred._proxied, nodes.FunctionDef)
+
+ def test_underlying_proxied_bound_method(self):
+ node = test_utils.extract_node('''
+ class A:
+ def test(self): pass
+ A().test
+ ''')
+ inferred = next(node.infer())
+ self.assertIsInstance(inferred._proxied, nodes.FunctionDef)
+
+
if __name__ == '__main__':
unittest.main()
diff --git a/astroid/tests/unittest_scoped_nodes.py b/astroid/tests/unittest_scoped_nodes.py index ef66ad1a..f9c73a8c 100644 --- a/astroid/tests/unittest_scoped_nodes.py +++ b/astroid/tests/unittest_scoped_nodes.py @@ -876,7 +876,7 @@ class ClassNodeTest(ModuleLoader, unittest.TestCase): for method in ('m1', 'm2', 'm3'): inferred = list(cls.igetattr(method)) self.assertEqual(len(inferred), 1) - self.assertIsInstance(inferred[0], UnboundMethod) + self.assertIsInstance(inferred[0], UnboundMethod if six.PY2 else nodes.FunctionDef) inferred = list(Instance(cls).igetattr(method)) self.assertEqual(len(inferred), 1) self.assertIsInstance(inferred[0], BoundMethod) @@ -1489,7 +1489,7 @@ class ClassNodeTest(ModuleLoader, unittest.TestCase): # from the class that uses the metaclass, the value # of the property property_meta = next(module['Metaclass'].igetattr('meta_property')) - self.assertIsInstance(property_meta, UnboundMethod) + self.assertIsInstance(property_meta, UnboundMethod if six.PY2 else nodes.FunctionDef) wrapping = scoped_nodes.get_wrapping_class(property_meta) self.assertEqual(wrapping, module['Metaclass']) diff --git a/astroid/tree/scoped_nodes.py b/astroid/tree/scoped_nodes.py index 92df6a71..87db07bb 100644 --- a/astroid/tree/scoped_nodes.py +++ b/astroid/tree/scoped_nodes.py @@ -106,12 +106,17 @@ def _verify_duplicates_mro(sequences, cls, context): mros=sequences, cls=cls, context=context) -def function_to_method(n, klass): +def function_to_method(n, klass, klass_context): if isinstance(n, FunctionDef): if n.type == 'classmethod': return objects.BoundMethod(n, klass) if n.type != 'staticmethod': - return objects.UnboundMethod(n) + if six.PY2: + return objects.UnboundMethod(n) + else: + if klass_context: + return n + return objects.BoundMethod(n, klass) return n @@ -1595,7 +1600,7 @@ class ClassDef(QualifiedNameMixin, base.FilterStmtsMixin, else: yield util.Uninferable else: - yield function_to_method(inferred, self) + yield function_to_method(inferred, self, class_context) except exceptions.AttributeInferenceError as error: if not name.startswith('__') and self.has_dynamic_getattr(context): # class handle some dynamic attributes, return a Uninferable object |
