-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
"unhashable type" is a beginner-unfriendly error message #132825
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This comment has been minimized.
This comment has been minimized.
I like your proposal. I wrote #132828 to implement your suggested error messages. Examples: >>> s=set(); s.add({})
TypeError: Cannot use 'dict' as a set element (unhashable type: 'dict')
>>> d = {}; l = [1, 2, 3]; d[l] = 12
TypeError: Cannot use 'list' as a dict key (unhashable type: 'list')
@cfbolz proposes the change the error message. Currently, the error message is not helpful:
|
This comment has been minimized.
This comment has been minimized.
Maybe, exception message should starts with lowercase? |
@vstinner thanks a lot Victor, this makes me super happy! |
What about cases where the key is nested: The problem is not the tuple type, but the list type within. The error message would be false if it said that |
You get this error message:
|
@hugovk This one could be added to What’s new section about improved error messages |
PR welcome :) |
OK #133622 |
Feature or enhancement
Proposal:
Every time I'm teaching dicts and sets to beginners, someone tries to use lists or other mutable data structures as dict keys or set members. The error message in that case is unhelpful, because it describes the problem in very technical terms (when you're just starting you have no idea what a hash function is). it could be changed to also add what operation is impossible as a result:
That way, the problem is stated much more in terms of what the programmer is actually trying to do.
Has this already been discussed elsewhere?
No response given
Links to previous discussion of this feature:
No response
Linked PRs
The text was updated successfully, but these errors were encountered: