[New-bugs-announce] [issue5760] __getitem__ error message hard to understand

Chris Rebert report at bugs.python.org
Wed Apr 15 08:32:11 CEST 2009

New submission from Chris Rebert <pybugs at rebertia.com>:

Prompted by

The current error message issued when trying to use the get item ([])
operator on an object that does not define __getitem__ can be hard to

>>> class A(object): pass
>>> A()['a']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'A' object is unsubscriptable

Problems observed:
- "unsubscriptable" is easily misread in haste as "unscriptable", which
can be quite confusing, especially to newbies
- "subscripting" is not frequently used to describe the [] operator,
making the message more difficult to decipher (again, esp. for newbies)
- the underlying lack of a __getitem__ method is not mentioned, thus not
making it obvious how to remedy the error

Use exception chaining and rephrase the error message to get something like:

AttributeError: class 'A' has no attribute '__getitem__'
The above exception was the direct cause of the following exception:
TypeError: 'A' object does not support the 'get item' operator

Similar changes should be made to __setitem__ & __delitem__.

components: Interpreter Core
messages: 85983
nosy: cvrebert
severity: normal
status: open
title: __getitem__ error message hard to understand
type: behavior
versions: Python 2.7, Python 3.0, Python 3.1

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list