Skip to content

"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

Closed
cfbolz opened this issue Apr 23, 2025 · 10 comments
Closed

"unhashable type" is a beginner-unfriendly error message #132825

cfbolz opened this issue Apr 23, 2025 · 10 comments
Labels
type-feature A feature request or enhancement

Comments

@cfbolz
Copy link
Contributor

cfbolz commented Apr 23, 2025

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:

>>> s = set()
>>> s.add({'pages': 12, 'grade': 'A'})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Cannot use 'dict' as a set element (unhashable type).
>>> d = {}
>>> l = [1, 2, 3]
>>> d[l] = 12
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Cannot use 'list' as a dict key (unhashable type).

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

@cfbolz cfbolz added the type-feature A feature request or enhancement label Apr 23, 2025
@StanFromIreland

This comment has been minimized.

@vstinner
Copy link
Member

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')

@StanFromIreland:

I don't see any way to improve the message.

@cfbolz proposes the change the error message. Currently, the error message is not helpful:

>>> s=set(); s.add({})
TypeError: unhashable type: 'dict'

>>> d = {}; l = [1, 2, 3]; d[l] = 12
TypeError: unhashable type: 'list'

@StanFromIreland

This comment has been minimized.

@methane
Copy link
Member

methane commented Apr 23, 2025

TypeError: Cannot use 'list' as a dict key (unhashable type: 'list')

Maybe, exception message should starts with lowercase?
It seems other exceptions start with "cannot...".

@cfbolz
Copy link
Contributor Author

cfbolz commented Apr 23, 2025

@vstinner thanks a lot Victor, this makes me super happy!

@UltimateLobster
Copy link

UltimateLobster commented 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 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.

@vstinner
Copy link
Member

What about cases where the key is nested: {(1, 2, []): ""}?

You get this error message:

>>> {(1, 2, []): ""}
TypeError: cannot use 'tuple' as a dict key (unhashable type: 'list')

@merwok
Copy link
Member

merwok commented May 7, 2025

@hugovk This one could be added to What’s new section about improved error messages

@hugovk
Copy link
Member

hugovk commented May 7, 2025

PR welcome :)

@merwok
Copy link
Member

merwok commented May 7, 2025

OK #133622

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

7 participants