You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Python 3.11 adds BaseException.add_note() method to add "notes" to exceptions (PEP 678). One of the Motivations is providing hints to novices:
programming environments for novices can provide more detailed descriptions of various errors, and tips for resolving them
I propose modifying the few existing errors which already provide hints by converting the sentence to a note: restrict the error message to the actual error, and move hints to notes.
Example of an existing error with a hint:
$ python3 -q
>>> import sys
>>> print >>sys.stderr, "hello"
...
TypeError: unsupported operand type(s) for>>: 'builtin_function_or_method' and '_io.TextIOWrapper'. Did you mean "print(<message>, file=<output_stream>)"?
Currently, the actual error ("unsupported operand") and the hint ("Did you mean...") are displayed on the same line. IMO displaying the hint on a separated line would make it easier to spot (it would be less likely to miss it).
Example raising a note manually to see how it's displayed:
try:
raiseTypeError("unsupported operand")
exceptExceptionasexc:
exc.add_note("Did you mean...")
raise
Current Python 3.12 output:
Traceback (most recent call last):
File "bug.py", line 2, in <module>
raise TypeError("unsupported operand")
TypeError: unsupported operand
Did you mean...
I proposed PR #96878 to add _PyErr_AddNote() function to the internal C API.
Python 3.11 adds BaseException.add_note() method to add "notes" to exceptions (PEP 678). One of the Motivations is providing hints to novices:
I propose modifying the few existing errors which already provide hints by converting the sentence to a note: restrict the error message to the actual error, and move hints to notes.
Example of an existing error with a hint:
Currently, the actual error ("unsupported operand") and the hint ("Did you mean...") are displayed on the same line. IMO displaying the hint on a separated line would make it easier to spot (it would be less likely to miss it).
Example raising a note manually to see how it's displayed:
Current Python 3.12 output:
I proposed PR #96878 to add
_PyErr_AddNote()function to the internal C API.I found these hints:
print >> 123:unsupported operand ...: Did you mean "print(<message>, "file=<output_stream>)"?1 is 2:"is" with a literal. Did you mean "=="?1 is not 2:"is" with a literal. Did you mean "!="?PyErr_Display()with_Py_Offer_Suggestions()(issue Offer suggestions on AttributeError and NameError #82711) is not a good fit: this code doesn't modify the exception on purposeprint "hello":Missing parentheses in call to 'print'. Did you mean print(...)?(similar error onexec code)