[Python-3000] Type annotations: annotating generators

Nick Coghlan ncoghlan at gmail.com
Sun May 21 09:00:28 CEST 2006


Greg Ewing wrote:
> Nick Coghlan 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]
>>
>> but how do I use this system to spell the type annotation for a tuple 
>> of unknown length containing only T instances?
> 
>   tuple[T, ...]

That looks more like "the first item is a T, the rest can be anything" to me, 
though. Slice notation might work:
     tuple[T:]

(Although Guido was cool towards a similar idea to use slice notation for 
mapping type descriptions)

> There wouldn't be any () equivalent, unless ... were made
> a generally-available expression, in which case you could use
> 
>   (T, ...)

I guess what I'm really asking is whether or not we should go to any 
significant effort to preserve two ways of saying the same thing. Since we 
plan to be able use tuples of type descriptions to describe heterogeneous 
tuples, why not use the tuple[T] notation to describe homogeneous tuples?

Then we can allow *args to be annotated normally, as sometimes it is used to 
accept a homogeneous tuple of arbitrary length, and other times is used to 
accept positional-only arguments to a function that accepts arbitrary 
keyword-only arguments (e.g. implementing methods in Python with signatures 
matching those of the dict constructor and update method).

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://www.boredomandlaziness.org


More information about the Python-3000 mailing list