Hey James!

I think this is the functionality provided by the Map operator that we included in early drafts of PEP 646, but removed to keep the size of the PEP manageable. We intend to introduce this in a future PEP - a (very) early draft of which is available here.


On Wed, 11 May 2022 at 20:05, James Murphy via Typing-sig <typing-sig@python.org> wrote:

Hi All,

I'm new to this list so forgive me if this has been previously discussed.

Is there any work currently towards allowing mapping dependent types of variadic generic TypeVarTuples? For instance, consider this function:

def pair_with_ids(*args):
    return tuple((id(x), x) for x in args)

The single-arg version of this function I could type as:

T = TypeVar('T')

def pair_with_ids(x: T) -> tuple[tuple[int, T]]:
    return ((id(x), x),)

Something similar could be done for any fixed number of parameters, e.g.

T1 = TypeVar('T1')
T2 = TypeVar('T2')

def pair_with_ids(x1: T2, x2: T2) -> tuple[tuple[int, T1], tuple[int, T2]]:
    return ((id(x1), x1), (id(x2), x2))

I would like to be able to type the whole thing at once with something like:

Ts = TypeVarTuple('Ts')
def pair_with_ids(*args: *Ts) -> tuple[*tuple[int, Ts]]:
    return tuple((id(x), x) for x in args)

I realize this breaks the "TypeVarTuple is always starred" rule, but I'm wondering if any kind of dynamic type mapping like this has been considered before.

The idea here being that you always need a star applying to a TypeVarTuple but not necessarily _immediately_ before. The star could be applied to any generic that has a single free type variable filled by a TypeVarTuple.



Typing-sig mailing list -- typing-sig@python.org
To unsubscribe send an email to typing-sig-leave@python.org
Member address: mrahtz@google.com