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.

Author miguendes
Recipients miguendes
Date 2021-05-18.07:33:38
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1621323219.05.0.0543571942932.issue44166@roundup.psfhosted.org>
In-reply-to
Content
I've noticed that in people will often ask for help on forums or stackoverflow to better understand the causes of
IndexErrors [1][2][3].
> The error message line for an IndexError doesn’t give you great information.[1]

Currently, when we access a out of bounds position we get something along the lines:
Traceback (most recent call last):

>>> a = [0, 10, 20, 30, 40]
>>> a[5]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range
>>> a.pop(6)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: pop index out of range
>>> a[6] = 7
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>> a = []
>>> a[2]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range


These error messages are too broad and doesn't inform the current length of the list, nor the index that was fed to it.

To improve the debugging experience in both interactive and non-interactive code,
I propose to offer a richer and more informative error messages. I think this can help users, especially newer and less
experienced folks, debug their code.


>>> a = [0, 10, 20, 30, 40]
>>> a[5]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range, the len is 5 so index must be in -5..4, got 5
>>> a[-6]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range, the len is 5 so index must be in -5..4, got -6
>>> a.pop(6)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: pop index out of range, the len is 5 so index must be in -5..4, got 6
>>> a[6] = 7
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range, the len is 5 so index must be in -5..4, got 6
>>> a = []
>>> a[2] = 0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range, got index 2 but the list is empty


These proposed messages are inspired by other languages, with the difference that in Python we can have negative index.
So informing the allowed ranges is a plus.

These improvements are not restricted to list, so it can be applied to strings and tuples as well, or any other indexable
object.

I have a branch ready, looking forward to hearing from you!

[1] https://stackoverflow.com/questions/1098643/indexerror-list-index-out-of-range-and-python
[2] https://stackoverflow.com/questions/16005707/index-error-list-index-out-of-range-python
[3] https://realpython.com/python-traceback/#indexerror
History
Date User Action Args
2021-05-18 07:33:39miguendessetrecipients: + miguendes
2021-05-18 07:33:39miguendessetmessageid: <1621323219.05.0.0543571942932.issue44166@roundup.psfhosted.org>
2021-05-18 07:33:39miguendeslinkissue44166 messages
2021-05-18 07:33:38miguendescreate