Decorator for Enforcing Argument Types

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Sat Dec 23 08:38:39 EST 2006


George Sakkis a écrit :
> John Machin wrote:
> 
> 
>>Peter  Wang wrote:
>>
>>>Bruno Desthuilliers wrote:
>>>
>>>><my humble opinion>
>>>>Python is dynamic, and fighting against the language is IMHO a really
>>>>bad idea. The only places where theres a real need for this kind of
>>>>stuff are when dealing with the "outside world" (IOW : inputs and
>>>>outputs). And then packages like formencode can do much more than mere
>>>>type-checking
>>>></my humble opinion>
>>>
(snip)
> I have also a very recent real-world example to share, from the other
> side of the fence this time. It's even worse because it's an error that
> passes silently. Cut-down version follows:
> 
> @cherrypy.expose
> def retrieve(self, **kwds):
>     queries = kwds['q']
>     rows = self._selectRows(*queries)
>     # more stuff
> 
> 'q' here is a multiselect field that is binded to a list of selected
> strings. Or so I thought, until someone noticed bizarre results in some
> cases. Turns out that if you select a single item from the select box,
> 'q' is binded to a string instead of a list of length 1, so instead of
> retrieving 'apple', she got back the results for 'a', 'p', 'p',
> 'l','e'.

This is a typical case of converting/validating data from the "outside 
world" - something well covered by formencode.

> Bottom line, type checking is a tricky business.

Indeed. In fact, proper "type checking" would first require some 
agreement on what's a "type"...



More information about the Python-list mailing list