]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-44761: Change default value of NewType __module__ attr (GH-27406) (GH-27477)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Fri, 30 Jul 2021 13:48:01 +0000 (06:48 -0700)
committerGitHub <noreply@github.com>
Fri, 30 Jul 2021 13:48:01 +0000 (15:48 +0200)
(cherry picked from commit 7b975f81e4dba70a42c6279539a7fcfe4211b4c0)

Co-authored-by: Yurii Karabas <1998uriyyo@gmail.com>
Lib/test/test_typing.py
Lib/typing.py

index bbda6aa2f13b5004eba30e75bdd739dcb31e37d3..3bc9b9cb8e6ed9db4a2c01103de64e2afd803f17 100644 (file)
@@ -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):
index 2c2d8ec1a1e961b5e0bd26033a2df97a31ac5d38..bcb22b2d178c39df056ba185a90e04ca34935c6c 100644 (file)
@@ -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__}'