[Python-ideas] accurate errors for "magic" methods

Chris Rebert pyideas at rebertia.com
Thu Apr 9 20:33:46 CEST 2009

On Thu, Apr 9, 2009 at 10:56 AM, Georg Brandl <g.brandl at gmx.net> wrote:
> spir schrieb:
>> Actually, I'm wrong: it's perfectly clear as long as the programmer is able
>> to follow all the necessary reflexion path; then probably also able to solve
>> the problem without any help from python.
>> The issue here is that a very specific (and meaningful) case (dict-like
>> behaviour missing) is adressed using a very generic (and thus helpless)
>> message (attributeError).
>> I think error cases about "magic" methods, that implement conceptually
>> meaningful behaviours, should have appropriate messages. In the case above,
>> maybe something like: "Values instance is not an item container (no
>> __getitem__ method found)."
> The time machine strikes again:
>>>> class A(object): pass
> ...
>>>> A()['a']
> Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
> TypeError: 'A' object is unsubscriptable

And if PEP 3134 (http://www.python.org/dev/peps/pep-3134/) were
accepted+implemented, this could be made even clearer by using
exception chaining to indicate that the TypeError was caused (at least
semantically, disregarding optimizations) by the AttributeError.


I have a blog:

More information about the Python-ideas mailing list