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: Make TypeError message less ambiguous
Type: enhancement Stage: patch review
Components: Versions: Python 3.10
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: coyot, rhettinger, terry.reedy, thatiparthy, xtreak
Priority: normal Keywords: patch

Created on 2018-10-26 15:52 by coyot, last changed 2022-04-11 14:59 by admin.

Pull Requests
URL Status Linked Edit
PR 10132 closed thatiparthy, 2018-10-26 16:12
Messages (11)
msg328565 - (view) Author: coyot linden (coyot) Date: 2018-10-26 15:52
The TypeError message:

TypeError: string indices must be integers

is ambiguously written. While some may understand the intent, others will read it as confusingly saying that strings must be integers since dicts among other things do have string indices.  Suggest changing the message to:

TypeError: indices of strings must be integers
msg328567 - (view) Author: Srinivas Reddy Thatiparthy(శ్రీనివాస్ రెడ్డి తాటిపర్తి) (thatiparthy) * Date: 2018-10-26 16:08
+1. 

In my early days of python programming i was confused of this message.
msg328580 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2018-10-26 17:34
Thanks for the report. Error message changes are generally done on master and not back ported. I am removing 3.5 which is security fixes only mode and adding 3.8 . There are other errors of similar format but indicate the wrong type in the error message like list indices.

$ ./python.exe
Python 3.8.0a0 (heads/bpo31177-dirty:19986202a8, Oct 26 2018, 22:19:23)
[Clang 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> [1]['1']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: list indices must be integers or slices, not str

I think we should also assert the error message in test_slice (Lib/test/string_tests.py) if there is a change.

Thanks Serhiy, I was about to add Raymond too who might have thoughts on the wording :)
msg328587 - (view) Author: coyot linden (coyot) Date: 2018-10-26 18:36
You're welcome. I filed against 3.5 because that's what I have in our env at the moment and I couldn't test and thus assert the issue in later versions. 

Happy to see the problem addressed more generally.

The abstraction of the problem is the ambiguity in English between nouns used as adjectives and their groupings: (noun phrase) versus (noun as adjective)(noun). Using "X of Y" rather than (XY) or (X)(Y) resolves the ambiguity.
msg328596 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2018-10-26 19:20
I have a mixed opinion of this.  On the one hand, 'string index', taken out of context, *is* ambiguous.  (In the context of the TypeError message, it took me a minute to see the string-index interpretation.)  On the other hand, dicts do not have indices.  They have keys.  So only the 'index of string' interpretation makes sense.

Is there anything in the docs that suggests that a mapping subscript can be called an 'index' in addition to 'key'?

It would be nicer if this TypeError message also included 'not x', but there may be an issue that the type x info is lost.
msg328597 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2018-10-26 19:45
I checked a bit.  All the 'index' entries in the index are about sequences.  However, https://docs.python.org/3/reference/datamodel.html has this abstract (non-Python) description:

"Mappings
    These represent finite sets of objects indexed by arbitrary index sets. The subscript notation a[k] selects the item indexed by k from the mapping a; ...
    Dictionaries
        These represent finite sets of objects indexed by nearly arbitrary values."

I personally would prefer 'keyed' or 'subscripted' and 'key'to prevent confusion.  The entry then switches to the usual Python term 'key'.

"The only types of values not acceptable as keys  ... a key’s hash value ... used for keys ..."

https://docs.python.org/3/library/stdtypes.html#mapping-types-dict uses 'index' once as a verb, otherwise uses 'key'.
msg328598 - (view) Author: coyot linden (coyot) Date: 2018-10-26 20:09
The problem I have with "So only the 'index of string' interpretation makes sense." is that by the time a developer has that understanding, they won't get this error. So it's fine and logical to say from the behind-the-scenes view that only sequences talk about indices, but that's not helpful to the confused developer who gets this message. The point of error messages should be to clearly indicate the problem and solution, not rely on the developer having a global understanding of error messages.

So, rather than defend the current practice, I ask if there are arguments against "X of Y". Is there some context in which that is confusing?
msg328604 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2018-10-26 20:37
Aside from the effort of making a change correct and consistent across our code, error message changes often breaks user code and tests that depend on the current wording.  So there must be sufficient benefit to overcome the cost.  (The user breakage cost is why we nearly always wait for the next version to make such changes.)
msg328834 - (view) Author: coyot linden (coyot) Date: 2018-10-29 14:38
Sure, this can wait to the next release. I don't see it as groundshatteringly ;) urgent.
msg387630 - (view) Author: coyot linden (coyot) Date: 2021-02-24 17:02
3.8 has come and gone, resubmitting this for 3.10
msg402579 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2021-09-24 17:31
https://bugs.python.org/issue44110 did some improvements to error message to include type.
History
Date User Action Args
2022-04-11 14:59:07adminsetgithub: 79258
2021-09-24 17:31:16xtreaksetmessages: + msg402579
2021-02-24 17:02:37coyotsetmessages: + msg387630
versions: + Python 3.10, - Python 3.8
2018-10-29 14:38:22coyotsetmessages: + msg328834
2018-10-26 20:37:34terry.reedysetmessages: + msg328604
2018-10-26 20:09:53coyotsetmessages: + msg328598
2018-10-26 19:45:35terry.reedysetmessages: + msg328597
2018-10-26 19:20:41terry.reedysetnosy: + terry.reedy
messages: + msg328596
2018-10-26 18:36:56coyotsetmessages: + msg328587
2018-10-26 17:34:56xtreaksetnosy: + xtreak

messages: + msg328580
versions: + Python 3.8, - Python 3.5
2018-10-26 17:00:12serhiy.storchakasetnosy: + rhettinger
2018-10-26 16:12:24thatiparthysetkeywords: + patch
stage: patch review
pull_requests: + pull_request9464
2018-10-26 16:08:11thatiparthysetnosy: + thatiparthy
messages: + msg328567
2018-10-26 15:52:43coyotcreate