[Python-ideas] from __future__ import function_annotations

Tony Lownds tony at PageDNA.com
Wed Apr 11 17:24:19 CEST 2007


On Apr 11, 2007, at 4:01 AM, Tobias Ivarsson wrote:

> I am just curiously wondering about the plans for introducing  
> function annotations (PEP 3107). I could not find any information  
> about this in the PEP, neither when I searched the mail archives.
> The way I see it this feature could be quite interesting to  
> introduce as early as possible since I believe that there are quite  
> a few tools that could benefit from this today.
> I could for example see Jython using function annotations for  
> declaring methods that are supposed to be accessible from java  
> code. This is done via annotations in the doc string today, and  
> would be a lot clearer using function annotations.
> Jython could implement this use of function annotations without  
> python supporting it, but that would make the code incompatible  
> between python and Jython, which would be highly unfortunate.
> Therefore i propose that python adds support for function  
> annotations in version 2.6 via
> from __future__ import function_annotations
> This would make the change as compatible as for example @decorators  
> or the with-statement.
>

Function annotations PEP is accepted and code has been checked in to  
p3yk. I don't think there would be much support for
the syntax in 2.6, but I could be wrong. A more palatable  
compatibility strategy may be to introduce a decorator that sets
function.__annotations__, so that these two function definitions  
would have equivalent annotations:

 >>> @annotate(int, int, returns=int)
... def gcd1(m, n):
...   etc

 >>> def gcd2(m: int, n: int) -> int:
...   etc

It's easier for a decorator to be compatible with run-time semantics,  
and more likely to avoid syntax questions, than embedding
the annotattions in docstrings. Source code conversion (2to3) could  
change the @annotate decorator form to the in-line function
form. Tools could be written to use either annotation set.

What do y'all think?

-Tony




More information about the Python-ideas mailing list