Skip to content

Commit 4659026

Browse files
[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 abe5f79) Co-authored-by: sobolevn <mail@sobolevn.me>
1 parent 42432e5 commit 4659026

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

Lib/test/test_type_aliases.py

+13
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,19 @@ def test_generic(self):
212212
self.assertEqual(TA.__value__, list[T])
213213
self.assertEqual(TA.__type_params__, (T,))
214214
self.assertEqual(TA.__module__, __name__)
215+
self.assertIs(type(TA[int]), types.GenericAlias)
216+
217+
def test_not_generic(self):
218+
TA = TypeAliasType("TA", list[int], type_params=())
219+
self.assertEqual(TA.__name__, "TA")
220+
self.assertEqual(TA.__value__, list[int])
221+
self.assertEqual(TA.__type_params__, ())
222+
self.assertEqual(TA.__module__, __name__)
223+
with self.assertRaisesRegex(
224+
TypeError,
225+
"Only generic type aliases are subscriptable",
226+
):
227+
TA[int]
215228

216229
def test_keywords(self):
217230
TA = TypeAliasType(name="TA", value=int)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix :class:`typing.TypeAliasType` not to be generic, when ``type_params`` is
2+
an empty tuple.

Objects/typevarobject.c

+10-1
Original file line numberDiff line numberDiff line change
@@ -1364,7 +1364,16 @@ typealias_alloc(PyObject *name, PyObject *type_params, PyObject *compute_value,
13641364
return NULL;
13651365
}
13661366
ta->name = Py_NewRef(name);
1367-
ta->type_params = Py_IsNone(type_params) ? NULL : Py_XNewRef(type_params);
1367+
if (
1368+
type_params == NULL
1369+
|| Py_IsNone(type_params)
1370+
|| (PyTuple_Check(type_params) && PyTuple_GET_SIZE(type_params) == 0)
1371+
) {
1372+
ta->type_params = NULL;
1373+
}
1374+
else {
1375+
ta->type_params = Py_NewRef(type_params);
1376+
}
13681377
ta->compute_value = Py_XNewRef(compute_value);
13691378
ta->value = Py_XNewRef(value);
13701379
ta->module = Py_XNewRef(module);

0 commit comments

Comments
 (0)