Motivation
Built-in error messages have become considerably more detailed.
For example, Python 3.14 gives me this:
>>> [a, b for a, b in ()]
SyntaxError: did you forget parentheses around the comprehension target?
>>> random
NameError: name 'random' is not defined. Did you forget to import 'random'?
>>> tupl
NameError: name 'tupl' is not defined. Did you mean: 'tuple'?
>>> [1, 2 3]
SyntaxError: invalid syntax. Perhaps you forgot a comma?
>>> {1: 2, 3}
SyntaxError: ':' expected after dictionary key
Python 3.15 added further improvements, for example:
>>> [1, 2, 3].push(4)
AttributeError: 'list' object has no attribute 'push'. Did you mean '.append'?
>>> {}.put("a", 1)
AttributeError: 'dict' object has no attribute 'put'. Use d[k] = v.
In my opinion, this is an excellent direction and makes the messages much more helpful. However, I find the way these messages are currently phrased somewhat surprising and inconsistent. I see three main reasons for this:
- Mixing errors and hints: The messages tend to combine a description of the actual error with a suggestion about what might help to address it. The suggestions are speculative and often more verbose, which can be distracting.
- Phrasing: The suggestions are phrased differently from one message to another. Sometimes they are direct questions (
Did you mean x?), sometimes possibilities (Perhaps you forgot y?), and sometimes instructions (Use z.). - Style: Suggestions tend to be phrased as complete sentences, which deviates from the compact style of traditional error messages. In particular, trailing periods and question marks feel a bit out of place.
Proposal
To address this, I would like to propose the following idea:
- Establish a convention for composing error messages. For example, stipulate that they should describe the actual error that occurred, without mixing in suggestions, and preferably without trailing periods or question marks.
- Provide a way to attach additional diagnostic hints to exceptions, separately from their actual messages. For example, a
hintsattribute holding zero or more strings could be added toException.
REPLs could then display errors like this, for instance:
>>> random
Traceback (most recent call last):
File "<python-input-1>", line 1, in <module>
random
NameError: name 'random' is not defined
Hint: consider importing 'random'
>>> {}.put("a", 1)
Traceback (most recent call last):
File "<python-input-0>", line 1, in <module>
{}.put("a", 1)
^^^^^^
AttributeError: 'dict' object has no attribute 'put'
Hint: use item assignment (d[k] = v)
It would also allow other environments, such as IDEs and applications, to decide whether and how to display these hints.
I am interested in hearing your thoughts. Would this direction be feasible and worth pursuing?