[Python-3000] Function annotations considered obfuscatory (Re: Conventions for annotation consumers)

Josiah Carlson jcarlson at uci.edu
Wed Aug 16 19:03:05 CEST 2006

"Guido van Rossum" <guido at python.org> wrote:
> On 8/16/06, Jim Jewett <jimjjewett at gmail.com> wrote:
> > That is one reason I wonder whether all annotations/modifications have
> > to actually be part of the prologue, or whether they could be applied
> > to the Signature afterwards.
> And how would that reduce the clutter? The information still has to be
> entered by the user, presumably with the same disambiguating tags, and
> some punctuation.

I'd say that pulling out annotations from the function signature, which
was argued to be the most important piece of information about a
function during the decorator discussion, could do at least as much to
reduce clutter and increase readability and understandability, as
anything else discussed with regards to the PEP so far.

To pull back out that 9 line function...

> @docstring
> @typechecker
> @constrain_values
> def foo(a: {'doc': "Frobnication count",
>            'type': Number,
>            'constrain_values': range(3, 9)},
>        b: {'type': Number,
>             # This can be only 4, 8 or 12
>            'constrain_values': [4, 8, 12]}) -> {'type': Number}

First cleaning up the annotations to not use a dictionary:

def foo(a: [doc("frobination count"),
        b: [type(Number),
            # This can be only 4, 8 or 12
            constrain_values([4,8,12])]) -> type(Number):

Now lets pull those annotations out of the signature...

@__signature__([doc("frobination count"),
                # This can be only 4, 8 or 12
                constrain_values((4,8,12))], returns=type(Number))
def foo(a, b):

Ultimately the full function definition (including decorators) is just
as cluttered, but now we can see that we have a function that takes two
arguments, without having to scan for 'name:' .  If it is necessary for
somone to know what kinds of values, types, docs, etc., then they can
use the documentation-producing tool that will hopefully come with their
annotation consumer(s).

 - Josiah

Then there is the blasphemous:

@docstring(a="frobination count")
@typechecker(a=type(Number), b=type(Number))
@constrain values(a=range(3,9), b=(4,8,12), returns=type(Number))
def foo(a, b):

More information about the Python-3000 mailing list