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: Improve string's __getitem__ error message
Type: enhancement Stage: resolved
Components: Interpreter Core Versions: Python 3.11
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: miguendes, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2021-05-11 20:50 by miguendes, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 26042 merged miguendes, 2021-05-11 20:51
Messages (3)
msg393473 - (view) Author: Miguel Brito (miguendes) * Date: 2021-05-11 20:50
I noticed that __getitem__ message, although helpful, could be improved a bit further. This will also make it consistent with other error messages such as the ones raised by `str.count`, `str.split`, `str.endswith` and so many others.

Currently, the error message goes like this: "TypeError: string indices must be integers" but we could specify the type of the object passed as argument to __getitem__. So, for example:

```
>>> idx = '1'
>>> s = 'abcde'
>>> s[idx]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: string indices must be integers, not 'str'
```

This makes easier to debug and it is also consistent with other methods:

>>> "alala".count(8)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: must be str, not int

>>> "lala|helo".split(1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: must be str or None, not int

>>> 1 in "lala"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'in <string>' requires string as left operand, not int

>>> "lala|helo".split(object())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: must be str or None, not object
msg396575 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-06-27 12:05
New changeset ed1076428cca3c8dc7d075c16a575aa390e25fb8 by Miguel Brito in branch 'main':
bpo-44110: Improve string's __getitem__ error message (GH-26042)
https://github.com/python/cpython/commit/ed1076428cca3c8dc7d075c16a575aa390e25fb8
msg396577 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-06-27 12:14
Thank you for your contribution Miguel.

I decided to not backport this change to 3.10 because the benefit is too small in comparison with possibility to break someone's tests.
History
Date User Action Args
2022-04-11 14:59:45adminsetgithub: 88276
2021-06-27 12:14:59serhiy.storchakasetmessages: + msg396577
2021-06-27 12:12:22serhiy.storchakasetstatus: open -> closed
stage: patch review -> resolved
resolution: fixed
versions: - Python 3.10
2021-06-27 12:05:15serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg396575
2021-05-11 20:51:25miguendessetkeywords: + patch
stage: patch review
pull_requests: + pull_request24687
2021-05-11 20:50:25miguendescreate