diff options
| author | Claudiu Popa <pcmanticore@gmail.com> | 2020-03-03 09:13:14 +0100 |
|---|---|---|
| committer | Claudiu Popa <pcmanticore@gmail.com> | 2020-03-03 09:15:14 +0100 |
| commit | 627886c1c8b770d04648bab3ea2168c10f39d816 (patch) | |
| tree | 8404fa3def2f52394f188d9fde0d1cd713641029 | |
| parent | 3eed22d5f381520e017a478c25b61910c6ac261b (diff) | |
| download | astroid-git-627886c1c8b770d04648bab3ea2168c10f39d816.tar.gz | |
Better inference of class and static methods decorated with custom methods
Close PyCQA/pylint#3209
| -rw-r--r-- | ChangeLog | 4 | ||||
| -rw-r--r-- | astroid/scoped_nodes.py | 15 | ||||
| -rw-r--r-- | tests/unittest_inference.py | 78 |
3 files changed, 97 insertions, 0 deletions
@@ -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() |
