[Zope-dev] Python3 and attribute annotations.

Dan Korostelev nadako at gmail.com
Mon Mar 9 18:35:18 EDT 2009


2009/3/10 Martijn Pieters <mj at zopatista.com>:
> On Mon, Mar 9, 2009 at 22:20, Dan Korostelev <nadako at gmail.com> wrote:
>> As you may know, python 3 introduced the concept of annotations for
>> callable objects. That annotations store information about arguments
>> and return values, which is kinda nice language feature that will
>> allow us to do interesting things.
>>
>> But there's a problem: those annotations will be stored in object's
>> __annotations__ attribute, which is also used by zope.annotation's
>> AttributeAnnotation implementation, so they will conflict.
>
> I don't think they are, according to PEP 3107 they are stored in the
> func_annotations attribute of the function.

No, they are stored in __annotations__. Look here:
http://docs.python.org/3.0/whatsnew/3.0.html#new-syntax

Also:

nyo at seasaw:~$ python3
Python 3.0.1+ (r301:69556, Feb 24 2009, 13:51:44)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.

>>> def hello(who:'name') -> None:
...     print('Hello, {0}!'.format(who))
...
>>> hello.__annotations__
{'who': 'name', 'return': None}


> Note that even if the name *where* the same, attribute annotations
> only work on classes and instances, while function annotations only
> apply to functions, not?

Good point. Looks like it is added automatically only for functions/methods. :)

However, we can't be sure there won't be annotations for any callable
object, because even PEP says that ``we say function, we mean callable
object``, so one day we certainly will conflict with something. Also,
as Gary pointed out, we mis-use the __*__ name pattern that is now
intended for defining special names that have special meaning for
python interpreter. And we'll certainly will mis-use the
__annotations__ name as it's clearly defined in python 3k.

So, after Gary reminded about __*__ names, I think we shoud use
something like "_z_annotations".

-- 
WBR, Dan Korostelev


More information about the Zope-Dev mailing list