Skip to content

Commit 0e15c31

Browse files
gh-98852: Fix subscription of type aliases (GH-98920)
Fix subscription of type aliases containing bare generic types or types like TypeVar: for example tuple[A, T][int] and tuple[TypeVar, T][int], where A is a generic type, and T is a type variable.
1 parent f5afb7f commit 0e15c31

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

Lib/test/test_typing.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3916,6 +3916,34 @@ class A:
39163916
# C version of GenericAlias
39173917
self.assertEqual(list[A()].__parameters__, (T,))
39183918

3919+
def test_non_generic_subscript(self):
3920+
T = TypeVar('T')
3921+
class G(Generic[T]):
3922+
pass
3923+
class A:
3924+
__parameters__ = (T,)
3925+
3926+
for s in (int, G, A, List, list,
3927+
TypeVar, TypeVarTuple, ParamSpec,
3928+
types.GenericAlias, types.UnionType):
3929+
3930+
for t in Tuple, tuple:
3931+
with self.subTest(tuple=t, sub=s):
3932+
self.assertEqual(t[s, T][int], t[s, int])
3933+
self.assertEqual(t[T, s][int], t[int, s])
3934+
a = t[s]
3935+
with self.assertRaises(TypeError):
3936+
a[int]
3937+
3938+
for c in Callable, collections.abc.Callable:
3939+
with self.subTest(callable=c, sub=s):
3940+
self.assertEqual(c[[s], T][int], c[[s], int])
3941+
self.assertEqual(c[[T], s][int], c[[int], s])
3942+
a = c[[s], s]
3943+
with self.assertRaises(TypeError):
3944+
a[int]
3945+
3946+
39193947
class ClassVarTests(BaseTestCase):
39203948

39213949
def test_basics(self):

Lib/typing.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,6 +1437,10 @@ def _determine_new_args(self, args):
14371437
new_args = []
14381438
for old_arg in self.__args__:
14391439

1440+
if isinstance(old_arg, type):
1441+
new_args.append(old_arg)
1442+
continue
1443+
14401444
substfunc = getattr(old_arg, '__typing_subst__', None)
14411445
if substfunc:
14421446
new_arg = substfunc(new_arg_by_param[old_arg])
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fix subscription of type aliases containing bare generic types or types like
2+
:class:`~typing.TypeVar`: for example ``tuple[A, T][int]`` and
3+
``tuple[TypeVar, T][int]``, where ``A`` is a generic type, and ``T`` is a
4+
type variable.

Objects/genericaliasobject.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,13 @@ _Py_subs_parameters(PyObject *self, PyObject *args, PyObject *parameters, PyObje
458458
}
459459
for (Py_ssize_t iarg = 0, jarg = 0; iarg < nargs; iarg++) {
460460
PyObject *arg = PyTuple_GET_ITEM(args, iarg);
461+
if (PyType_Check(arg)) {
462+
Py_INCREF(arg);
463+
PyTuple_SET_ITEM(newargs, jarg, arg);
464+
jarg++;
465+
continue;
466+
}
467+
461468
int unpack = _is_unpacked_typevartuple(arg);
462469
if (unpack < 0) {
463470
Py_DECREF(newargs);

0 commit comments

Comments
 (0)