IndexError message should show index and length

What makes you think that?

Personally, I’ve always wanted to know why the error occurred, and knowing the index and length was always helpful to this end.

4 Likes

Do you have a meaningfully useful code example that produces IndexError where debugging would’ve been made easier if the error message included the index and the length?

As mentioned in my previous post, I agree with Raymond’s sentiment in the GitHub discussion:

and:

because although at first glance I thought “of course knowing more details would’ve helped”, I struggle to come up with a concrete example from my past debugging experiences where knowing the actual index and length that trigger an IndexError would’ve helped. Knowing that there’s an IndexError is itself sufficient for me to realize the logical errors I’ve made.

That said, I’m sure there can still be cases where knowing index and length can help even though I struggle to come with a good use case myself. Including index and length in the exception object is cheap, and message formatting can be done lazily, so as long as you can lay out a few good cases where index and length are actually helpful to debugging the devs are likely to reconsider.

When debugging a program, it can help if you print out the values of the relevant variables to see whether they are you think they are.

If there’s an IndexError, saying what the index and the length are is helpful for the same reason. do the values look “reasonable”?

You could always have a fallback to the current message if the index or the length is extremely large.

4 Likes

There are some hints to be gleamed from indexes and lengths.

  • Index 3 out of bounds for list of length 3 → Either an off by one error or a for i in range(n): a[i] where n wasn’t expected to exceed len(a)
  • Index -1852686345 out of bounds for list of length 15670 → Some index arithmetic gone wrong
  • Index 0 or -1 out of bounds for list of length 0 → An attempt to get the first or last element that fails to handle the null case
  • Index 10 out of bounds for list of length 0 → List isn’t populating for some reason

In contexts where there’s a rough idea of what such values should be, seeing an unexpected index or an unexpected length is a hint as to whether to scrutinise the list population or the index calculation.

They’re certainly the first things I check when debugging an index error unless I already know better.

17 Likes

A solution would be to:

  • leave the message as is
  • add index and length attributes
  • add a details property the produces a messages that includes index and length. Details could be made smart enough not to format very large lengths if unnecessary. e.g. passing a negative index in.
2 Likes