Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 20c258c

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. (cherry picked from commit 0e15c31) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 217a317 commit 20c258c

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
@@ -3912,6 +3912,34 @@ class A:
39123912
# C version of GenericAlias
39133913
self.assertEqual(list[A()].__parameters__, (T,))
39143914

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

39173945
def test_basics(self):

Lib/typing.py

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

1428+
if isinstance(old_arg, type):
1429+
new_args.append(old_arg)
1430+
continue
1431+
14281432
substfunc = getattr(old_arg, '__typing_subst__', None)
14291433
if substfunc:
14301434
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)