Issue 32448: subscriptable - Python tracker (original) (raw)

Created on 2017-12-29 18:59 by thehesiod, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (5)
msg309187 - (view) Author: Alexander Mohr (thehesiod) * Date: 2017-12-29 18:59
Currently subscriting a attribute that's None reports the following: >>> class Foo: pass >>> Foo.organizer = None >>> Foo.organizer['start'] Traceback (most recent call last): File "", line 1, in TypeError: 'NoneType' object is not subscriptable What would be nice is if it were to report the name of the attribute that is not subscriptable as it would greatly help in the logs, something like: Traceback (most recent call last): File "", line 1, in TypeError: 'NoneType' object of attribute 'organizer' is not subscriptable just a thought. Otherwise one would need to sprinkle their code with asserts, especially if it's a compound statement like: Foo.organizer.blah[0], you wouldn't know which attribute wasn't None
msg309192 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2017-12-29 20:00
At the point at which that error is raised, Python doesn't know the name of the attribute, nor is attribute access the only place where that particular error report is triggered (it's in a generic subscript-this-object call). So I doubt doing this would be practical. As for Foo.organizer.blah[0], it has to be blah that is None, since '.organizer' is an attribute access and not a subscript operation. The more difficult case is something like getattr(someobject, somevar)[0], where you can't tell what somevar contains just from the traceback. Which is why some value-added systems also dump the locals from the frame in the traceback.
msg309240 - (view) Author: Alexander Mohr (thehesiod) * Date: 2017-12-30 16:46
oh for second example I meant something like this: >>> class Foo: pass >>> Foo.organizer = None >>> Foo.blah = Foo >>> Foo.blah.organizer = None >>> Foo.blah.organizer[0] ya this is just a pie in the sky request :)
msg309260 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2017-12-30 21:30
Well, in that case having 'organizer' in the error message wouldn't help you untangle your code ;)
msg309518 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2018-01-05 19:44
I agree that a better message would be nice. But compiling 'a.b[c]' breaks the expression apart into the equivalent of 't=a.b; t[c]', where 't' is an anonymous reference, so as David notes, there is no 'name' left to display. We don't have a 'not possible resoluton, so "won't fix (because not possible)" seems closest.
History
Date User Action Args
2022-04-11 14:58:56 admin set github: 76629
2018-01-05 19:44:48 terry.reedy set status: open -> closednosy: + terry.reedymessages: + resolution: wont fixstage: resolved
2017-12-30 21:30:33 r.david.murray set messages: +
2017-12-30 16:46:24 thehesiod set messages: +
2017-12-29 20:00:00 r.david.murray set nosy: + r.david.murraymessages: +
2017-12-29 18:59:48 thehesiod create