]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-44761: Change default value of NewType __module__ attr (GH-27406)
authorYurii Karabas <1998uriyyo@gmail.com>
Fri, 30 Jul 2021 12:56:12 +0000 (15:56 +0300)
committerGitHub <noreply@github.com>
Fri, 30 Jul 2021 12:56:12 +0000 (14:56 +0200)
Lib/test/test_typing.py
Lib/typing.py

index bc9021addbe3538299c6b4d18db63b8e9f4d90d2..06bd49b593a77bf7f755b2843850abb82f529f92 100644 (file)
@@ -3777,6 +3777,12 @@ class NewTypeTests:
                 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 NewTypePythonTests(NewTypeTests, BaseTestCase):
     module = py_typing
index f07b00f32ece969426a511cb65a6da3d27b50832..2826525efff7aaf4b51e7a7fe787a30ef7c1d5ca 100644 (file)
@@ -1387,11 +1387,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):
@@ -2395,8 +2395,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__}'