[Zope-dev] z3c.formwidget.query - incorrect interface implementation?

Marius Gedminas marius at gedmin.as
Sun Aug 24 22:18:36 EDT 2008


On Sun, Aug 24, 2008 at 07:30:24PM +0100, Martin Aspeli wrote:
> I'm trying to build a widget that allows for auto-complete of items in a 
> vocabulary, but also allows additional (string) values to be added. To 
> understand how that works, I am digging into z3c.formwidget.query, and 
> it looks to me like it's making incorrect assumptions about its own 
> interfaces.
<snip>
> class IBaseVocabulary(ISource):
> 
>      def getTerm(value):
>          """Return the ITerm object for the term 'value'.
> 
>          If 'value' is not a valid term, this method raises LookupError.
>          """
<snip>
> class IVocabularyTokenized(IVocabulary):
> 
>      def getTermByToken(token):
>          """Return an ITokenizedTerm for the passed-in token.
> 
>          If `token` is not represented in the vocabulary, `LookupError`
>          is raised.
>          """
<snip>
> However, more importantly, in the README.txt of z3c.formwidget.query we 
> have this:
> 
>    ...     def getTermByValue(self, value):
>    ...         return self.vocabulary.by_value[value]
> 
> and similarly, in widget.py:
> 
>          map(terms.add,
>              map(source.getTermByValue,
>                  filter(lambda value: value and value not in values, 
> selection)))
> 
> If I'm not reading this wrong, it seems to me that z3c.formwidget.query 
> is using getTermByValue() (which I can't find anywhere else) instead of 
> getTermByToken() as defined by IVocabularyTokenized.

That does look like a bug.  However, without looking very deeply at the
code,the fix would be to use getTerm, not getTermByToken.

I wouldn't mind seeing getTerm renamed to getTermByValue, but that's
probably difficult to do without breaking backwards-compatibility.

Marius Gedminas
-- 
I used to think I was indecisive, but now I'm not so sure.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://mail.zope.org/pipermail/zope-dev/attachments/20080824/d99d26a8/attachment.bin 


More information about the Zope-Dev mailing list