diff options
Diffstat (limited to 'tests/unittest_brain.py')
-rw-r--r-- | tests/unittest_brain.py | 93 |
1 files changed, 90 insertions, 3 deletions
diff --git a/tests/unittest_brain.py b/tests/unittest_brain.py index 0a833664..e6ff6928 100644 --- a/tests/unittest_brain.py +++ b/tests/unittest_brain.py @@ -18,11 +18,12 @@ # Copyright (c) 2018 Anthony Sottile <asottile@umich.edu> # Copyright (c) 2018 Ioana Tagirta <ioana.tagirta@gmail.com> # Copyright (c) 2018 Ahmed Azzaoui <ahmed.azzaoui@engie.com> +# Copyright (c) 2019-2020 Bryce Guinta <bryce.guinta@protonmail.com> # Copyright (c) 2019 Ashley Whetter <ashley@awhetter.co.uk> # Copyright (c) 2019 Tomas Novak <ext.Tomas.Novak@skoda-auto.cz> # Copyright (c) 2019 Hugo van Kemenade <hugovk@users.noreply.github.com> # Copyright (c) 2019 Grygorii Iermolenko <gyermolenko@gmail.com> -# Copyright (c) 2019 Bryce Guinta <bryce.guinta@protonmail.com> +# Copyright (c) 2020 Peter Kolbus <peter.kolbus@gmail.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 @@ -540,8 +541,10 @@ class MultiprocessingBrainTest(unittest.TestCase): obj = next(module[attr].infer()) self.assertEqual(obj.qname(), "{}.{}".format(bases.BUILTINS, attr)) - array = next(module["array"].infer()) - self.assertEqual(array.qname(), "array.array") + # pypy's implementation of array.__spec__ return None. This causes problems for this inference. + if not hasattr(sys, "pypy_version_info"): + array = next(module["array"].infer()) + self.assertEqual(array.qname(), "array.array") manager = next(module["manager"].infer()) # Verify that we have these attributes @@ -839,6 +842,16 @@ class EnumBrainTest(unittest.TestCase): assert inferred_tuple_node.as_string() == "(1, 2)" assert inferred_list_node.as_string() == "[2, 4]" + def test_enum_starred_is_skipped(self): + code = """ + from enum import Enum + class ContentType(Enum): + TEXT, PHOTO, VIDEO, GIF, YOUTUBE, *_ = [1, 2, 3, 4, 5, 6] + ContentType.TEXT #@ + """ + node = astroid.extract_node(code) + next(node.infer()) + @unittest.skipUnless(HAS_DATEUTIL, "This test requires the dateutil library.") class DateutilBrainTest(unittest.TestCase): @@ -924,6 +937,42 @@ 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): @@ -1279,6 +1328,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""" @@ -2020,5 +2076,36 @@ def test_dataclasses(): assert isinstance(name[0], astroid.Unknown) +@pytest.mark.parametrize( + "code,expected_class,expected_value", + [ + ("'hey'.encode()", astroid.Const, b""), + ("b'hey'.decode()", astroid.Const, ""), + ("'hey'.encode().decode()", astroid.Const, ""), + ], +) +def test_str_and_bytes(code, expected_class, expected_value): + node = astroid.extract_node(code) + inferred = next(node.infer()) + assert isinstance(inferred, expected_class) + assert inferred.value == expected_value + + +def test_no_recursionerror_on_self_referential_length_check(): + """ + Regression test for https://github.com/PyCQA/astroid/issues/777 + """ + with pytest.raises(astroid.InferenceError): + node = astroid.extract_node( + """ + class Crash: + def __len__(self) -> int: + return len(self) + len(Crash()) #@ + """ + ) + node.inferred() + + if __name__ == "__main__": unittest.main() |