https://github.com/python/cpython/commit/4659026e1d90a6510a4bb2cf548688046a258de0
commit: 4659026e1d90a6510a4bb2cf548688046a258de0
branch: 3.12
author: Miss Islington (bot) <[email protected]>
committer: sobolevn <[email protected]>
date: 2024-09-26T14:46:29Z
summary:

[3.12] gh-124498: Fix `TypeAliasType` not to be generic, when `type_params=()` 
(GH-124499) (#124604)

gh-124498: Fix `TypeAliasType` not to be generic, when `type_params=()` 
(GH-124499)
(cherry picked from commit abe5f799e6ce1d177f79554f1b84d348b6141045)

Co-authored-by: sobolevn <[email protected]>

files:
A Misc/NEWS.d/next/Library/2024-09-25-12-14-58.gh-issue-124498.Ozxs55.rst
M Lib/test/test_type_aliases.py
M Objects/typevarobject.c

diff --git a/Lib/test/test_type_aliases.py b/Lib/test/test_type_aliases.py
index f8b395fdc8bb1d..4c17933e7f7c23 100644
--- a/Lib/test/test_type_aliases.py
+++ b/Lib/test/test_type_aliases.py
@@ -212,6 +212,19 @@ def test_generic(self):
         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
index 00000000000000..4dbf4eb709733d
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-09-25-12-14-58.gh-issue-124498.Ozxs55.rst
@@ -0,0 +1,2 @@
+Fix :class:`typing.TypeAliasType` not to be generic, when ``type_params`` is
+an empty tuple.
diff --git a/Objects/typevarobject.c b/Objects/typevarobject.c
index db9c2191d60090..4f0bc09637bc39 100644
--- a/Objects/typevarobject.c
+++ b/Objects/typevarobject.c
@@ -1364,7 +1364,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);

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to