Python variadic generics accepting class

169 Views Asked by At

a variable annotated with Type[C] may accept values that are classes themselves

How do you do this with TypeVarTuples (PEP 646)

from typing import TypeVar, TypeVarTuple

T = TypeVar("T")
Ts = TypeVarTuple("Ts")

def foo(a: T) -> T:
    ...
def bar(a: type[T]) -> T:
    ...
def baz(*a: *Ts) -> tuple[*Ts]:
    ...

a = foo(int) # type[int]
b = bar(int) # int
c = baz(int, str) # tuple[type[int], type[str]]

# What syntax?
def qux(*a: type[*Ts]) -> tuple[*Ts]: 
    ...
d = qux(int, str) # should be tuple[int, str]
0

There are 0 best solutions below