[Python-3000] Type annotations: annotating generators

Paul Moore p.f.moore at gmail.com
Thu May 18 17:56:17 CEST 2006


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 ????

Two obvious options come to my mind: (1) don't allow it - the return
type is unspecified, after all type annotations are optional, and this
is going to get too hard, too fast; or (2) something like "function()
-> Number".

Now reasoning by analogy, you can apply the same logic to generators -
either (1) don't allow them to be specified, or (2) use something like
"generator() -> int" (and this option allows you to use the "argument"
of the generator as a type annotation for send.

My preference is for (1), but that's in the more general context that
I expect to ignore type annotations in 99% of cases for my own code...

Paul.


More information about the Python-3000 mailing list