From 04f5853b42f3c886fc7a3b553e32f1d1bf21419b Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sat, 7 Mar 2020 16:54:59 +0100 Subject: Do not infer the first argument of a staticmethod in a metaclass as the class itself Close PyCQA/pylint#3032 --- ChangeLog | 4 ++++ astroid/protocols.py | 9 +++++++-- tests/unittest_inference.py | 12 ++++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index 7485f123..d9708b07 100644 --- a/ChangeLog +++ b/ChangeLog @@ -14,6 +14,10 @@ Release Date: TBA Close PyCQA/pylint#2991 +* Do not infer the first argument of a staticmethod in a metaclass as the class itself + + Close PyCQA/pylint#3032 + * ``NodeNG.bool_value()`` gained an optional ``context`` parameter We need to pass an inference context downstream when inferring the boolean diff --git a/astroid/protocols.py b/astroid/protocols.py index 6179ab34..185bda55 100644 --- a/astroid/protocols.py +++ b/astroid/protocols.py @@ -317,9 +317,14 @@ def _arguments_infer_argname(self, name, context): if not (self.arguments or self.vararg or self.kwarg): yield util.Uninferable return + + functype = self.parent.type # first argument of instance/class method - if self.arguments and getattr(self.arguments[0], "name", None) == name: - functype = self.parent.type + if ( + self.arguments + and getattr(self.arguments[0], "name", None) == name + and functype != "staticmethod" + ): 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 diff --git a/tests/unittest_inference.py b/tests/unittest_inference.py index 7f86de4d..f9e95e40 100644 --- a/tests/unittest_inference.py +++ b/tests/unittest_inference.py @@ -5743,5 +5743,17 @@ def test_getattr_fails_on_empty_values(): inferred.getattr("") +def test_infer_first_argument_of_static_method_in_metaclass(): + code = """ + class My(type): + @staticmethod + def test(args): + args #@ + """ + node = extract_node(code) + inferred = next(node.infer()) + assert inferred is util.Uninferable + + if __name__ == "__main__": unittest.main() -- cgit v1.2.1