Pre-PEP Considerations and Feedback: Type Transformations on Variadic Generics

Just throwing my usecase in here:

from typing import TypeVar, TypeVarTuple

T = TypeVar('T')
Shape = TypeVarTuple('Shape')

class Action[T]:
    def run(self) -> T:
        raise NotImplementedError()

class FloatAction(Action[float]):
    def run(self) -> float:
        return 3.2

class IntAction(Action[int]):
    def run(self) -> int:
        return 12

def do_actions(*actions: Action[*Shape]) -> tuple[*Shape]:
    return tuple(action.run() for action in actions)

results: tuple[float, int] = do_actions(FloatAction(), IntAction())

In other words, I want to take an arbitrary set of Action[T] instances, where each one is parameterised by a different T, and return a tuple parameterised by that same set of different Ts. So I want to unwrap/extract the set of T from tuple[Action[T1], Action[T1]] into Tuple[T1, T2].

1 Like