[Python-checkins] bpo-44632: Fix support of TypeVar in the union type (GH-27139) (GH-27143)
serhiy-storchaka
webhook-mailer at python.org
Thu Jul 15 03:25:41 EDT 2021
https://github.com/python/cpython/commit/cc1a47c849a206441c9b370b6ca954862a523082
commit: cc1a47c849a206441c9b370b6ca954862a523082
branch: 3.10
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: serhiy-storchaka <storchaka at gmail.com>
date: 2021-07-15T10:25:22+03:00
summary:
bpo-44632: Fix support of TypeVar in the union type (GH-27139) (GH-27143)
int | TypeVar('T') returns now an instance of types.Union
instead of typing.Union.
(cherry picked from commit a158b20019b50e3ece6e4743ec4e6ae8d818b690)
Co-authored-by: Serhiy Storchaka <storchaka at gmail.com>
files:
M Lib/test/test_types.py
M Objects/unionobject.c
diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py
index 5254d5894b3d6..3f7677108ef2a 100644
--- a/Lib/test/test_types.py
+++ b/Lib/test/test_types.py
@@ -759,6 +759,7 @@ def test_or_type_repr(self):
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]
@@ -795,13 +796,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 98db27d435727..229b518ea7575 100644
--- a/Objects/unionobject.c
+++ b/Objects/unionobject.c
@@ -124,7 +124,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 *
More information about the Python-checkins
mailing list