]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-119011: `type.__type_params__` now return an empty tuple (GH-119296) (...
authorJelle Zijlstra <jelle.zijlstra@gmail.com>
Tue, 28 May 2024 19:54:47 +0000 (12:54 -0700)
committerGitHub <noreply@github.com>
Tue, 28 May 2024 19:54:47 +0000 (19:54 +0000)
(cherry picked from commit 6b240c2308a044e38623900ccb8fa58c3549d4ae)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
Lib/test/test_functools.py
Lib/test/test_type_params.py
Misc/NEWS.d/next/Core and Builtins/2024-05-21-09-46-51.gh-issue-119011.WOe3bu.rst [new file with mode: 0644]
Objects/typeobject.c

index b73e48789425b78d724152b1eab9afcbc7823082..7f1b80a5e51ec46ed7f0f2ec9e59e95e86a7a196 100644 (file)
@@ -705,6 +705,14 @@ class TestUpdateWrapper(unittest.TestCase):
         self.assertTrue(wrapper.__doc__.startswith('max('))
         self.assertEqual(wrapper.__annotations__, {})
 
+    def test_update_type_wrapper(self):
+        def wrapper(*args): pass
+
+        functools.update_wrapper(wrapper, type)
+        self.assertEqual(wrapper.__name__, 'type')
+        self.assertEqual(wrapper.__annotations__, {})
+        self.assertEqual(wrapper.__type_params__, ())
+
 
 class TestWraps(TestUpdateWrapper):
 
index 38416315173bd4a5bdded0f909ffee0abb79fccc..ad5339ef4483ee349d1123aebefdf017c17ca15e 100644 (file)
@@ -499,6 +499,11 @@ class TypeParamsAccessTest(unittest.TestCase):
                                             r"Cannot use [a-z]+ in annotation scope within class scope"):
                     run_code(code.format(case))
 
+    def test_type_special_case(self):
+        # https://github.com/python/cpython/issues/119011
+        self.assertEqual(type.__type_params__, ())
+        self.assertEqual(object.__type_params__, ())
+
 
 def make_base(arg):
     class Base:
diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-05-21-09-46-51.gh-issue-119011.WOe3bu.rst b/Misc/NEWS.d/next/Core and Builtins/2024-05-21-09-46-51.gh-issue-119011.WOe3bu.rst
new file mode 100644 (file)
index 0000000..0083c18
--- /dev/null
@@ -0,0 +1,2 @@
+Fixes ``type.__type_params__`` to return an empty tuple instead of a
+descriptor.
index 0a21ec8dbb54afa7fd72b1a9e259d1b8b58a2a1d..bf2be42f73fdd4886f1788855f3d24c64801a3da 100644 (file)
@@ -1506,8 +1506,11 @@ type_set_annotations(PyTypeObject *type, PyObject *value, void *context)
 static PyObject *
 type_get_type_params(PyTypeObject *type, void *context)
 {
-    PyObject *params = PyDict_GetItemWithError(lookup_tp_dict(type), &_Py_ID(__type_params__));
+    if (type == &PyType_Type) {
+        return PyTuple_New(0);
+    }
 
+    PyObject *params = PyDict_GetItemWithError(lookup_tp_dict(type), &_Py_ID(__type_params__));
     if (params) {
         return Py_NewRef(params);
     }