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

spir denis.spir at free.fr
Fri Apr 10 08:52:38 CEST 2009

Le Fri, 10 Apr 2009 14:45:12 +1000,
Steven D'Aprano <steve at pearwood.info> s'exprima ainsi:

> On Fri, 10 Apr 2009 03:56:16 am Georg Brandl 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
> >
> >
> > (the difference being that A is new-style, while Values is
> > old-style.)
> Except that the error "object is unsubscriptable" might as well be in 
> Klingon to most people, particularly newbies. 
> (1) It's easy to misread it as "unscriptable", which is even more 
> mysterious.
> (2) As far as I know, there's no tradition of describing key or index 
> lookup as "subscripting" in Python. I've never noticed it in doc 
> strings or the online docs, and after hanging around comp.lang.python 
> extensively for years, I feel safe to say it's not a common term among 
> even experienced Python developers. I suppose that there's a weak 
> connection between index lookup and subscripts in mathematics.
> (3) The classic error message tells the newbie exactly what the error 
> is: the object has no __getitem__ method. The new error message tells 
> the newbie nothing useful. Given that obj is unsubscriptable, what 
> needs to be done to make it subscriptable?
I do agree with all of these comments. A useful error message in such a case should
(1) Speak first at the conceptual level (if it's not a container of individual elements, it can't be indexed nore "key-ed").
(2) Do this using an idiom that a newbie has a good chance to figure out -- even if with some mental effort.
(3) Point at the precise issue from which python itself could figure out there is an error (no __getitem__)

In this specific case, an additional difficulty comes from the common __getitem__ for both index and key lookup (and even slicing).

I have no real clue how to makes things better generally. I do not mean that somathing like 
   "Values instance is not an item container (no __getitem__ method found)."
if for the best.
But I'm sure there is an issue. ABCs bring a common and consistent organisation. We could start from there, establish a standard vocabulary (including eg "subscript" as a superclass of "key" & "index") that would be reused in all docs and tutorials; and use this system for error messages instead of random (& helpless) formulation.

la vita e estrany

More information about the Python-ideas mailing list