Skip to content

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

Description

@cfbolz

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

Activity

  1. StanFromIreland commented on Apr 23, 2025

    @StanFromIreland
  2. added 2 commits that reference this issue on Apr 23, 2025
  3. vstinner commented on Apr 23, 2025

    @vstinner
    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'
    
  4. StanFromIreland commented on Apr 23, 2025

    @StanFromIreland
  5. methane commented on Apr 23, 2025

    @methane
    Member

    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...".

  6. added a commit that references this issue on Apr 23, 2025
  7. cfbolz commented on Apr 23, 2025

    @cfbolz
    ContributorAuthor

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

  8. added a commit that references this issue on Apr 23, 2025
  9. added a commit that references this issue on Apr 23, 2025
  10. UltimateLobster commented on Apr 24, 2025

    @UltimateLobster

    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.

  11. vstinner commented on Apr 24, 2025

    @vstinner
    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')
    
  12. merwok commented on May 7, 2025

    @merwok
    Member

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

  13. hugovk commented on May 7, 2025

    @hugovk
    Member

    PR welcome :)

  14. merwok commented on May 7, 2025

    @merwok
    Member
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions