[Python-3000] Type annotations: annotating generators

Collin Winter collinw at gmail.com
Thu May 18 18:05:45 CEST 2006


On 5/18/06, Paul Moore <p.f.moore at gmail.com> wrote:
> On 5/18/06, Collin Winter <collinw at gmail.com> wrote:
> > In looking through all of Guido's blog posts on the subject -- and all
> > the comments on them -- I haven't seen anyone consider the case of
> > generators. Assuming that "->" makes assertions only about the
> > function's return type, if I were to write a generator with the
> > following type,
> >
> > """def my_range(min: Number, max: Number) -> Number"""
> >
> > it would blow up because the function returns a generator, not a Number.
>
> I'd suggest you consider this in analogy with functions returning
> functions. Example:
>
>     def foo(n : Number) -> ????:
>         def bar():
>             return 12
>         return bar
>
> What is ????

You seem to have missed the threads [1], [2] where this exact issue
was discussed. The current thinking is something like Function(int,
int).returns(int).

So, reasoning by analogy would point to something like
Generator(is_sent=int, yields=int). In my mind, though, since
generators -- as opposed to function-returning-functions -- are a
sufficiently core part of the language worth their own special syntax
(e.g., the yield keyword), there might be interest in continuing this
special status in the type annotation syntax.

Thanks,
Collin Winter

[1] - http://mail.python.org/pipermail/python-3000/2006-May/001972.html
[2] - http://mail.python.org/pipermail/python-3000/2006-May/002061.html


More information about the Python-3000 mailing list