-
-
Notifications
You must be signed in to change notification settings - Fork 30k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve string's __getitem__ error message #88276
Comments
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 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:
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 |
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. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: