I would like to propose an enhancement to Python error messages to make them more beginner-friendly by providing possible solutions along with the error description.
Many newcomers struggle with cryptic error messages, and a small hint about the possible fix could improve their learning experience. For example:
print("Hello" + 5)
Current Error:
TypeError: can only concatenate str (not “int”) to str`
Suggested Improvement:
TypeError: can only concatenate str (not “int”) to str. Did you mean print(“Hello” + str(5))?`
This approach is similar to what Rust and Swift do with their compiler messages. I would love to hear your thoughts on this.
I do really appreciate these friendly error messages in Rust.
But I don’t think they’re necessary in Python. Because in Python it’s so easy to play around with code, where in Rust it’s generally hard to make something compile.
Going with your example, print(“Hello” + str(5))? probably isn’t something anyone would actually want. In Rust it’s appreciated as a suggestion because when the code runs you can figure out what it’s doing. In Python, you can already do that.
a = input("Enter a number ") # user writes a number, say 3
print(a + 5)
Python would give you the exact same error message. However, in this case, the “correct” suggestion [*] would be to convert the string a to an integer and not the other way around.
The title of your post refers to “Beginner-friendly…”. For beginners, it is essential to ensure that any suggestion that is made is “almost certainly” the correct one in the context where it occurs. This is often difficult to do without doing a deeper analysis; in the above example it would mean to see if the value of the string might not be a numerical value. I believe that Python actually strikes the right balance between making useful suggestions when a traceback occurs and not overly complicating the code dealing with exceptions to try finding useful suggestions in all possible cases.
[*] For the example above, friendly/friendly-traceback, which is intended to help beginners and suggest fixes when an error message occurs, makes the following suggestion
Did you forget to convert the string a into an integer (int)?
With your original example print("hello" + 5), friendly/friendly_traceback does not currently offer a useful suggestion like the one you proposed.