[Python-3000] Type annotations: annotating generators

Collin Winter collinw at gmail.com
Sun May 21 20:23:51 CEST 2006


On 5/20/06, Nick Coghlan <ncoghlan at gmail.com> wrote:
> Assume the following is true for a type annotation system:
>    (T,) is equivalent to tuple[T]
>    (T1, T2) is equivalent to tuple[T1, T2]
>    (T1, T2, T3) is equivalent to tuple[T1, T2, T3]
>
> This fits in with how tuples are typically intended to be used (small
> collections of heterogeneous data), but how do I use this system to spell the
> type annotation for a tuple of unknown length containing only T instances?
>
> I'd say "that's what lists are for", except that the CPython core uses tuples
> as homogeneous containers of unknown length in several places. For example,
> the sequence used to hold the additional arguments to a function is a tuple.
> So I would expect the type annotation in "*args : T" to be implicitly
> translated to "tuple[T]". Similarly, I would expect the type description for
> both __mro__ and __bases__ to be "tuple[type]".

This is something I've been meaning to bring up: if we're serious
about the "tuples = fixed-length collections, lists = unknown-length
collections" distinction, then the CPython core should switch over to
this mindset as well. The fact that Python 2.x uses a tuple for
*varargs has caused me to jump through a number of hoops in my own
typecheck package.

If there's interest, I can put together a separate PEP on this issue
that lists all the places where tuples would need to be changed to
lists.

Collin Winter


More information about the Python-3000 mailing list