[Zope3-dev] Riddle: zope.interface versus zope.formlib

Stephan Richter srichter at cosmos.phy.tufts.edu
Wed Aug 23 15:57:44 EDT 2006


Hi everyone,

today I found a nice brain teaser. What is the outcome of the following:

>>> from zope.interface import Interface
>>> from zope.schema import TextLine
>>> class IFoo(Interface):
...     title = TextLine()
...
>>> class IBar(IFoo):
...     pass
...
>>> IBar['title']
<zope.schema._bootstrapfields.TextLine object at 0xb7bc17ac>
>>> IBar['title'].interface
???????????????????

If you ask the zope.interface.interfaces.IAttribute documentation you get:

    interface = Attribute('interface',
                          'Stores the interface instance in which the '
                          'attribute is located.')

In plain English: <InterfaceClass __main__.IFoo>

If you ask zope.formlib (form.py, line 227)::

  # Adapt context, if necessary
  interface = field.interface
  ...         
  adapter = interface(context)

Here the answer would be: <InterfaceClass __main__.IBar>

Either way you look at it, one package is wrong and thus broken.

But which is it? It turns out that zope.interface wins and IFoo is returned. 
This really hurts zope.formlib. After an initial discussion on IRC, more 
people expected IBar, since it is more in line with im_class, but there were 
also people expecting IFoo.

Before playing with it, I would like to know what other people think. So, what 
do you think?

Regards,
Stephan
-- 
Stephan Richter
CBU Physics & Chemistry (B.S.) / Tufts Physics (Ph.D. student)
Web2k - Web Software Design, Development and Training


More information about the Zope3-dev mailing list