From a488feb93116cea555302761014a3309d76d8793 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sat, 31 Oct 2015 21:31:36 +0200 Subject: Fix some inconsistencies with accessing the first argument of methods If the current class is a metaclass (inherits from `type`), then the first argument is always a class, not an instance. --- astroid/arguments.py | 10 +++++++++ astroid/protocols.py | 10 ++++++--- astroid/tests/unittest_inference.py | 42 +++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 3 deletions(-) diff --git a/astroid/arguments.py b/astroid/arguments.py index 5670fa8..e80f5f4 100644 --- a/astroid/arguments.py +++ b/astroid/arguments.py @@ -183,6 +183,16 @@ class CallSite(object): else: # XXX can do better ? boundnode = funcnode.parent.frame() + + if isinstance(boundnode, nodes.ClassDef): + # Verify that we're accessing a method + # of the metaclass through a class, as in + # `cls.metaclass_method`. In this case, the + # first argument is always the class. + method_scope = funcnode.parent.scope() + if method_scope is boundnode.metaclass(): + return iter((boundnode, )) + if funcnode.type == 'method': if not isinstance(boundnode, bases.Instance): boundnode = bases.Instance(boundnode) diff --git a/astroid/protocols.py b/astroid/protocols.py index 7c9e4b9..28b1dd2 100644 --- a/astroid/protocols.py +++ b/astroid/protocols.py @@ -280,12 +280,16 @@ def _arguments_infer_argname(self, name, context): # first argument of instance/class method if self.args and getattr(self.args[0], 'name', None) == name: functype = self.parent.type + cls = self.parent.parent.scope() + is_metaclass = isinstance(cls, nodes.ClassDef) and cls.type == 'metaclass' + # If this is a metaclass, then the first argument will always + # be the class, not an instance. + if is_metaclass or functype == 'classmethod': + yield cls + return if functype == 'method': yield bases.Instance(self.parent.parent.frame()) return - if functype == 'classmethod': - yield self.parent.parent.frame() - return if context and context.callcontext: call_site = arguments.CallSite(context.callcontext) diff --git a/astroid/tests/unittest_inference.py b/astroid/tests/unittest_inference.py index fa090f8..0f45d3a 100644 --- a/astroid/tests/unittest_inference.py +++ b/astroid/tests/unittest_inference.py @@ -3143,6 +3143,48 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertIsInstance(third_c, Instance) self.assertEqual(third_c.name, 'A') + def test_metaclass_subclasses_arguments_are_classes_not_instances(self): + ast_node = test_utils.extract_node(''' + class A(type): + def test(cls): + return cls + import six + @six.add_metaclass(A) + class B(object): + pass + + B.test() #@ + ''') + inferred = next(ast_node.infer()) + self.assertIsInstance(inferred, nodes.ClassDef) + self.assertEqual(inferred.name, 'B') + + def test_infer_cls_in_class_methods(self): + ast_nodes = test_utils.extract_node(''' + class A(type): + def __call__(cls): + cls #@ + class B(object): + def __call__(cls): + cls #@ + ''') + first = next(ast_nodes[0].infer()) + self.assertIsInstance(first, nodes.ClassDef) + second = next(ast_nodes[1].infer()) + self.assertIsInstance(second, Instance) + + @unittest.expectedFailure + def test_metaclass_arguments_are_classes_not_instances(self): + ast_node = test_utils.extract_node(''' + class A(type): + def test(cls): return cls + A.test() #@ + ''') + # This is not supported yet + inferred = next(ast_node.infer()) + self.assertIsInstance(inferred, ClassDef) + self.assertEqual(inferred.name, 'A') + class GetattrTest(unittest.TestCase): -- cgit v1.2.1 From 7390da6f62c1fb4e28623023ecf66fdec299680e Mon Sep 17 00:00:00 2001 From: Dmitry Pribysh Date: Wed, 4 Nov 2015 13:53:03 +0300 Subject: Add __getitem__ method to collections.deque inferface description. --- astroid/brain/brain_stdlib.py | 1 + 1 file changed, 1 insertion(+) diff --git a/astroid/brain/brain_stdlib.py b/astroid/brain/brain_stdlib.py index a03358f..f799929 100644 --- a/astroid/brain/brain_stdlib.py +++ b/astroid/brain/brain_stdlib.py @@ -140,6 +140,7 @@ class deque(object): def rotate(self, n): pass def __iter__(self): return self def __reversed__(self): return self.iterable[::-1] + def __getitem__(self, index): pass ''') -- cgit v1.2.1 From 7ca154dd00788422ea44c8f92ab1de323741249f Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Wed, 4 Nov 2015 12:54:15 +0200 Subject: Backout changeset 9c3f71, because it leads to too many false positives when using .split() in assignment unpacking. --- astroid/brain/brain_builtin_inference.py | 2 -- astroid/tests/unittest_inference.py | 6 ------ 2 files changed, 8 deletions(-) diff --git a/astroid/brain/brain_builtin_inference.py b/astroid/brain/brain_builtin_inference.py index 7559271..c6245be 100644 --- a/astroid/brain/brain_builtin_inference.py +++ b/astroid/brain/brain_builtin_inference.py @@ -60,8 +60,6 @@ def _extend_str(class_node, rvalue): return {rvalue} def ljust(self, width, fillchar=None): return {rvalue} - def split(self, *args): - return [] ''') code = code.format(rvalue=rvalue) fake = AstroidBuilder(MANAGER).string_build(code)['whatever'] diff --git a/astroid/tests/unittest_inference.py b/astroid/tests/unittest_inference.py index 0f45d3a..7287f70 100644 --- a/astroid/tests/unittest_inference.py +++ b/astroid/tests/unittest_inference.py @@ -1796,8 +1796,6 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): ' '.index() #@ ' '.find() #@ ' '.count() #@ - - ' '.split() #@ """ ast = test_utils.extract_node(code, __name__) self.assertInferConst(ast[0], u'') @@ -1805,7 +1803,6 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertInferConst(ast[i], '') for i in range(16, 19): self.assertInferConst(ast[i], 0) - self.assertInferList(ast[19], []) def test_unicode_methods(self): code = """ @@ -1830,8 +1827,6 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): u' '.index() #@ u' '.find() #@ u' '.count() #@ - - u' '.split() #@ """ ast = test_utils.extract_node(code, __name__) self.assertInferConst(ast[0], '') @@ -1839,7 +1834,6 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertInferConst(ast[i], u'') for i in range(16, 19): self.assertInferConst(ast[i], 0) - self.assertInferList(ast[19], []) def test_scope_lookup_same_attributes(self): code = ''' -- cgit v1.2.1 From fcadcc2ea44651540f1118778a3826bbd4afec48 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Wed, 4 Nov 2015 12:58:43 +0200 Subject: Fix pylint warnings. --- astroid/arguments.py | 4 +-- astroid/helpers.py | 2 +- astroid/scoped_nodes.py | 68 +++++++++++++++++++++++++------------------------ 3 files changed, 38 insertions(+), 36 deletions(-) diff --git a/astroid/arguments.py b/astroid/arguments.py index e80f5f4..ab841a9 100644 --- a/astroid/arguments.py +++ b/astroid/arguments.py @@ -183,12 +183,12 @@ class CallSite(object): else: # XXX can do better ? boundnode = funcnode.parent.frame() - + if isinstance(boundnode, nodes.ClassDef): # Verify that we're accessing a method # of the metaclass through a class, as in # `cls.metaclass_method`. In this case, the - # first argument is always the class. + # first argument is always the class. method_scope = funcnode.parent.scope() if method_scope is boundnode.metaclass(): return iter((boundnode, )) diff --git a/astroid/helpers.py b/astroid/helpers.py index 00f4784..d4cd0dd 100644 --- a/astroid/helpers.py +++ b/astroid/helpers.py @@ -86,7 +86,7 @@ def object_type(node, context=None): This is used to implement the ``type`` builtin, which means that it's used for inferring type calls, as well as used in a couple of other places - in the inference. + in the inference. The node will be inferred first, so this function can support all sorts of objects, as long as they support inference. """ diff --git a/astroid/scoped_nodes.py b/astroid/scoped_nodes.py index e6e3324..729f283 100644 --- a/astroid/scoped_nodes.py +++ b/astroid/scoped_nodes.py @@ -777,43 +777,45 @@ class FunctionDef(node_classes.Statement, Lambda): else: type_name = 'method' - if self.decorators: - for node in self.decorators.nodes: - if isinstance(node, node_classes.Name): - if node.name in builtin_descriptors: - return node.name - - if isinstance(node, node_classes.Call): - # Handle the following case: - # @some_decorator(arg1, arg2) - # def func(...) - # - try: - current = next(node.func.infer()) - except exceptions.InferenceError: - continue - _type = _infer_decorator_callchain(current) + if not self.decorators: + return type_name + + for node in self.decorators.nodes: + if isinstance(node, node_classes.Name): + if node.name in builtin_descriptors: + return node.name + + if isinstance(node, node_classes.Call): + # Handle the following case: + # @some_decorator(arg1, arg2) + # def func(...) + # + try: + current = next(node.func.infer()) + except exceptions.InferenceError: + continue + _type = _infer_decorator_callchain(current) + if _type is not None: + return _type + + try: + for inferred in node.infer(): + # Check to see if this returns a static or a class method. + _type = _infer_decorator_callchain(inferred) if _type is not None: return _type - try: - for inferred in node.infer(): - # Check to see if this returns a static or a class method. - _type = _infer_decorator_callchain(inferred) - if _type is not None: - return _type - - if not isinstance(inferred, ClassDef): + if not isinstance(inferred, ClassDef): + continue + for ancestor in inferred.ancestors(): + if not isinstance(ancestor, ClassDef): continue - for ancestor in inferred.ancestors(): - if not isinstance(ancestor, ClassDef): - continue - if ancestor.is_subtype_of('%s.classmethod' % BUILTINS): - return 'classmethod' - elif ancestor.is_subtype_of('%s.staticmethod' % BUILTINS): - return 'staticmethod' - except exceptions.InferenceError: - pass + if ancestor.is_subtype_of('%s.classmethod' % BUILTINS): + return 'classmethod' + elif ancestor.is_subtype_of('%s.staticmethod' % BUILTINS): + return 'staticmethod' + except exceptions.InferenceError: + pass return type_name @decorators_mod.cachedproperty -- cgit v1.2.1