This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: subscriptable
Type: enhancement Stage: resolved
Components: Interpreter Core Versions: Python 3.8, Python 3.7
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: r.david.murray, terry.reedy, thehesiod
Priority: normal Keywords:

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 "<stdin>", line 1, in <module>
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 "<stdin>", line 1, in <module>
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:56adminsetgithub: 76629
2018-01-05 19:44:48terry.reedysetstatus: open -> closed

nosy: + terry.reedy
messages: + msg309518

resolution: wont fix
stage: resolved
2017-12-30 21:30:33r.david.murraysetmessages: + msg309260
2017-12-30 16:46:24thehesiodsetmessages: + msg309240
2017-12-29 20:00:00r.david.murraysetnosy: + r.david.murray
messages: + msg309192
2017-12-29 18:59:48thehesiodcreate