10000 gh-88965: Fix type substitution of a list of types after initial `ParamSpec` substitution by sobolevn · Pull Request #102808 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-88965: Fix type substitution of a list of types after initial ParamSpec substitution #102808

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -7679,6 +7679,71 @@ def test_bad_var_substitution(self):
with self.assertRaises(TypeError):
collections.abc.Callable[P, T][arg, str]

def test_type_var_subst_for_other_type_vars(self):
T = TypeVar('T')
T2 = TypeVar('T2')
P = ParamSpec('P')
P2 = ParamSpec('P2')
Ts = TypeVarTuple('Ts')

class Base(Generic[P]):
pass

A1 = Base[T]
self.assertEqual(A1.__parameters__, (T,))
self.assertEqual(A1.__args__, ((T,),))
self.assertEqual(A1[int], Base[int])

A2 = Base[[T]]
self.assertEqual(A2.__parameters__, (T,))
self.assertEqual(A2.__args__, ((T,),))
self.assertEqual(A2[int], Base[int])

A3 = Base[[int, T]]
self.assertEqual(A3.__parameters__, (T,))
self.assertEqual(A3.__args__, ((int, T),))
self.assertEqual(A3[str], Base[[int, str]])

A4 = Base[[T, int, T2]]
self.assertEqual(A4.__parameters__, (T, T2))
self.assertEqual(A4.__args__, ((T, int, T2),))
self.assertEqual(A4[str, bool], Base[[str, int, bool]])

A5 = Base[[*Ts, int]]
self.assertEqual(A5.__parameters__, (Ts,))
self.assertEqual(A5.__args__, ((*Ts, int),))
self.assertEqual(A5[str, bool], Base[[str, bool, int]])

A5_2 = Base[[int, *Ts]]
self.assertEqual(A5_2.__parameters__, (Ts,))
self.assertEqual(A5_2.__args__, ((int, *Ts),))
self.assertEqual(A5_2[str, bool], Base[[int, str, bool]])

A6 = Base[[T, *Ts]]
self.assertEqual(A6.__parameters__, (T, Ts))
self.assertEqual(A6.__args__, ((T, *Ts),))
self.assertEqual(A6[int, str, bool], Base[[int, str, bool]])

A7 = Base[[T, T]]
self.assertEqual(A7.__parameters__, (T,))
self.assertEqual(A7.__args__, ((T, T),))
self.assertEqual(A7[int], Base[[int, int]])

A8 = Base[[T, list[T]]]
self.assertEqual(A8.__parameters__, (T,))
self.assertEqual(A8.__args__, ((T, list[T]),))
self.assertEqual(A8[int], Base[[int, list[int]]])

A9 = Base[[Tuple[*Ts], *Ts]]
self.assertEqual(A9.__parameters__, (Ts,))
self.assertEqual(A9.__args__, ((Tuple[*Ts], *Ts),))
self.assertEqual(A9[int, str], Base[Tuple[int, str], int, str])

A10 = Base[P2]
self.assertEqual(A10.__parameters__, (P2,))
self.assertEqual(A10.__args__, (P2,))
self.assertEqual(A10[[int, str]], Base[[int, str]])

def test_paramspec_in_nested_generics(self):
# Although ParamSpec should not be found in __parameters__ of most
# generics, they probably should be found when nested in
Expand Down
114 changes: 69 additions & 45 deletions Lib/typing.py
ADF8
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,16 @@ def _collect_parameters(args):
# We don't want __parameters__ descriptor of a bare Python class.
if isinstance(t, type):
continue

# `t` might be a tuple, when `ParamSpec` is substituted with
# `[T, int]`, or `[int, *Ts]`, etc.
if isinstance(t, tuple):
for x in t:
for collected in _collect_parameters([x]):
if collected not in parameters:
parameters.append(collected)
continue

if hasattr(t, '__typing_subst__'):
if t not in parameters:
parameters.append(t)
Expand Down Expand Up @@ -1444,54 +1454,68 @@ def _determine_new_args(self, args):

