[Python-3000] Changing function-related TypeErrors

Nick Coghlan ncoghlan at gmail.com
Mon May 1 10:34:48 CEST 2006


Collin Winter wrote:
> One of the things that's always puzzled me about Python 2.x is why any
> of the following raise a TypeError:
> 
> Given a function "def foo(a, b, c=None)",
> 
>> foo(4)
>> foo(4, 5, 6, 7)
>> foo(4, 5, b=6)
>> foo(4, 5, d=6)
> 
> all raise TypeErrors, as do several other classes of calling errors --
> none of which have anything to do with the type of an object. This
> usage of TypeError is confusing. Also, "except TypeError" statements
> may end up accidentally hiding this class of error, making one of
> these situations very difficult to debug.

The Type Error is actually referring to the type of 'foo' - the code is 
clearly expecting it to be something with a different __call__ signature.

However, for things like functions, there's nothing wrong with the type of the 
supplied object. For functions, the problem is actually a Value Error, in that 
the signature of that particular function *instance* is wrong.

I don't really have a preference here - the current error is a little obscure, 
but I'm not sure fixing it is worth the effort of adding a new exception type.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://www.boredomandlaziness.org


More information about the Python-3000 mailing list