[Python-Dev] PEP-646 question: unpacking into single Generic parameter

133 views
Skip to first unread message

wi...@schinmeyer.de

unread,
Sep 24, 2021, 9:58:58 AM9/24/21
to pytho...@python.org
Hello,

first of all, thanks for working on variadic generics! I look forward to them.

My question: all the examples in https://www.python.org/dev/peps/pep-0646/ unpack into variadic arguments. But can I write code like this?

```
Ts = TypeVarTuple("Ts")

def enumerate_args(f: Callable[[*Tuple[int, Ts]]], *args: *Ts):
f(*enumerate(args))
```

In particular I'm talking about the `*Tuple[int, Ts]` syntax. All the examples from the PEP use `*Ts` so I don't know if this is legal, but I hope so.

This should probably be clarified in the PEP.

--
Best regards,

Willi Schinmeyer
_______________________________________________
Python-Dev mailing list -- pytho...@python.org
To unsubscribe send an email to python-d...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/pytho...@python.org/message/K76H6C5JWGJ7TW6DGOCZGOTVONWOFCWD/
Code of Conduct: http://python.org/psf/codeofconduct/

Mehdi2277

unread,
Sep 24, 2021, 11:53:18 PM9/24/21
to pytho...@python.org
I don't believe this is supported in pep 646 today. It was originally going to be supported with a Map operator. With a Map operator it would let you do something like,

T = TypeVar('T')
Ts = TypeVarTuple("Ts")
IndexedTuple = Tuple[Int, T]

def enumerage_args(*args: *Ts) -> *Map[IndexedTuple, Ts]
return enumerate(args)

Map operator takes two arguments, a generic type and type var tuple. This document describes map operator in more detail and an older version of the pep had it.

https://docs.google.com/document/d/1szTVcFyLznoDT7phtT-6Fpvp27XaBw9DmbTLHrB6BE4/edit

The reason it is missing is original version of 646 was becoming too complex for a single pep. My understanding is your example/similar examples are intended for the future to be supported, but that Map operator and other extensions to variadic generics will be future peps.
_______________________________________________
Python-Dev mailing list -- pytho...@python.org
To unsubscribe send an email to python-d...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/pytho...@python.org/message/YBJXIAGODKH4NB5OD4YBZUZPA7NV6T73/

Matthew Rahtz via Python-Dev

unread,
Oct 14, 2021, 1:40:19 PM10/14/21
to pytho...@python.org
Oops, sorry for the slow reply - I'm not subscribed to this mailing list.

As Mehdi2277 says, this would indeed require the Map operator we'll introduce in a future PEP.

But that's a good point about the `*Tuple[int, Ts]` syntax. I think the interpretation of it that would be most consistent with the rest of PEP 646 would actually be something slightly different than you had in mind - instead of saying that the Callable took args like

Tuple[int, Ts[0]], Tuple[int, Ts[1]], ...

it would say that the Callable took a variable number of arguments, the first of which was `int`, and the rest of which are bound to `Ts`. And this is indeed something we haven't talked about explicitly how to specify yet.

Most of the discussion of PEP 646 has taken place on typing-sig, so I'll start a new thread there to discuss this.
_______________________________________________
Python-Dev mailing list -- pytho...@python.org
To unsubscribe send an email to python-d...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/pytho...@python.org/message/PZYE2XNNOF3WCU4DXSLYFKOXDZW6WNJA/
Reply all
Reply to author
Forward
0 new messages