New submission from Miguel Brito <miguel.mdebr...@gmail.com>: 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 ---------- components: Interpreter Core messages: 393858 nosy: miguendes priority: normal severity: normal status: open title: Make IndexError messages for list more informative type: enhancement versions: Python 3.11 _______________________________________ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue44166> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com