3

What is the reason that raising a KeyError does not format the string whereas other errors do? (notice the embedded \n vs a new line in the examples below)

In [1]: raise KeyError("Does not \n format")
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-1-2a34667e842b> in <module>()
----> 1 raise KeyError("Does not \n format")

KeyError: 'Does not \n format'

Whereas for a ValueError

In [2]: raise ValueError("Does \n format")
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-2-3f8ba28869e2> in <module>()
----> 1 raise ValueError("Does \n format")

ValueError: Does 
 format

Or an IndexError

 In [3]: raise IndexError("Does \n format")
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-4-2db4e87594fd> in <module>()
----> 1 raise IndexError("Does \n format")

IndexError: Does 
 format
mgilbert
  • 3,495
  • 4
  • 22
  • 39
  • @selcuk I would argue that this is not a duplicate of https://stackoverflow.com/questions/46892261/new-line-on-error-message-in-keyerror-python-3-3. The central piece of that question is **how** to get an IndexError to print an unquoted formatted string whereas I am asking **why** this is the case – mgilbert May 23 '18 at 00:17
  • I would also say that the accepted answer to the referenced question does not really address the **why**. I do agree the linked issue tracker in that answer does clarify this however, I think an appropriate answer would incorporate more of this discussion instead of just linking to it. I could close this and answer that question however as stated above I do not think that is the central question of that thread and that elaborated answer would be more appropriate here. – mgilbert May 23 '18 at 00:20
  • The reason is briefly described within the answer and the detailed explanation can be read in the issue itself, specifically this comment: `/* If args is a tuple of exactly one item, apply repr to args[0]. This is done so that e.g. the exception raised by {}[''] prints KeyError: '' rather than the confusing KeyError` – Selcuk May 23 '18 at 01:10
  • As I mentioned in my preceding comment, I do agree with you that the referenced bug tracker does answer the question and provide justification for why this is done. Specifically the portion you quoted `This is done so that e.g. the exception raised by {}[''] prints KeyError: '' rather than the confusing KeyError`. However this excerpt does not appear in the answer to the previous SO question, and since the central topic of that question was **how** to change the display of a `KeyError` and not **why** it is displayed that way it did not seem appropriate to include a new answer there. – mgilbert May 23 '18 at 02:19
  • Agreed. Care to edit the other answer to add that detail? – Selcuk May 23 '18 at 02:24
  • 1
    Okay I've incorporate that into the accepted answer to the linked question. – mgilbert May 23 '18 at 15:04

0 Answers0