]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-44632: Fix support of TypeVar in the union type (GH-27139)
authorSerhiy Storchaka <storchaka@gmail.com>
Wed, 14 Jul 2021 17:09:15 +0000 (20:09 +0300)
committerGitHub <noreply@github.com>
Wed, 14 Jul 2021 17:09:15 +0000 (20:09 +0300)
int | TypeVar('T') returns now an instance of types.Union
instead of typing.Union.

Lib/test/test_types.py
Objects/unionobject.c

index bc7e6a6613b6edda72c19c44eb197ba56ce2dd95..8f529a052f40e58ce8fc2f13583385954cdb8041 100644 (file)
@@ -769,6 +769,7 @@ class TypesTests(unittest.TestCase):
         assert repr(int | None) == "int | None"
         assert repr(int | type(None)) == "int | None"
         assert repr(int | typing.GenericAlias(list, int)) == "int | list[int]"
+        assert repr(int | typing.TypeVar('T')) == "int | ~T"
 
     def test_or_type_operator_with_genericalias(self):
         a = list[int]
@@ -805,13 +806,18 @@ class TypesTests(unittest.TestCase):
                     issubclass(int, type_)
 
     def test_or_type_operator_with_bad_module(self):
-        class TypeVar:
+        class BadMeta(type):
+            __qualname__ = 'TypeVar'
             @property
             def __module__(self):
                 1 / 0
+        TypeVar = BadMeta('TypeVar', (), {})
+        _SpecialForm = BadMeta('_SpecialForm', (), {})
         # Crashes in Issue44483
         with self.assertRaises(ZeroDivisionError):
             str | TypeVar()
+        with self.assertRaises(ZeroDivisionError):
+            str | _SpecialForm()
 
     @cpython_only
     def test_or_type_operator_reference_cycle(self):
index bdcdb747459db6a4493efbf69b771df4307e843e..8818cc2bf49d06b9f34f6c58db23cf4b6a75fa87 100644 (file)
@@ -127,7 +127,7 @@ is_typing_name(PyObject *obj, char *name)
     if (strcmp(type->tp_name, name) != 0) {
         return 0;
     }
-    return is_typing_module(obj);
+    return is_typing_module((PyObject *)type);
 }
 
 static PyObject *