summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2021-07-30 13:48:01 (GMT)
committerGitHub <noreply@github.com>2021-07-30 13:48:01 (GMT)
commit56122b0bba7a9e2e3ec672a6c22bfdd7ecf08cbe (patch)
tree454a748194c2d0f7b60d66362589e28c0ca2ce51 /Lib
parent0cb470e622ba37920c72e4d8f284741b9fbaea8b (diff)
downloadcpython-56122b0bba7a9e2e3ec672a6c22bfdd7ecf08cbe.zip
cpython-56122b0bba7a9e2e3ec672a6c22bfdd7ecf08cbe.tar.gz
cpython-56122b0bba7a9e2e3ec672a6c22bfdd7ecf08cbe.tar.bz2
bpo-44761: Change default value of NewType __module__ attr (GH-27406) (GH-27477)
(cherry picked from commit 7b975f81e4dba70a42c6279539a7fcfe4211b4c0) Co-authored-by: Yurii Karabas <1998uriyyo@gmail.com>
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_typing.py6
-rw-r--r--Lib/typing.py10
2 files changed, 12 insertions, 4 deletions
diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py
index bbda6aa..3bc9b9c 100644
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -3752,6 +3752,12 @@ class NewTypeTests(BaseTestCase):
with self.assertRaises(pickle.PicklingError):
pickle.dumps(UserAge, proto)
+ def test_missing__name__(self):
+ code = ("import typing\n"
+ "NT = typing.NewType('NT', int)\n"
+ )
+ exec(code, {})
+
class NamedTupleTests(BaseTestCase):
class NestedEmployee(NamedTuple):
diff --git a/Lib/typing.py b/Lib/typing.py
index 2c2d8ec..bcb22b2 100644
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -1379,11 +1379,11 @@ def _no_init(self, *args, **kwargs):
if type(self)._is_protocol:
raise TypeError('Protocols cannot be instantiated')
-def _callee(depth=2, default=None):
+def _caller(depth=1, default='__main__'):
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()
- return default
+ return None
def _allow_reckless_class_checks(depth=3):
@@ -2385,8 +2385,10 @@ class NewType:
if '.' in name:
name = name.rpartition('.')[-1]
self.__name__ = name
- self.__module__ = _callee(default='typing')
self.__supertype__ = tp
+ def_mod = _caller()
+ if def_mod != 'typing':
+ self.__module__ = def_mod
def __repr__(self):
return f'{self.__module__}.{self.__qualname__}'