summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Liu <david@cs.toronto.edu>2021-08-26 23:24:47 -0400
committerPierre Sassoulas <pierre.sassoulas@gmail.com>2021-08-28 09:24:30 +0200
commit7824a2c3ad5bf8971668db23c3a6cd4e2c28bbc2 (patch)
tree73fe100388b94d637489d3fd19b98b0ca38125a8
parentc7666b4ac0f476b98c50ad117256199993fa7f6c (diff)
downloadastroid-git-7824a2c3ad5bf8971668db23c3a6cd4e2c28bbc2.tar.gz
Fixed inference bug with unbound inherited methods
-rw-r--r--ChangeLog6
-rw-r--r--astroid/arguments.py12
-rw-r--r--tests/unittest_inference_calls.py127
3 files changed, 140 insertions, 5 deletions
diff --git a/ChangeLog b/ChangeLog
index e8e61212..4152047d 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -10,6 +10,12 @@ Release date: TBA
Closes PyCQA/pylint#400
+* Fixed bug in inference for superclass instance methods called
+ from the class rather than an instance.
+
+ Closes #1008
+ Closes PyCQA/pylint#4377
+
What's New in astroid 2.7.3?
============================
diff --git a/astroid/arguments.py b/astroid/arguments.py
index 3ee77ef9..a1104e2b 100644
--- a/astroid/arguments.py
+++ b/astroid/arguments.py
@@ -216,11 +216,15 @@ class CallSite:
positional.append(arg)
if argindex is not None:
+ boundnode = getattr(context, "boundnode", None)
# 2. first argument of instance/class method
if argindex == 0 and funcnode.type in ("method", "classmethod"):
- if context.boundnode is not None:
- boundnode = context.boundnode
- else:
+ # context.boundnode is None when an instance method is called with
+ # the class, e.g. MyClass.method(obj, ...). In this case, self
+ # is the first argument.
+ if boundnode is None and funcnode.type == "method" and positional:
+ return positional[0].infer(context=context)
+ if boundnode is None:
# XXX can do better ?
boundnode = funcnode.parent.frame()
@@ -242,7 +246,7 @@ class CallSite:
# if we have a method, extract one position
# from the index, so we'll take in account
# the extra parameter represented by `self` or `cls`
- if funcnode.type in ("method", "classmethod"):
+ if funcnode.type in ("method", "classmethod") and boundnode:
argindex -= 1
# 2. search arg index
try:
diff --git a/tests/unittest_inference_calls.py b/tests/unittest_inference_calls.py
index 66a64dd1..ce99fa43 100644
--- a/tests/unittest_inference_calls.py
+++ b/tests/unittest_inference_calls.py
@@ -1,6 +1,6 @@
"""Tests for function call inference"""
-from astroid import builder, nodes
+from astroid import bases, builder, nodes
from astroid.util import Uninferable
@@ -409,3 +409,128 @@ def test_dunder_getitem():
inferred = node.inferred()
assert len(inferred) == 1
assert inferred[0] is Uninferable
+
+
+def test_instance_method():
+ """Tests for instance method, both bound and unbound."""
+ nodes_ = builder.extract_node(
+ """
+ class A:
+ def method(self, x):
+ return x
+
+ A().method(42) #@
+
+ # In this case, the 1 argument is bound to self, which is ignored in the method
+ A.method(1, 42) #@
+ """
+ )
+
+ for node in nodes_:
+ inferred = node.inferred()
+ assert len(inferred) == 1
+ assert isinstance(inferred[0], nodes.Const)
+ assert inferred[0].value == 42
+
+
+def test_class_method():
+ """Tests for class method calls, both instance and with the class."""
+ nodes_ = builder.extract_node(
+ """
+ class A:
+ @classmethod
+ def method(cls, x):
+ return x
+
+ A.method(42) #@
+ A().method(42) #@
+
+ """
+ )
+
+ for node in nodes_:
+ inferred = node.inferred()
+ assert len(inferred) == 1
+ assert isinstance(inferred[0], nodes.Const), node
+ assert inferred[0].value == 42
+
+
+def test_static_method():
+ """Tests for static method calls, both instance and with the class."""
+ nodes_ = builder.extract_node(
+ """
+ class A:
+ @staticmethod
+ def method(x):
+ return x
+
+ A.method(42) #@
+ A().method(42) #@
+ """
+ )
+
+ for node in nodes_:
+ inferred = node.inferred()
+ assert len(inferred) == 1
+ assert isinstance(inferred[0], nodes.Const), node
+ assert inferred[0].value == 42
+
+
+def test_instance_method_inherited():
+ """Tests for instance methods that are inherited from a superclass.
+
+ Based on https://github.com/PyCQA/astroid/issues/1008.
+ """
+ nodes_ = builder.extract_node(
+ """
+ class A:
+ def method(self):
+ return self
+
+ class B(A):
+ pass
+
+ A().method() #@
+ A.method(A()) #@
+
+ B().method() #@
+ B.method(B()) #@
+ A.method(B()) #@
+ """
+ )
+ expected = ["A", "A", "B", "B", "B"]
+ for node, expected in zip(nodes_, expected):
+ inferred = node.inferred()
+ assert len(inferred) == 1
+ assert isinstance(inferred[0], bases.Instance)
+ assert inferred[0].name == expected
+
+
+def test_class_method_inherited():
+ """Tests for class methods that are inherited from a superclass.
+
+ Based on https://github.com/PyCQA/astroid/issues/1008.
+ """
+ nodes_ = builder.extract_node(
+ """
+ class A:
+ @classmethod
+ def method(cls):
+ return cls
+
+ class B(A):
+ pass
+
+ A().method() #@
+ A.method() #@
+
+ B().method() #@
+ B.method() #@
+ """
+ )
+ expected = ["A", "A", "B", "B"]
+ for node, expected in zip(nodes_, expected):
+ inferred = node.inferred()
+ assert len(inferred) == 1
+ assert isinstance(inferred[0], nodes.ClassDef)
+ assert inferred[0].name == expected