From 627886c1c8b770d04648bab3ea2168c10f39d816 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Tue, 3 Mar 2020 09:13:14 +0100 Subject: Better inference of class and static methods decorated with custom methods Close PyCQA/pylint#3209 --- ChangeLog | 4 +++ astroid/scoped_nodes.py | 15 +++++++++ tests/unittest_inference.py | 78 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+) diff --git a/ChangeLog b/ChangeLog index 9cef7b94..117f7a08 100644 --- a/ChangeLog +++ b/ChangeLog @@ -14,6 +14,10 @@ Release Date: TBA Close PyCQA/pylint#3414 +* Better inference of class and static methods decorated with custom methods + + Close PyCQA/pylint#3209 + * Numpy `datetime64.astype` return value is inferred as a `ndarray`. Close PyCQA/pylint#3332 diff --git a/astroid/scoped_nodes.py b/astroid/scoped_nodes.py index 0abf03d4..c7353f5b 100644 --- a/astroid/scoped_nodes.py +++ b/astroid/scoped_nodes.py @@ -1076,6 +1076,21 @@ def _infer_decorator_callchain(node): return "classmethod" if result.is_subtype_of("%s.staticmethod" % BUILTINS): return "staticmethod" + if isinstance(result, FunctionDef): + if not result.decorators: + return None + # Determine if this function is decorated with one of the builtin descriptors we want. + for decorator in result.decorators.nodes: + if isinstance(decorator, node_classes.Name): + if decorator.name in BUILTIN_DESCRIPTORS: + return decorator.name + if ( + isinstance(decorator, node_classes.Attribute) + and isinstance(decorator.expr, node_classes.Name) + and decorator.expr.name == BUILTINS + and decorator.attrname in BUILTIN_DESCRIPTORS + ): + return decorator.attrname return None diff --git a/tests/unittest_inference.py b/tests/unittest_inference.py index 9808e3bb..83dfcfb5 100644 --- a/tests/unittest_inference.py +++ b/tests/unittest_inference.py @@ -5566,5 +5566,83 @@ def test_infer_dict_passes_context(): assert inferred.qname() == "builtins.dict" +@pytest.mark.parametrize( + "code,obj,obj_type", + [ + ( + """ + def klassmethod1(method): + @classmethod + def inner(cls): + return method(cls) + return inner + + class X(object): + @klassmethod1 + def x(cls): + return 'X' + X.x + """, + BoundMethod, + "classmethod", + ), + ( + """ + def staticmethod1(method): + @staticmethod + def inner(cls): + return method(cls) + return inner + + class X(object): + @staticmethod1 + def x(cls): + return 'X' + X.x + """, + nodes.FunctionDef, + "staticmethod", + ), + ( + """ + def klassmethod1(method): + def inner(cls): + return method(cls) + return classmethod(inner) + + class X(object): + @klassmethod1 + def x(cls): + return 'X' + X.x + """, + BoundMethod, + "classmethod", + ), + ( + """ + def staticmethod1(method): + def inner(cls): + return method(cls) + return staticmethod(inner) + + class X(object): + @staticmethod1 + def x(cls): + return 'X' + X.x + """, + nodes.FunctionDef, + "staticmethod", + ), + ], +) +def test_custom_decorators_for_classmethod_and_staticmethods(code, obj, obj_type): + node = extract_node(code) + inferred = next(node.infer()) + assert isinstance(inferred, obj) + assert inferred.type == obj_type + + if __name__ == "__main__": unittest.main() -- cgit v1.2.1