[Python-3000] Mixing annotations and non-annotations
Collin Winter
collinw at gmail.com
Sun May 21 20:33:34 CEST 2006
On 5/21/06, Marcin 'Qrczak' Kowalczyk <qrczak at knm.org.pl> wrote:
> Nick Coghlan <ncoghlan at gmail.com> writes:
>
> > 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 is ambiguous: tuple[T1, T2] is the same as tuple[(T1, T2)],
> i.e. it's a 1-tuple containing a 2-tuple.
tuple[T1, T2] a 2-tuple with element 1 of type T1 and element 2 of
type T2. A 1-tuple containing a 2-tuple would be tuple[tuple[T1, T2]].
However, we still need to decide what happens in the case of
tuple[(T1, T2)]. Is the (T1, T2) coerced to tuple[T1, T2]?
My vote is "yes", but I don't have a full idea of how this coercion
would work. One solution would be to let the metaclass (the one that
catches the __getitem__ call on, e.g., tuple) automatically convert
non-annotations (using a to-be-decided test for annotation-ness) to
annotations. Something like this on the metaclass might do the trick
(based on Guido's metaclass sketch):
def __getitem__(self, arg):
newcls = self.__class__(self.__name__, (self,), {})
newcls.T = arg if is_annotation(arg) else type(arg)[arg]
return newcls
Collin Winter
More information about the Python-3000
mailing list