Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7b975f8

Browse files
authored
bpo-44761: Change default value of NewType __module__ attr (GH-27406)
1 parent 6ff8903 commit 7b975f8

2 files changed

Lines changed: 12 additions & 4 deletions

File tree

Lib/test/test_typing.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3777,6 +3777,12 @@ def test_pickle(self):
37773777
with self.assertRaises(pickle.PicklingError):
37783778
pickle.dumps(UserAge, proto)
37793779

3780+
def test_missing__name__(self):
3781+
code = ("import typing\n"
3782+
"NT = typing.NewType('NT', int)\n"
3783+
)
3784+
exec(code, {})
3785+
37803786

37813787
class NewTypePythonTests(NewTypeTests, BaseTestCase):
37823788
module = py_typing

Lib/typing.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,11 +1387,11 @@ def _no_init(self, *args, **kwargs):
13871387
if type(self)._is_protocol:
13881388
raise TypeError('Protocols cannot be instantiated')
13891389

1390-
def _callee(depth=2, default=None):
1390+
def _caller(depth=1, default='__main__'):
13911391
try:
1392-
return sys._getframe(depth).f_globals['__name__']
1392+
return sys._getframe(depth + 1).f_globals.get('__name__', default)
13931393
except (AttributeError, ValueError): # For platforms without _getframe()
1394-
return default
1394+
return None
13951395

13961396

13971397
def _allow_reckless_class_checks(depth=3):
@@ -2395,8 +2395,10 @@ def __init__(self, name, tp):
23952395
if '.' in name:
23962396
name = name.rpartition('.')[-1]
23972397
self.__name__ = name
2398-
self.__module__ = _callee(default='typing')
23992398
self.__supertype__ = tp
2399+
def_mod = _caller()
2400+
if def_mod != 'typing':
2401+
self.__module__ = def_mod
24002402

24012403
def __repr__(self):
24022404
return f'{self.__module__}.{self.__qualname__}'

0 commit comments

Comments
 (0)