mirror of
https://github.com/python/cpython.git
synced 2025-09-27 02:39:58 +00:00
bpo-39942:Fix failure in TypeVar
when missing __name__
(GH-19616)
https://bugs.python.org/issue39942
This commit is contained in:
parent
eba9f6155d
commit
a25a04fea5
3 changed files with 13 additions and 1 deletions
|
@ -221,6 +221,13 @@ class TypeVarTests(BaseTestCase):
|
||||||
with self.assertRaises(TypeError):
|
with self.assertRaises(TypeError):
|
||||||
TypeVar('X', str, float, bound=Employee)
|
TypeVar('X', str, float, bound=Employee)
|
||||||
|
|
||||||
|
def test_missing__name__(self):
|
||||||
|
# See bpo-39942
|
||||||
|
code = ("import typing\n"
|
||||||
|
"T = typing.TypeVar('T')\n"
|
||||||
|
)
|
||||||
|
exec(code, {})
|
||||||
|
|
||||||
def test_no_bivariant(self):
|
def test_no_bivariant(self):
|
||||||
with self.assertRaises(ValueError):
|
with self.assertRaises(ValueError):
|
||||||
TypeVar('T', covariant=True, contravariant=True)
|
TypeVar('T', covariant=True, contravariant=True)
|
||||||
|
|
|
@ -606,7 +606,10 @@ class TypeVar(_Final, _Immutable, _root=True):
|
||||||
self.__bound__ = _type_check(bound, "Bound must be a type.")
|
self.__bound__ = _type_check(bound, "Bound must be a type.")
|
||||||
else:
|
else:
|
||||||
self.__bound__ = None
|
self.__bound__ = None
|
||||||
def_mod = sys._getframe(1).f_globals['__name__'] # for pickling
|
try:
|
||||||
|
def_mod = sys._getframe(1).f_globals.get('__name__', '__main__') # for pickling
|
||||||
|
except (AttributeError, ValueError):
|
||||||
|
def_mod = None
|
||||||
if def_mod != 'typing':
|
if def_mod != 'typing':
|
||||||
self.__module__ = def_mod
|
self.__module__ = def_mod
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Set "__main__" as the default module name when "__name__" is missing in
|
||||||
|
:class:`typing.TypeVar`. Patch by Weipeng Hong.
|
Loading…
Add table
Add a link
Reference in a new issue