bpo-43224: Draft implementation of PEP 646#30398
Closed
mrahtz wants to merge 18 commits intopython:mainfrom
Closed
bpo-43224: Draft implementation of PEP 646#30398mrahtz wants to merge 18 commits intopython:mainfrom
mrahtz wants to merge 18 commits intopython:mainfrom
Conversation
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PEP 646 (Variadic Generics) hasn't yet been completely approved by the steering council, but here's a draft of its implementation so we can get started on code review.Update 2021-01-19: the SC has approved the PEP, so full steam ahead!
TODO:
typing.get_argsandtyping.get_origin(rather than just checking__args__etc)A couple of notes below.
@pradeep90 @gvanrossum @Fidget-Spinner
Starred tuple types
I realised there's a tricky issue with starred tuple types.
If we have e.g.
tuple[int, *tuple[str, bool]], the obvious thing to do would be to unpack the innertupleat runtime, resulting intuple[int, str, bool]. This would imply thatiter(tuple[str, bool])creates an iterator returningstrthenbool.But this would cause problems when using starred tuple types as the type of
*args, e.g.def foo(*args: *tuple[str, bool]):foo.__annotations__['args']should definitely not betuple[str, bool]. That would instead implydef foo(*args: tuple[str, bool])*args, its iterator should return only a single value. (Or at least, this is the behaviour specified for*args: *Ts; @pradeep90, I've just realised, should we update that section of the PEP to make it explicit that that starred tuples can be also used for*args?)I think the simplest solution to both of these problems is to not unpack tuple types as runtime, having
*tuple[stuff]instead creating an iterator that returns only a single item, an instance of a new 'starred tuple'. (Note that such a type does have to exist anyway, in order to represent e.g.*tuple[int, ...].)For the native
tupletype, I've tentatively implemented this by adding a newstarredflag togaobjectingenericaliasobject.crather than creating a whole new type. This makes implementation easier, but maybe it's suboptimal considering thatgaobjectis used for lots of things other thantuple- feedback appreciated. Fortyping.Tuple, I've implemented a new class intyping.pycalledStarredTuple, which should behave in the same way. (This does mean that*tuple[int] != *Tuple[int], but given thattuple[int] != Tuple[int], this seems fine.)Implementation of
*tupleI'm pretty uncertain about my implementation in
genericaliasobject.c. In particular:typing.py, the implementation is as simple asdef __iter__(self): yield StarredTuple(self). Is there a way to do something similar inga_iter, without the need to also implementga_iternext?https://bugs.python.org/issue43224