[Python-ideas] Specificity in AttributeError

Ned Batchelder ned at nedbatchelder.com
Sat Apr 27 13:53:54 CEST 2013


On 4/27/2013 3:39 AM, Ethan Furman wrote:
> On 04/26/2013 11:59 PM, Devin Jeanpierre wrote:
>> On Sat, Apr 27, 2013 at 1:49 AM, Ethan Furman <ethan at stoneleaf.us> 
>> wrote:
>>
>> Discovering that there is a bug is one thing, discovering why is
>> another. The problems that result from silently doing the wrong thing
>> can be significantly harder to diagnose than an exception traceback
>> is, and this hasattr_lite would let me get an exception in cases where
>> I might otherwise have silently wrong behavior.
>>
>> I mean, yes, this error was in fact found in my unit test suite. I
>> spent a lot of time tracking it down (perhaps too much time, because I
>> was expecting something else [oops, too many changes in one changeset
>> :X]), and eventually narrowed it down to a try/except
>> AttributeError I had. Even then, trying to filter out the legitimate
>> AttributeErrors and the illegitimate one from the same test case was
>> annoying. I ended up breaking at the except block and individually
>> examining the exceptions, both legitimate and not. This solved
>> everything.
>>
>> hasattr_lite (if it worked) would've reported the problem as an
>> AttributeError, with exactly the typo I had made in an attribute name
>> inside a property. Seconds to figure out and fix.
>>
>> So what I mean is, it isn't necessary, but I would find it helpful and
>> convenient.
>
> I absolutely agree, and would like to see the available info on other 
> exceptions (KeyError, IndexError, etc.) as well. I suspect it would 
> take some serious effort to upgrade all the exceptions from all the 
> places they can be raised from, though.

I also agree that more information can only be a good thing.  Unless 
someone can show why it could be harmful (cycles caused by the exception 
keeping a reference to the offending object??), the only downside I can 
see is the work needed to change the throw points.

--Ned.

>
> -- 
> ~Ethan~
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>




More information about the Python-ideas mailing list