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: tuple.index error message improvement
Type: Stage: resolved
Components: Interpreter Core Versions: Python 3.7
process
Status: closed Resolution: duplicate
Dependencies: Superseder: Non-informative error message in index() and remove() functions
View: 13349
Assigned To: Nosy List: brett.cannon, llllllllll, martin.panter, rhettinger, schen
Priority: normal Keywords:

Created on 2017-05-25 22:41 by llllllllll, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 1820 open llllllllll, 2017-05-25 22:41
Messages (7)
msg294516 - (view) Author: Joe Jevnik (llllllllll) * Date: 2017-05-25 22:41
The old error of tuple.index(x): x not in tuple seemed very confusing to me. It was also harder to quickly understand what the program was doing wrong. This saves people a second pass through the program under the debugger because they can just see what the invalid value was.

The reason I am explicitly calling repr instead of using %R is that I didn't want to call repr twice if I didn't need to. If people think that is fine the format can just be tuple.index(%R): %R not in tuple instead.
msg294517 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2017-05-25 22:48
I'm wondering if including the whole "tuple.index(%R)" part is still necessary? The traceback will tell you what method you called, so wouldn't "%R not in tuple" be enough?
msg294518 - (view) Author: Joe Jevnik (llllllllll) * Date: 2017-05-25 22:52
I agree, "%R in tuple" is enough information for me. This would also remove the need to manually repr the object.
msg294519 - (view) Author: Joe Jevnik (llllllllll) * Date: 2017-05-25 22:55
As a more meta question: I have noticed that many error messages in the stdlib use PyErr_SetString, or choose to use the type name instead of the repr of the object. Is there a reason for this? I normally try to fill the error message with as much context as possible to make debugging easier. Is it a performance worry?
msg294522 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2017-05-26 02:22
> Is it a performance worry?

It really depends on what people are doing with their down.  If indexing potentially missing values is common, there will be a performance impact.  

Also, the cost of a __repr__ varies wildly depending on the data (i.e. a collections.Counter instance has an expensive __repr__, decimal objects have computations to runs as well).

From a user point of view, a repr might be helpful or it might be disasterously lengthy (a webpage, a giant bytearray, a long list of tuples).

Personally, I would rather not do this PR which seems to have the view that the exception is an error condition as opposed to being a normal way to terminate a series of index() calls.
msg294537 - (view) Author: (schen) Date: 2017-05-26 08:32
What is the rationale for the inconsistency between tuples and lists here?

().index(0)
ValueError: tuple.index(x): x not in tuple

[].index(0)
ValueError: 0 is not in list

In this simple artificial case, it seems clear to me that the list version (which is similar to the proposed change) is superior.

However, it is often not sufficient to use such cases as the basis for making a decision. Raymond raises some very good points.

In particular, I noticed that printing the repr quickly becomes unwieldy for more complex objects. For example, I tried the following:
[].index(''.join(chr(randrange(97, 97+26)) for _ in range(1000)))
[].index(Counter(randrange(10) for _ in range(10000)))
msg294550 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2017-05-26 13:32
Also discussed in Issue 13349
History
Date User Action Args
2022-04-11 14:58:46adminsetgithub: 74662
2017-05-27 19:40:12brett.cannonsetstatus: open -> closed
resolution: duplicate
stage: resolved
2017-05-26 13:32:27martin.pantersetsuperseder: Non-informative error message in index() and remove() functions

messages: + msg294550
nosy: + martin.panter
2017-05-26 08:32:59schensetnosy: + schen
messages: + msg294537
2017-05-26 02:22:02rhettingersetnosy: + rhettinger
messages: + msg294522
2017-05-25 22:55:37llllllllllsetmessages: + msg294519
2017-05-25 22:52:04llllllllllsettype: enhancement ->
messages: + msg294518
2017-05-25 22:48:56brett.cannonsettype: enhancement
messages: + msg294517
components: + Interpreter Core
2017-05-25 22:41:20llllllllllcreate