classification
Title: KeyError exception is more difficult to read due to quoting
Type: Stage: resolved
Components: Versions: Python 3.10, Python 3.9, Python 3.8
process
Status: closed Resolution:
Dependencies: Superseder: Strings passed to KeyError do not round trip
View: 2651
Assigned To: Nosy List: Anthony Sottile, martin.panter
Priority: normal Keywords:

Created on 2020-09-12 00:17 by Anthony Sottile, last changed 2020-09-12 02:10 by Anthony Sottile. This issue is now closed.

Messages (2)
msg376754 - (view) Author: Anthony Sottile (Anthony Sottile) * Date: 2020-09-12 00:17
For example:

>>> import pwd
>>> pwd.getpwnam("test")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: "getpwnam(): name not found: 'test'"


An improvement would be something which doesn't have the extra set of quotes such as:

>>> import pwd
>>> pwd.getpwnam("test")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: getpwnam(): name not found: 'test'



I understand this is due to the `KeyError` repr, I wonder if something could be done to improve this (maybe a subclass of KeyError? maybe a new  optional named-only argument to KeyError?)


Seems there are others which could be improved here as well:

>>> import unicodedata
>>> unicodedata.lookup('zzz')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: "undefined character name 'zzz'"
msg376762 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2020-09-12 01:57
Perhaps a duplicate of Issue 2651, closed because it was too hard to fix without breaking compatibility.
History
Date User Action Args
2020-09-12 02:10:38Anthony Sottilesetstatus: open -> closed
stage: resolved
2020-09-12 01:57:05martin.pantersetsuperseder: Strings passed to KeyError do not round trip

messages: + msg376762
nosy: + martin.panter
2020-09-12 00:17:04Anthony Sottilecreate