From 531338fa08e1a96ce357df056d27f10859cf16e9 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Wed, 14 Jul 2021 18:51:59 +0300 Subject: [PATCH] bpo-44632: Fix support of TypeVar in the union type int | TypeVar('T') returns now an instance of types.Union instead of typing.Union. --- Lib/test/test_types.py | 8 +++++++- Objects/unionobject.c | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py index f2c64649e1b670..a18629779a3794 100644 --- a/Lib/test/test_types.py +++ b/Lib/test/test_types.py @@ -756,6 +756,7 @@ def test_or_type_operator_with_SpecialForm(self): def test_or_type_repr(self): assert repr(int | 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] @@ -792,13 +793,18 @@ def __eq__(self, other): 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): diff --git a/Objects/unionobject.c b/Objects/unionobject.c index cf04de1bcc8095..f998b3474152c7 100644 --- a/Objects/unionobject.c +++ b/Objects/unionobject.c @@ -133,7 +133,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 *