summaryrefslogtreecommitdiff
path: root/tests/unittest_inference.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/unittest_inference.py')
-rw-r--r--tests/unittest_inference.py83
1 files changed, 59 insertions, 24 deletions
diff --git a/tests/unittest_inference.py b/tests/unittest_inference.py
index b3df5f6c..7b80b530 100644
--- a/tests/unittest_inference.py
+++ b/tests/unittest_inference.py
@@ -14,14 +14,18 @@
# Copyright (c) 2017 Calen Pennington <calen.pennington@gmail.com>
# Copyright (c) 2017 David Euresti <david@dropbox.com>
# Copyright (c) 2017 Derek Gustafson <degustaf@gmail.com>
+# Copyright (c) 2018 Bryce Guinta <bryce.paul.guinta@gmail.com>
# Copyright (c) 2018 Daniel Martin <daniel.martin@crowdstrike.com>
# Copyright (c) 2018 Ville Skyttä <ville.skytta@iki.fi>
-# Copyright (c) 2018 Bryce Guinta <bryce.paul.guinta@gmail.com>
# Copyright (c) 2018 Anthony Sottile <asottile@umich.edu>
# Copyright (c) 2019 Stanislav Levin <slev@altlinux.org>
# Copyright (c) 2019 David Liu <david@cs.toronto.edu>
# Copyright (c) 2019 Ashley Whetter <ashley@awhetter.co.uk>
# Copyright (c) 2019 Hugo van Kemenade <hugovk@users.noreply.github.com>
+# Copyright (c) 2020 Peter Kolbus <peter.kolbus@gmail.com>
+# Copyright (c) 2020 hippo91 <guillaume.peillex@gmail.com>
+# Copyright (c) 2020 Karthikeyan Singaravelan <tir.karthi@gmail.com>
+# Copyright (c) 2020 Bryce Guinta <bryce.guinta@protonmail.com>
# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER
@@ -428,7 +432,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase):
del undefined_attr
"""
delete = extract_node(code, __name__)
- self.assertRaises(InferenceError, delete.infer)
+ self.assertRaises(InferenceError, next, delete.infer())
def test_del2(self):
code = """
@@ -541,18 +545,6 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase):
self.assertEqual(ancestor.root().name, BUILTINS)
self.assertRaises(StopIteration, partial(next, ancestors))
- def test_qqch(self):
- code = """
- from astroid.modutils import load_module_from_name
- xxx = load_module_from_name('__pkginfo__')
- """
- ast = parse(code, __name__)
- xxx = ast["xxx"]
- self.assertSetEqual(
- {n.__class__ for n in xxx.inferred()},
- {nodes.Const, util.Uninferable.__class__},
- )
-
def test_method_argument(self):
code = '''
class ErudiEntitySchema:
@@ -2083,8 +2075,6 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase):
def test_str_methods(self):
code = """
' '.decode() #@
-
- ' '.encode() #@
' '.join('abcd') #@
' '.replace('a', 'b') #@
' '.format('a') #@
@@ -2106,15 +2096,13 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase):
"""
ast = extract_node(code, __name__)
self.assertInferConst(ast[0], "")
- for i in range(1, 16):
+ for i in range(1, 15):
self.assertInferConst(ast[i], "")
- for i in range(16, 19):
+ for i in range(15, 18):
self.assertInferConst(ast[i], 0)
def test_unicode_methods(self):
code = """
- u' '.encode() #@
-
u' '.decode() #@
u' '.join('abcd') #@
u' '.replace('a', 'b') #@
@@ -2137,9 +2125,9 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase):
"""
ast = extract_node(code, __name__)
self.assertInferConst(ast[0], "")
- for i in range(1, 16):
+ for i in range(1, 15):
self.assertInferConst(ast[i], "")
- for i in range(16, 19):
+ for i in range(15, 18):
self.assertInferConst(ast[i], 0)
def test_scope_lookup_same_attributes(self):
@@ -2459,7 +2447,6 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase):
1 ** (lambda x: x) #@
{} * {} #@
{} - {} #@
- {} | {} #@
{} >> {} #@
[] + () #@
() + [] #@
@@ -2504,7 +2491,6 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase):
msg.format(op="**", lhs="int", rhs="function"),
msg.format(op="*", lhs="dict", rhs="dict"),
msg.format(op="-", lhs="dict", rhs="dict"),
- msg.format(op="|", lhs="dict", rhs="dict"),
msg.format(op=">>", lhs="dict", rhs="dict"),
msg.format(op="+", lhs="list", rhs="tuple"),
msg.format(op="+", lhs="tuple", rhs="list"),
@@ -2519,6 +2505,12 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase):
msg.format(op="+=", lhs="int", rhs="A"),
msg.format(op="+=", lhs="int", rhs="list"),
]
+
+ # PEP-584 supports | for dictionary union
+ if sys.version_info < (3, 9):
+ ast_nodes.append(extract_node("{} | {} #@"))
+ expected.append(msg.format(op="|", lhs="dict", rhs="dict"))
+
for node, expected_value in zip(ast_nodes, expected):
errors = node.type_errors()
self.assertEqual(len(errors), 1)
@@ -5668,6 +5660,10 @@ def test_custom_decorators_for_classmethod_and_staticmethods(code, obj, obj_type
@pytest.mark.skipif(sys.version_info < (3, 8), reason="Needs dataclasses available")
+@pytest.mark.skipif(
+ sys.version_info >= (3, 9),
+ reason="Exact inference with dataclasses (replace function) in python3.9",
+)
def test_dataclasses_subscript_inference_recursion_error():
code = """
from dataclasses import dataclass, replace
@@ -5688,6 +5684,31 @@ def test_dataclasses_subscript_inference_recursion_error():
assert helpers.safe_infer(node) is None
+@pytest.mark.skipif(
+ sys.version_info < (3, 9),
+ reason="Exact inference with dataclasses (replace function) in python3.9",
+)
+def test_dataclasses_subscript_inference_recursion_error_39():
+ code = """
+ from dataclasses import dataclass, replace
+
+ @dataclass
+ class ProxyConfig:
+ auth: str = "/auth"
+
+
+ a = ProxyConfig("")
+ test_dict = {"proxy" : {"auth" : "", "bla" : "f"}}
+
+ foo = test_dict['proxy']
+ replace(a, **test_dict['proxy']) # This fails
+ """
+ node = extract_node(code)
+ infer_val = helpers.safe_infer(node)
+ assert isinstance(infer_val, Instance)
+ assert infer_val.pytype() == ".ProxyConfig"
+
+
def test_self_reference_infer_does_not_trigger_recursion_error():
# Prevents https://github.com/PyCQA/pylint/issues/1285
code = """
@@ -5864,5 +5885,19 @@ def test_infer_generated_setter():
assert list(inferred.nodes_of_class(nodes.Const)) == []
+def test_infer_list_of_uninferables_does_not_crash():
+ code = """
+ x = [A] * 1
+ f = [x, [A] * 2]
+ x = list(f) + [] # List[Uninferable]
+ tuple(x[0])
+ """
+ node = extract_node(code)
+ inferred = next(node.infer())
+ assert isinstance(inferred, nodes.Tuple)
+ # Would not be able to infer the first element.
+ assert not inferred.elts
+
+
if __name__ == "__main__":
unittest.main()