[Python-3000] Type annotations: annotating generators

Collin Winter collinw at gmail.com
Fri May 19 17:51:41 CEST 2006


On 5/19/06, Guido van Rossum <guido at python.org> wrote:
> On 5/18/06, Kay Schluehr <kay.schluehr at gmx.net> wrote:
> > I have a question to the audience. How do you represent higher order
> > functions using this syntax?
>
> I think Collin and I both (independently) proposed the pragmatic
> Function(<type>, <type>, ..., returns=<type>) for this.

Based on a suggestion from Nick Coghlan [1], I'm now favoring
something like this:

Function(<type>, <type>, required_keyword=<type>, ...).returns(<type>)

The idea would be that you can use keyword arguments to Function to
assert that the passed-in function object supports being called with
certain keyword arguments.
That is, if you were planning to call a passed-in function like so:

func(5, 6, abc=7)

you'd want to assert that func actually has a parameter named 'abc'.
The corresponding Function would be Function(Number, Number,
abc=Number).

Using a 'returns' keyword argument to Function would effectively
prohibit you from making sure that a function has a parameter named
'returns'. Hence, the returns() method.

As I mentioned [2], you can play around with this model using the
trunk/ version of typecheck [3] (with docs for Function() [4]). This
is by no means set in stone, so comment away. The main point I'm not
entirely happy with: the use of star() and double_star() methods to
indicate types for *vargs and **kwargs, respectively.

If you like this general approach to handling Function(), I'll sketch
out a version of Generator along the same lines.

Collin Winter

[1] - http://mail.python.org/pipermail/python-3000/2006-May/002033.html
[2] - http://mail.python.org/pipermail/python-3000/2006-May/002061.html
[3] - http://oakwinter.com/code/typecheck/dist/typecheck-svn-latest.tar.gz
[4]  - http://oakwinter.com/code/typecheck/dev/utility/function.html


More information about the Python-3000 mailing list