]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-107905: Test raising `__value__` for `TypeAliasType` (#107997)
authorNikita Sobolev <mail@sobolevn.me>
Mon, 21 Aug 2023 13:52:37 +0000 (16:52 +0300)
committerGitHub <noreply@github.com>
Mon, 21 Aug 2023 13:52:37 +0000 (13:52 +0000)
Lib/test/test_type_aliases.py

index 0ce97f57de6860961bee2c07696daddd0dc9cb9b..8f0a998e1f3dc1786664ff5dc0a1cf0a5719c26b 100644 (file)
@@ -168,6 +168,24 @@ class TypeParamsAliasValueTest(unittest.TestCase):
         self.assertEqual(repr(GenericRecursive[GenericRecursive[int]]),
                          "GenericRecursive[GenericRecursive[int]]")
 
+    def test_raising(self):
+        type MissingName = list[_My_X]
+        with self.assertRaisesRegex(
+            NameError,
+            "cannot access free variable '_My_X' where it is not associated with a value",
+        ):
+            MissingName.__value__
+        _My_X = int
+        self.assertEqual(MissingName.__value__, list[int])
+        del _My_X
+        # Cache should still work:
+        self.assertEqual(MissingName.__value__, list[int])
+
+        # Explicit exception:
+        type ExprException = 1 / 0
+        with self.assertRaises(ZeroDivisionError):
+            ExprException.__value__
+
 
 class TypeAliasConstructorTest(unittest.TestCase):
     def test_basic(self):