[Python-3000] Changing function-related TypeErrors
Collin Winter
collinw at gmail.com
Mon May 1 05:22:12 CEST 2006
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.
To make it more obvious what the problem is, I propose that Python
3000 introduce an ArgumentError exception to handle these cases.
The one case that I'm not sure about is this:
"""
>>> def foo(a, (b, c)): pass
...
>>> foo(5, 6)
TraceBack (most recent call last)
File "<stdin>", line 1, in <module>
File "<stind>", line 1, in foo
TypeError: unpack non-sequence
"""
I can see arguments both for keeping this as a TypeError and for
changing it to an ArgumentError.
Since I doubt that anyone is (intentionally) trapping this particular
brand of TypeErrors, it might even be feasible to make the TypeError
-> ArgumentError changeover in Python 2.6.
I'm more than willing to work up a PEP and patches for this if there's interest.
Collin Winter
More information about the Python-3000
mailing list