bpo-43224: Forbid TypeVar substitution with Unpack by serhiy-storchaka · Pull Request #32031 · python/cpython (original) (raw)

I'll rewrite it as B[int, *Ts2], so we could unambiguously refer to variables.

AFAIK PEP 646 does not cover such case. It does not specify the result.

But what can be the result of substitution B[int, *Ts2]? T is substituted with int, T2 should be substituted with the last item of *Ts2, and *T should be substituted with all but the last items of *Ts2. But we cannot express this, because Ts2 is a variable. If we substitute *Ts2 with an empty sequence of types (X[()]), there would not be a value for T2.

I think that it is better to make it an error:

def f(a: tuple[*Ts2]) -> B[int, *Ts2]: ...

You should write

def f(a: tuple[*Ts2, T3]) -> B[int, *Ts2, T3]: ...

to make it having some sense.

There is a workaround of this problem. It may even make the code more clear. You can see, that a should be a tuple containing at least 1 item. T2 in B will be substituted with the type of the last item, and *Ts in B will be substituted with the rest of types.