[Python-3000] Draft pre-PEP: function annotations
Collin Winter
collinw at gmail.com
Sat Aug 12 01:49:32 CEST 2006
I'll combine my replies to Josian and Talin:
On 8/11/06, Josiah Carlson <jcarlson at uci.edu> wrote:
> Let us say that I have two metadata interpters. One that believes that
> the metadata is types and wants to verify type on function call. The
> other believes that the metadata is documentation. Both were written
> without regards to the other. Please describe to me (in code preferably)
> how I would be able to use both of them without having a defined
> metadata interpretation chaining semantic.
On 8/11/06, Talin <talin at acm.org> wrote:
> Say I want to annotate a specific argument with two pieces of
> information, a type and a docstring. I have two metadata interpreters,
> one which uses the type information to restrict the kinds of arguments
> that can be passed in, and another which uses the docstring to enhance
> the generated documentation.
[snipped: the rise of a defacto annotation-sharing standard]
> What this means is that, despite the statements that annotations have no
> defined format or meaning, the fact is that they now do: The defacto A&B
> sharing convention. The sharing convention tells metadata interpreters
> how to distinguish between metadata that they can interpret, and how to
> skip over other metadata.
What Josiah is hinting at -- and what Talin describes more explicitly
-- is the problem of how exactly "chaining" annotation interpreters
will work.
The case I've thought out the most completely is that of using
decorators to analyse/utilise the annotations:
1) Each decorator should be written with the assumption that it is the
only decorator that will be applied to a given function (with respect
to annotations).
2) Chaining will be accomplished by maintaining this illusion for each
decorator. For example, if our annotation-sharing convention is that
annotations will be n-tuples (n == number of annotation-interpreting
decorators), where t[i] is the annotation the i-th decorator should
care about, the following chain() function will do the trick (a full
demo script is attached):
>>> def chain(*decorators):
>>> assert len(decorators) >= 2
>>>
>>> def decorate(function):
>>> sig = function.__signature__
>>> original = sig.annotations
>>>
>>> for i, dec in enumerate(decorators):
>>> fake = dict((p, original[p][i]) for p in original)
>>>
>>> function.__signature__.annotations = fake
>>> function = dec(function)
>>>
>>> function.__signature__.annotations = original
>>> return function
>>> return decorate
A similar function can be worked out for using dictionaries to specify
multiple annotations.
I'll update the PEP draft to include a section on guidelines for
writing such decorators.
Collin Winter
-------------- next part --------------
A non-text attachment was scrubbed...
Name: chaining_decorators.py
Type: text/x-python-script
Size: 1497 bytes
Desc: not available
Url : http://mail.python.org/pipermail/python-3000/attachments/20060811/065a0df9/attachment.bin
More information about the Python-3000
mailing list