[Python-checkins] gh-98852: Fix subscription of type aliases (GH-98920)

miss-islington webhook-mailer at python.org
Tue Nov 1 04:02:05 EDT 2022


https://github.com/python/cpython/commit/20c258c6928b878e0cb63f6b792e904e18f2242f
commit: 20c258c6928b878e0cb63f6b792e904e18f2242f
branch: 3.11
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2022年11月01日T01:01:28-07:00
summary:
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 0e15c31c7e9907fdbe38a3f419b669fed5bb3b33)
Co-authored-by: Serhiy Storchaka <storchaka at gmail.com>
files:
A Misc/NEWS.d/next/Core and Builtins/2022-10-31-21-01-35.gh-issue-98852.MYaRN6.rst
M Lib/test/test_typing.py
M Lib/typing.py
M Objects/genericaliasobject.c
diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py
index 50fe679c6a31..3b0d76754635 100644
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -3912,6 +3912,34 @@ class A:
 # C version of GenericAlias
 self.assertEqual(list[A()].__parameters__, (T,))
 
+ def test_non_generic_subscript(self):
+ T = TypeVar('T')
+ class G(Generic[T]):
+ pass
+ class A:
+ __parameters__ = (T,)
+
+ for s in (int, G, A, List, list,
+ TypeVar, TypeVarTuple, ParamSpec,
+ types.GenericAlias, types.UnionType):
+
+ for t in Tuple, tuple:
+ with self.subTest(tuple=t, sub=s):
+ self.assertEqual(t[s, T][int], t[s, int])
+ self.assertEqual(t[T, s][int], t[int, s])
+ a = t[s]
+ with self.assertRaises(TypeError):
+ a[int]
+
+ for c in Callable, collections.abc.Callable:
+ with self.subTest(callable=c, sub=s):
+ self.assertEqual(c[[s], T][int], c[[s], int])
+ self.assertEqual(c[[T], s][int], c[[int], s])
+ a = c[[s], s]
+ with self.assertRaises(TypeError):
+ a[int]
+
+
 class ClassVarTests(BaseTestCase):
 
 def test_basics(self):
diff --git a/Lib/typing.py b/Lib/typing.py
index 1e335bb7204d..9c4d653372d4 100644
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -1425,6 +1425,10 @@ def _determine_new_args(self, args):
 new_args = []
 for old_arg in self.__args__:
 
+ if isinstance(old_arg, type):
+ new_args.append(old_arg)
+ continue
+
 substfunc = getattr(old_arg, '__typing_subst__', None)
 if substfunc:
 new_arg = substfunc(new_arg_by_param[old_arg])
diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-10-31-21-01-35.gh-issue-98852.MYaRN6.rst b/Misc/NEWS.d/next/Core and Builtins/2022-10-31-21-01-35.gh-issue-98852.MYaRN6.rst
new file mode 100644
index 000000000000..0e15819a862b
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2022-10-31-21-01-35.gh-issue-98852.MYaRN6.rst	
@@ -0,0 +1,4 @@
+Fix subscription of type aliases containing bare generic types or types like
+:class:`~typing.TypeVar`: for example ``tuple[A, T][int]`` and
+``tuple[TypeVar, T][int]``, where ``A`` is a generic type, and ``T`` is a
+type variable.
diff --git a/Objects/genericaliasobject.c b/Objects/genericaliasobject.c
index 19f011fd3a74..77acd1bc57a5 100644
--- a/Objects/genericaliasobject.c
+++ b/Objects/genericaliasobject.c
@@ -458,6 +458,13 @@ _Py_subs_parameters(PyObject *self, PyObject *args, PyObject *parameters, PyObje
 }
 for (Py_ssize_t iarg = 0, jarg = 0; iarg < nargs; iarg++) {
 PyObject *arg = PyTuple_GET_ITEM(args, iarg);
+ if (PyType_Check(arg)) {
+ Py_INCREF(arg);
+ PyTuple_SET_ITEM(newargs, jarg, arg);
+ jarg++;
+ continue;
+ }
+
 int unpack = _is_unpacked_typevartuple(arg);
 if (unpack < 0) {
 Py_DECREF(newargs);


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /