[Python-3000] Type annotations: annotating generators

Collin Winter collinw at gmail.com
Fri May 19 21:17:03 CEST 2006


On 5/19/06, Jim Jewett <jimjjewett at gmail.com> wrote:
> On 5/19/06, Guido van Rossum <guido at python.org> wrote:
> > The rest is just an exercise in attempting to come up with a pragmatic
> > set of operators and conventions that some people might be using for
> > talking about types. This is the idea of parameterizing types a la
> > dict[str, int|str] (which I should point out is valid *syntax* today
> > and can be made to execute correctly by modest additions to the type
> > metaclass) and solving various other issues pragmatically, e.g.
> > lambda:A for forward references and Funct(int, list[int]).returns(int)
> > to describe signatures.
>
> Today, int|str raises a TypeError.

He said it's valid *syntax*, not that it's valid semantics.

> If I didn't already know what you wanted, I would sort of expect it to
> be the same as (int or str) which isn't helpful.
>
> It sounds like what you want is that type.__or__ would magically
> include the equivalent of not fully reducing the expression.  For
> example, it might return a new instance of class TypeChecker with a
> __call__ method that checks isinstance against each argument, unless
> one argument is itself another TypeChecker instance, in which case it
> would call that checker instead of doing an isinstance ...

That's pretty much the idea, yes.

> I think trying to put compound types directly into the signature may
> require a little too much magic, compared to either:
>
> (1)  Just use a tuple, and put the smarts in your decorator if you
> need to actually do something with the information.
>
>     @decorator_which_handles_tuples
>     def f(a: (int, str)):

How do you differentiate between this and wanting to assert that 'a'
is a 2-tuple with a first element of type int and a second element of
type str?

> (2)  Defining the complex predicate before using it
>
>     def int_or_str(arg):
>         return isinstance(arg, int) or isinstance(arg, str)
>
>     def f(a:int_or_str)

My own description of that would run toward "hideously complex predicate".

Collin Winter


More information about the Python-3000 mailing list