From bfad34705828d0b211c7908e204de4942f04d538 Mon Sep 17 00:00:00 2001 From: hippo91 Date: Mon, 18 Jan 2021 19:13:22 +0100 Subject: Adds unittest for brain_type module --- tests/unittest_brain.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'tests') diff --git a/tests/unittest_brain.py b/tests/unittest_brain.py index 7bb1f70f..0190ed3f 100644 --- a/tests/unittest_brain.py +++ b/tests/unittest_brain.py @@ -937,6 +937,33 @@ class IOBrainTest(unittest.TestCase): self.assertEqual(raw.name, "FileIO") +@test_utils.require_version("3.9") +class TypeBrain(unittest.TestCase): + def test_type_subscript(self): + 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): + 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): -- cgit v1.2.1 From f184c8af7581b6d1c56270f1eb66254175978595 Mon Sep 17 00:00:00 2001 From: hippo91 Date: Tue, 19 Jan 2021 20:46:30 +0100 Subject: Adds doc --- tests/unittest_brain.py | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'tests') diff --git a/tests/unittest_brain.py b/tests/unittest_brain.py index 0190ed3f..443fcb49 100644 --- a/tests/unittest_brain.py +++ b/tests/unittest_brain.py @@ -940,6 +940,10 @@ class IOBrainTest(unittest.TestCase): @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 @@ -952,6 +956,11 @@ class TypeBrain(unittest.TestCase): 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" -- cgit v1.2.1 From 01c678ad8a94be36ca316d63d08b1b79ab82ea11 Mon Sep 17 00:00:00 2001 From: hippo91 Date: Wed, 20 Jan 2021 18:52:32 +0100 Subject: Formatting according to black --- tests/unittest_brain.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/unittest_brain.py b/tests/unittest_brain.py index 443fcb49..9e5e1b7c 100644 --- a/tests/unittest_brain.py +++ b/tests/unittest_brain.py @@ -952,7 +952,7 @@ class TypeBrain(unittest.TestCase): 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] + meth_inf = val_inf.getattr("__class_getitem__")[0] self.assertIsInstance(meth_inf, astroid.FunctionDef) def test_invalid_type_subscript(self): @@ -970,7 +970,7 @@ class TypeBrain(unittest.TestCase): 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] + meth_inf = val_inf.getattr("__class_getitem__")[0] @test_utils.require_version("3.6") -- cgit v1.2.1