summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorClaudiu Popa <pcmanticore@gmail.com>2019-11-25 08:32:49 +0100
committerClaudiu Popa <pcmanticore@gmail.com>2019-11-25 08:32:49 +0100
commit130406463f3c978571a7aca6e33c985e17848f21 (patch)
treefcb51e52ab231c0e047d85656fbb30567653434c /tests
parent05a9106b539182610c1153fbe6ad6fbe8a6d7d58 (diff)
downloadastroid-git-130406463f3c978571a7aca6e33c985e17848f21.tar.gz
Scope the inference to the current bound node when inferring instances of classes
When inferring instances of classes from arguments, such as ``self`` in a bound method, we could use as a hint the context's ``boundnode``, which indicates the instance from which the inference originated. As an example, a subclass that uses a parent's method which returns ``self``, will override the ``self`` to point to it instead of pointing to the parent class. Close PyCQA/pylint#3157
Diffstat (limited to 'tests')
-rw-r--r--tests/unittest_inference.py52
1 files changed, 52 insertions, 0 deletions
diff --git a/tests/unittest_inference.py b/tests/unittest_inference.py
index f8dc7a64..7d0e10b9 100644
--- a/tests/unittest_inference.py
+++ b/tests/unittest_inference.py
@@ -5370,5 +5370,57 @@ def test_infer_exception_instance_attributes():
assert isinstance(index[0], nodes.AssignAttr)
+@pytest.mark.parametrize(
+ "code,instance_name",
+ [
+ (
+ """
+ class A:
+ def __enter__(self):
+ return self
+ def __exit__(self, err_type, err, traceback):
+ return
+ class B(A):
+ pass
+ with B() as b:
+ b #@
+ """,
+ "B",
+ ),
+ (
+ """
+ class A:
+ def __enter__(self):
+ return A()
+ def __exit__(self, err_type, err, traceback):
+ return
+ class B(A):
+ pass
+ with B() as b:
+ b #@
+ """,
+ "A",
+ ),
+ (
+ """
+ class A:
+ def test(self):
+ return A()
+ class B(A):
+ def test(self):
+ return A.test(self)
+ B().test()
+ """,
+ "A",
+ ),
+ ],
+)
+def test_inference_is_limited_to_the_boundnode(code, instance_name):
+ node = extract_node(code)
+ inferred = next(node.infer())
+ assert isinstance(inferred, Instance)
+ assert inferred.name == instance_name
+
+
if __name__ == "__main__":
unittest.main()