new_args = []
for old_arg in self.__args__:
self._make_substitution(old_arg, new_args, new_arg_by_param)
return tuple(new_args)

if isinstance(old_arg, type):
new_args.append(old_arg)
continue
def _make_substitution(self, old_arg, new_args, new_arg_by_param):
# Mutates `new_args` and inserts the correct new argument.
if isinstance(old_arg, type):
new_args.append(old_arg)
return

substfunc = getattr(old_arg, '__typing_subst__', None)
if substfunc:
new_arg = substfunc(new_arg_by_param[old_arg])
else:
subparams = getattr(old_arg, '__parameters__', ())
if not subparams:
new_arg = old_arg
else:
subargs = []
for x in subparams:
if isinstance(x, TypeVarTuple):
subargs.extend(new_arg_by_param[x])
else:
subargs.append(new_arg_by_param[x])
new_arg = old_arg[tuple(subargs)]

if self.__origin__ == collections.abc.Callable and isinstance(new_arg, tuple):
# Consider the following `Callable`.
# C = Callable[[int], str]
# Here, `C.__args__` should be (int, str) - NOT ([int], str).
# That means that if we had something like...
# P = ParamSpec('P')
# T = TypeVar('T')
# C = Callable[P, T]
# D = C[[int, str], float]
# ...we need to be careful; `new_args` should end up as
# `(int, str, float)` rather than `([int, str], float)`.
new_args.extend(new_arg)
elif _is_unpacked_typevartuple(old_arg):
# Consider the following `_GenericAlias`, `B`:
# class A(Generic[*Ts]): ...
# B = A[T, *Ts]
# If we then do:
# B[float, int, str]
# The `new_arg` corresponding to `T` will be `float`, and the
# `new_arg` corresponding to `*Ts` will be `(int, str)`. We
# should join all these types together in a flat list
# `(float, int, str)` - so again, we should `extend`.
new_args.extend(new_arg)
substfunc = getattr(old_arg, '__typing_subst__', None)
if substfunc:
new_arg = substfunc(new_arg_by_param[old_arg])
else:
subparams = getattr(old_arg, '__parameters__', ())
if not subparams:
new_arg = old_arg
else:
new_args.append(new_arg)

return tuple(new_args)
subargs = []
for x in subparams:
if isinstance(x, TypeVarTuple):
subargs.extend(new_arg_by_param[x])
else:
subargs.append(new_arg_by_param[x])
new_arg = old_arg[tuple(subargs)]

if self.__origin__ == collections.abc.Callable and isinstance(new_arg, tuple):
# Consider the following `Callable`.
# C = Callable[[int], str]
# Here, `C.__args__` should be (int, str) - NOT ([int], str).
# That means that if we had something like...
# P = ParamSpec('P')
# T = TypeVar('T')
# C = Callable[P, T]
# D = C[[int, str], float]
# ...we need to be careful; `new_args` should end up as
# `(int, str, float)` rather than `([int, str], float)`.
new_args.extend(new_arg)
elif _is_unpacked_typevartuple(old_arg):
# Consider the following `_GenericAlias`, `B`:
# class A(Generic[*Ts]): ...
# B = A[T, *Ts]
# If we then do:
# B[float, int, str]
# The `new_arg` corresponding to `T` will be `float`, and the
# `new_arg` corresponding to `*Ts` will be `(int, str)`. We
# should join all these types together in a flat list
# `(float, int, str)` - so again, we should `extend`.
new_args.extend(new_arg)
elif isinstance(old_arg, tuple):
# Corner case:
# P = ParamSpec('P')
# T = TypeVar('T')
# class Base(Generic[P]): ...
# Can be substituted like this:
# X = Base[[int, T]]
# In this case, `old_arg` will be a tuple:
sub_args = []
for x in old_arg:
self._make_substitution(x, sub_args, new_arg_by_param)
new_args.append(tuple(sub_args))
else:
new_args.append(new_arg)

def copy_with(self, args):
return self.__class__(self.__origin__, args, name=self._name, inst=self._inst,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix type substitution after ParamSpec substitution of the user generic with
a list with a TypeVar.
0