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
{{ message }}
Repository navigation
"unhashable type" is a beginner-unfriendly error message #132825
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:
What about cases where the key is nested: {(1, 2, []): ""}?
The problem is not the tuple type, but the list type within. The error message would be false if it said that tuple cannot be used as a dict key. It would also be misleading if it said that list cannot be used a dict key, since the user did not use the list directly as a key.
Activity
StanFromIreland commentedon Apr 23, 2025
pythongh-132825: Change unhashable key error messages for dict and set
pythongh-132825: Change unhashable key error messages for dict and set
vstinner commentedon Apr 23, 2025
I like your proposal. I wrote #132828 to implement your suggested error messages.
Examples:
@StanFromIreland:
@cfbolz proposes the change the error message. Currently, the error message is not helpful:
StanFromIreland commentedon Apr 23, 2025
methane commentedon Apr 23, 2025
Maybe, exception message should starts with lowercase?
It seems other exceptions start with "cannot...".
gh-132825: Enhance unhashable error messages for dict and set (#132828)
cfbolz commentedon Apr 23, 2025
@vstinner thanks a lot Victor, this makes me super happy!
pythongh-132825: Fix typo in dict_unhashable_type() name
gh-132825: Fix typo in dict_unhashable_type() name (#132847)
UltimateLobster commentedon Apr 24, 2025
What about cases where the key is nested:
{(1, 2, []): ""}?The problem is not the tuple type, but the list type within. The error message would be false if it said that
tuplecannot be used as a dict key. It would also be misleading if it said thatlistcannot be used a dict key, since the user did not use the list directly as a key.vstinner commentedon Apr 24, 2025
You get this error message:
merwok commentedon May 7, 2025
@hugovk This one could be added to What’s new section about improved error messages
hugovk commentedon May 7, 2025
PR welcome :)
merwok commentedon May 7, 2025
OK #133622
Fix test_set_only_work_in_hashable_types on Python 3.14
Fix test_set_only_work_in_hashable_types on Python 3.14
Fix test_set_only_work_in_hashable_types on Python 3.14 (#286)