summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorhippo91 <guillaume.peillex@gmail.com>2021-02-07 16:29:50 +0100
committerGitHub <noreply@github.com>2021-02-07 16:29:50 +0100
commitd60eb5e207427599fe2f4a8d44e4cb0bebcf3c9f (patch)
treeafca46ee9a4b30effa5a6d36657b64b7f492c651 /tests
parent529cd943ea05e1c263b3e07366973bc9a32ff171 (diff)
parent599fe72a0c0e07e2a7720237c40800aa3c611708 (diff)
downloadastroid-git-d60eb5e207427599fe2f4a8d44e4cb0bebcf3c9f.tar.gz
Merge branch 'master' into master
Diffstat (limited to 'tests')
-rw-r--r--tests/unittest_brain.py55
-rw-r--r--tests/unittest_brain_numpy_core_umath.py8
-rw-r--r--tests/unittest_inference.py103
-rw-r--r--tests/unittest_nodes.py24
-rw-r--r--tests/unittest_regrtest.py2
5 files changed, 185 insertions, 7 deletions
diff --git a/tests/unittest_brain.py b/tests/unittest_brain.py
index 628e6e2c..cb8f9e26 100644
--- a/tests/unittest_brain.py
+++ b/tests/unittest_brain.py
@@ -146,6 +146,17 @@ class CollectionsDequeTests(unittest.TestCase):
self.assertIn("insert", inferred.locals)
self.assertIn("index", inferred.locals)
+ @test_utils.require_version(maxver="3.8")
+ def test_deque_not_py39methods(self):
+ inferred = self._inferred_queue_instance()
+ with self.assertRaises(astroid.exceptions.AttributeInferenceError):
+ inferred.getattr("__class_getitem__")
+
+ @test_utils.require_version(minver="3.9")
+ def test_deque_py39methods(self):
+ inferred = self._inferred_queue_instance()
+ self.assertTrue(inferred.getattr("__class_getitem__"))
+
class OrderedDictTest(unittest.TestCase):
def _inferred_ordered_dict_instance(self):
@@ -947,6 +958,43 @@ class IOBrainTest(unittest.TestCase):
self.assertEqual(raw.name, "FileIO")
+@test_utils.require_version("3.9")
+class TypeBrain(unittest.TestCase):
+ def test_type_subscript(self):
+ """
+ Check that type object has the __class_getitem__ method
+ when it is used as a subscript
+ """
+ src = builder.extract_node(
+ """
+ a: type[int] = int
+ """
+ )
+ val_inf = src.annotation.value.inferred()[0]
+ self.assertIsInstance(val_inf, astroid.ClassDef)
+ self.assertEqual(val_inf.name, "type")
+ meth_inf = val_inf.getattr("__class_getitem__")[0]
+ self.assertIsInstance(meth_inf, astroid.FunctionDef)
+
+ def test_invalid_type_subscript(self):
+ """
+ Check that a type (str for example) that inherits
+ from type does not have __class_getitem__ method even
+ when it is used as a subscript
+ """
+ src = builder.extract_node(
+ """
+ a: str[int] = "abc"
+ """
+ )
+ val_inf = src.annotation.value.inferred()[0]
+ self.assertIsInstance(val_inf, astroid.ClassDef)
+ self.assertEqual(val_inf.name, "str")
+ with self.assertRaises(astroid.exceptions.AttributeInferenceError):
+ meth_inf = val_inf.getattr("__class_getitem__")[0]
+
+
+@test_utils.require_version("3.6")
class TypingBrain(unittest.TestCase):
def test_namedtuple_base(self):
klass = builder.extract_node(
@@ -1298,6 +1346,13 @@ class SubprocessTest(unittest.TestCase):
assert isinstance(inferred, astroid.Const)
assert isinstance(inferred.value, (str, bytes))
+ @test_utils.require_version("3.9")
+ def test_popen_does_not_have_class_getitem(self):
+ code = """import subprocess; subprocess.Popen"""
+ node = astroid.extract_node(code)
+ inferred = next(node.infer())
+ assert "__class_getitem__" in inferred
+
class TestIsinstanceInference:
"""Test isinstance builtin inference"""
diff --git a/tests/unittest_brain_numpy_core_umath.py b/tests/unittest_brain_numpy_core_umath.py
index 2d2abdbe..acfaeb70 100644
--- a/tests/unittest_brain_numpy_core_umath.py
+++ b/tests/unittest_brain_numpy_core_umath.py
@@ -65,6 +65,7 @@ class NumpyBrainCoreUmathTest(unittest.TestCase):
)
two_args_ufunc = (
+ "add",
"bitwise_and",
"bitwise_or",
"bitwise_xor",
@@ -92,6 +93,7 @@ class NumpyBrainCoreUmathTest(unittest.TestCase):
"logical_xor",
"maximum",
"minimum",
+ "multiply",
"nextafter",
"not_equal",
"power",
@@ -224,11 +226,9 @@ class NumpyBrainCoreUmathTest(unittest.TestCase):
with self.subTest(typ=func_):
inferred_values = list(self._inferred_numpy_func_call(func_))
self.assertTrue(
- len(inferred_values) == 1
- or len(inferred_values) == 2
- and inferred_values[-1].pytype() is util.Uninferable,
+ len(inferred_values) == 1,
msg="Too much inferred values ({}) for {:s}".format(
- inferred_values[-1].pytype(), func_
+ inferred_values, func_
),
)
self.assertTrue(
diff --git a/tests/unittest_inference.py b/tests/unittest_inference.py
index df9d2781..e580ee2a 100644
--- a/tests/unittest_inference.py
+++ b/tests/unittest_inference.py
@@ -1299,7 +1299,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase):
result = node.inferred()
assert len(result) == 2
assert isinstance(result[0], nodes.Dict)
- assert result[1] is util.Uninferable
+ assert isinstance(result[1], nodes.Dict)
def test_python25_no_relative_import(self):
ast = resources.build_file("data/package/absimport.py")
@@ -2982,6 +2982,23 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase):
self.assertIsInstance(inferred, nodes.Const)
self.assertEqual(inferred.value, 24)
+ def test_with_metaclass__getitem__(self):
+ ast_node = extract_node(
+ """
+ class Meta(type):
+ def __getitem__(cls, arg):
+ return 24
+ import six
+ class A(six.with_metaclass(Meta)):
+ pass
+
+ A['Awesome'] #@
+ """
+ )
+ inferred = next(ast_node.infer())
+ self.assertIsInstance(inferred, nodes.Const)
+ self.assertEqual(inferred.value, 24)
+
def test_bin_op_classes(self):
ast_node = extract_node(
"""
@@ -2998,6 +3015,23 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase):
self.assertIsInstance(inferred, nodes.Const)
self.assertEqual(inferred.value, 24)
+ def test_bin_op_classes_with_metaclass(self):
+ ast_node = extract_node(
+ """
+ class Meta(type):
+ def __or__(self, other):
+ return 24
+ import six
+ class A(six.with_metaclass(Meta)):
+ pass
+
+ A | A
+ """
+ )
+ inferred = next(ast_node.infer())
+ self.assertIsInstance(inferred, nodes.Const)
+ self.assertEqual(inferred.value, 24)
+
def test_bin_op_supertype_more_complicated_example(self):
ast_node = extract_node(
"""
@@ -3335,6 +3369,22 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase):
self.assertIsInstance(inferred, nodes.Const)
self.assertEqual(inferred.value, 42)
+ def test_unary_op_classes_with_metaclass(self):
+ ast_node = extract_node(
+ """
+ import six
+ class Meta(type):
+ def __invert__(self):
+ return 42
+ class A(six.with_metaclass(Meta)):
+ pass
+ ~A
+ """
+ )
+ inferred = next(ast_node.infer())
+ self.assertIsInstance(inferred, nodes.Const)
+ self.assertEqual(inferred.value, 42)
+
def _slicing_test_helper(self, pairs, cls, get_elts):
for code, expected in pairs:
ast_node = extract_node(code)
@@ -3634,7 +3684,8 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase):
flow = AttributeDict()
flow['app'] = AttributeDict()
flow['app']['config'] = AttributeDict()
- flow['app']['config']['doffing'] = AttributeDict() #@
+ flow['app']['config']['doffing'] = AttributeDict()
+ flow['app']['config']['doffing']['thinkto'] = AttributeDict() #@
"""
)
self.assertIsNone(helpers.safe_infer(ast_node.targets[0]))
@@ -3725,6 +3776,40 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase):
self.assertIsInstance(inferred, nodes.ClassDef)
self.assertEqual(inferred.name, "B")
+ def test_With_metaclass_subclasses_arguments_are_classes_not_instances(self):
+ ast_node = extract_node(
+ """
+ class A(type):
+ def test(cls):
+ return cls
+ import six
+ class B(six.with_metaclass(A)):
+ pass
+
+ B.test() #@
+ """
+ )
+ inferred = next(ast_node.infer())
+ self.assertIsInstance(inferred, nodes.ClassDef)
+ self.assertEqual(inferred.name, "B")
+
+ def test_With_metaclass_with_partial_imported_name(self):
+ ast_node = extract_node(
+ """
+ class A(type):
+ def test(cls):
+ return cls
+ from six import with_metaclass
+ class B(with_metaclass(A)):
+ pass
+
+ B.test() #@
+ """
+ )
+ inferred = next(ast_node.infer())
+ self.assertIsInstance(inferred, nodes.ClassDef)
+ self.assertEqual(inferred.name, "B")
+
def test_infer_cls_in_class_methods(self):
ast_nodes = extract_node(
"""
@@ -5854,5 +5939,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()
diff --git a/tests/unittest_nodes.py b/tests/unittest_nodes.py
index 89140116..3396f91e 100644
--- a/tests/unittest_nodes.py
+++ b/tests/unittest_nodes.py
@@ -1157,6 +1157,19 @@ def test_type_comments_posonly_arguments():
assert actual_arg.as_string() == expected_arg
+@pytest.mark.skipif(not HAS_TYPED_AST, reason="requires typed_ast")
+def test_correct_function_type_comment_parent():
+ data = """
+ def f(a):
+ # type: (A) -> A
+ pass
+ """
+ astroid = builder.parse(data)
+ f = astroid.body[0]
+ assert f.type_comment_args[0].parent is f
+ assert f.type_comment_returns.parent is f
+
+
def test_is_generator_for_yield_assignments():
node = astroid.extract_node(
"""
@@ -1334,5 +1347,16 @@ def test_is_generator_for_yield_in_if():
assert bool(node.is_generator())
+def test_is_generator_for_yield_in_aug_assign():
+ code = """
+ def test():
+ buf = ''
+ while True:
+ buf += yield
+ """
+ node = astroid.extract_node(code)
+ assert bool(node.is_generator())
+
+
if __name__ == "__main__":
unittest.main()
diff --git a/tests/unittest_regrtest.py b/tests/unittest_regrtest.py
index 45fbabf8..582e5072 100644
--- a/tests/unittest_regrtest.py
+++ b/tests/unittest_regrtest.py
@@ -92,7 +92,7 @@ class NonRegressionTests(resources.AstroidCacheSetupMixin, unittest.TestCase):
data = """
from numpy import multiply
-multiply(1, 2, 3)
+multiply([1, 2], [3, 4])
"""
astroid = builder.string_build(data, __name__, __file__)
callfunc = astroid.body[1].value.func