[Grok-dev] Annotations and Interface Inheritance

Paul Wilson paul.wilson at icr3s.co.uk
Sat May 9 07:57:51 EDT 2009


Hi there,

I have a couple of interfaces sharing a common base that my annotation
adapters will implement:

>>> from zope.interface import Interface
>>> class IBaseAnnotation(Interface):
...     pass
...
>>> class IAnnotationA(IBaseAnnotation):
...     pass
...
>>> class IAnnotationB(IBaseAnnotation):
...     pass
...

Along with a content class:

>>> class Content(object):
...     pass
...

And finally some actual adapters:

>>> import grok
>>> class AnnotationA(grok.Annotation):
...     grok.implements(IAnnotationA)
...     grok.context(Content)
...
>>> class AnnotationB(grok.Annotation):
...     grok.implements(IAnnotationB)
...     grok.context(Content)
...

Imagine that the above has been grokked.

Let's get a Content object:

>>> content = Content()

And give it one annotation from the two adapters I've defined:

>>> meta_a = IAnnotationA(content)
>>> meta_a.foo = "MEGROK!!!"

Something strange now happens when I try to get all adapters to my base
annotation interface from the component architecture:

>>> from zope.component import getAdapters
>>> list(getAdapters((content,), IBaseAnnotation)
      [(u'', <....AnnotationB object at 0x8ff5f6c>)]

It's given me the wrong object back! If I rerun the above but _omit_ the
definition of ``AnnotationB`` then it gives me the correct result of
``AnnotationA``.

After some digging, I found that using:

>>> from zope.annotations.interfaces import IAnnotations
>>> list(getAdapters((content,), IAnnotations)

gives correct results in both cases - but for my particular example it also
gives a:
``zope.dublincore.annotatableadapter.ZDCAnnotationData`` too (for container
items only) which I don't want. I could filter this out of course without
much problem - but something doesn't smell right!!

Any hints/advice would be welcomed!

Regards,
Paul
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.zope.org/pipermail/grok-dev/attachments/20090509/9d2b907e/attachment.html 


More information about the Grok-dev mailing list