mirror of https://github.com/python/cpython.git
bpo-44761: Change default value of NewType __module__ attr (GH-27406)
This commit is contained in:
parent
6ff8903809
commit
7b975f81e4
|
@ -3777,6 +3777,12 @@ def test_pickle(self):
|
||||||
with self.assertRaises(pickle.PicklingError):
|
with self.assertRaises(pickle.PicklingError):
|
||||||
pickle.dumps(UserAge, proto)
|
pickle.dumps(UserAge, proto)
|
||||||
|
|
||||||
|
def test_missing__name__(self):
|
||||||
|
code = ("import typing\n"
|
||||||
|
"NT = typing.NewType('NT', int)\n"
|
||||||
|
)
|
||||||
|
exec(code, {})
|
||||||
|
|
||||||
|
|
||||||
class NewTypePythonTests(NewTypeTests, BaseTestCase):
|
class NewTypePythonTests(NewTypeTests, BaseTestCase):
|
||||||
module = py_typing
|
module = py_typing
|
||||||
|
|
|
@ -1387,11 +1387,11 @@ def _no_init(self, *args, **kwargs):
|
||||||
if type(self)._is_protocol:
|
if type(self)._is_protocol:
|
||||||
raise TypeError('Protocols cannot be instantiated')
|
raise TypeError('Protocols cannot be instantiated')
|
||||||
|
|
||||||
def _callee(depth=2, default=None):
|
def _caller(depth=1, default='__main__'):
|
||||||
try:
|
try:
|
||||||
return sys._getframe(depth).f_globals['__name__']
|
return sys._getframe(depth + 1).f_globals.get('__name__', default)
|
||||||
except (AttributeError, ValueError): # For platforms without _getframe()
|
except (AttributeError, ValueError): # For platforms without _getframe()
|
||||||
return default
|
return None
|
||||||
|
|
||||||
|
|
||||||
def _allow_reckless_class_checks(depth=3):
|
def _allow_reckless_class_checks(depth=3):
|
||||||
|
@ -2395,8 +2395,10 @@ def __init__(self, name, tp):
|
||||||
if '.' in name:
|
if '.' in name:
|
||||||
name = name.rpartition('.')[-1]
|
name = name.rpartition('.')[-1]
|
||||||
self.__name__ = name
|
self.__name__ = name
|
||||||
self.__module__ = _callee(default='typing')
|
|
||||||
self.__supertype__ = tp
|
self.__supertype__ = tp
|
||||||
|
def_mod = _caller()
|
||||||
|
if def_mod != 'typing':
|
||||||
|
self.__module__ = def_mod
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return f'{self.__module__}.{self.__qualname__}'
|
return f'{self.__module__}.{self.__qualname__}'
|
||||||
|
|
Loading…
Reference in New Issue