]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-124498: Fix `TypeAliasType` not to be generic, when `type_params=()` (GH...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Mon, 30 Sep 2024 01:01:06 +0000 (03:01 +0200)
committerGitHub <noreply@github.com>
Mon, 30 Sep 2024 01:01:06 +0000 (18:01 -0700)
gh-124498: Fix `TypeAliasType` not to be generic, when `type_params=()` (GH-124499)
(cherry picked from commit abe5f799e6ce1d177f79554f1b84d348b6141045)

Co-authored-by: sobolevn <mail@sobolevn.me>
Lib/test/test_type_aliases.py
Misc/NEWS.d/next/Library/2024-09-25-12-14-58.gh-issue-124498.Ozxs55.rst [new file with mode: 0644]
Objects/typevarobject.c

index f8b395fdc8bb1d4552c3c92e9d9bc326ff29d79e..4c17933e7f7c235264c8ecdaef45a360ac444b4a 100644 (file)
@@ -212,6 +212,19 @@ class TypeAliasConstructorTest(unittest.TestCase):
         self.assertEqual(TA.__value__, list[T])
         self.assertEqual(TA.__type_params__, (T,))
         self.assertEqual(TA.__module__, __name__)
+        self.assertIs(type(TA[int]), types.GenericAlias)
+
+    def test_not_generic(self):
+        TA = TypeAliasType("TA", list[int], type_params=())
+        self.assertEqual(TA.__name__, "TA")
+        self.assertEqual(TA.__value__, list[int])
+        self.assertEqual(TA.__type_params__, ())
+        self.assertEqual(TA.__module__, __name__)
+        with self.assertRaisesRegex(
+            TypeError,
+            "Only generic type aliases are subscriptable",
+        ):
+            TA[int]
 
     def test_keywords(self):
         TA = TypeAliasType(name="TA", value=int)
diff --git a/Misc/NEWS.d/next/Library/2024-09-25-12-14-58.gh-issue-124498.Ozxs55.rst b/Misc/NEWS.d/next/Library/2024-09-25-12-14-58.gh-issue-124498.Ozxs55.rst
new file mode 100644 (file)
index 0000000..4dbf4eb
--- /dev/null
@@ -0,0 +1,2 @@
+Fix :class:`typing.TypeAliasType` not to be generic, when ``type_params`` is
+an empty tuple.
index c8ab14053de4183ea1fdaf97136e9c323a375849..f3e3ed0c9afae7e75b7a8cac46aeb6ae4f5cab3c 100644 (file)
@@ -1640,7 +1640,16 @@ typealias_alloc(PyObject *name, PyObject *type_params, PyObject *compute_value,
         return NULL;
     }
     ta->name = Py_NewRef(name);
-    ta->type_params = Py_IsNone(type_params) ? NULL : Py_XNewRef(type_params);
+    if (
+        type_params == NULL
+        || Py_IsNone(type_params)
+        || (PyTuple_Check(type_params) && PyTuple_GET_SIZE(type_params) == 0)
+    ) {
+        ta->type_params = NULL;
+    }
+    else {
+        ta->type_params = Py_NewRef(type_params);
+    }
     ta->compute_value = Py_XNewRef(compute_value);
     ta->value = Py_XNewRef(value);
     ta->module = Py_XNewRef(module);