Skip to content

Commit a158b20

Browse files
bpo-44632: Fix support of TypeVar in the union type (GH-27139)
int | TypeVar('T') returns now an instance of types.Union instead of typing.Union.
1 parent b81cac0 commit a158b20

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

Lib/test/test_types.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,7 @@ def test_or_type_repr(self):
769769
assert repr(int | None) == "int | None"
770770
assert repr(int | type(None)) == "int | None"
771771
assert repr(int | typing.GenericAlias(list, int)) == "int | list[int]"
772+
assert repr(int | typing.TypeVar('T')) == "int | ~T"
772773

773774
def test_or_type_operator_with_genericalias(self):
774775
a = list[int]
@@ -805,13 +806,18 @@ def __eq__(self, other):
805806
issubclass(int, type_)
806807

807808
def test_or_type_operator_with_bad_module(self):
808-
class TypeVar:
809+
class BadMeta(type):
810+
__qualname__ = 'TypeVar'
809811
@property
810812
def __module__(self):
811813
1 / 0
814+
TypeVar = BadMeta('TypeVar', (), {})
815+
_SpecialForm = BadMeta('_SpecialForm', (), {})
812816
# Crashes in Issue44483
813817
with self.assertRaises(ZeroDivisionError):
814818
str | TypeVar()
819+
with self.assertRaises(ZeroDivisionError):
820+
str | _SpecialForm()
815821

816822
@cpython_only
817823
def test_or_type_operator_reference_cycle(self):

Objects/unionobject.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ is_typing_name(PyObject *obj, char *name)
127127
if (strcmp(type->tp_name, name) != 0) {
128128
return 0;
129129
}
130-
return is_typing_module(obj);
130+
return is_typing_module((PyObject *)type);
131131
}
132132

133133
static PyObject *

0 commit comments

Comments
 (0)