summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaudiu Popa <pcmanticore@gmail.com>2020-03-07 16:54:59 +0100
committerClaudiu Popa <pcmanticore@gmail.com>2020-03-07 16:54:59 +0100
commit04f5853b42f3c886fc7a3b553e32f1d1bf21419b (patch)
tree668f217fdb9666fd2fb638c46c9a01405195606d
parent7120d894635662d49b05c0cc5d664b5a25544605 (diff)
downloadastroid-git-04f5853b42f3c886fc7a3b553e32f1d1bf21419b.tar.gz
Do not infer the first argument of a staticmethod in a metaclass as the class itself
Close PyCQA/pylint#3032
-rw-r--r--ChangeLog4
-rw-r--r--astroid/protocols.py9
-rw-r--r--tests/unittest_inference.py12
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()