GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — "unhashable type" is a beginner-unfriendly error message · Issue #132825 · python/cpython

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

New issue
Copy link
New issue
Copy link
Closed
Closed
"unhashable type" is a beginner-unfriendly error message#132825
Copy link
Labels
type-featureA feature request or enhancementA feature request or enhancement

Description

@cfbolz
cfbolz
opened on Apr 23, 2025
Issue body actions

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

  • gh-132825: Enhance unhashable error messages for dict and set #132828
  • gh-132825: Fix typo in dict_unhashable_type() name #132847
Reacted by Thomas Germer
Reacted by Daniel Hollas

Activity

  1. cfbolz
    added
    type-featureA feature request or enhancementA feature request or enhancement
    on Apr 23, 2025
  2. StanFromIreland commented on Apr 23, 2025

    @StanFromIreland
    StanFromIreland
    on Apr 23, 2025 · Hidden as resolved
    show commentMore actions
  3. vstinner
    added 2 commits that reference this issue on Apr 23, 2025

    pythongh-132825: Change unhashable key error messages for dict and set

    0e0b837

    pythongh-132825: Change unhashable key error messages for dict and set

    b23bd8d
  4. vstinner commented on Apr 23, 2025

    @vstinner
    vstinner
    on Apr 23, 2025
    Member
    More actions

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

    @StanFromIreland
    StanFromIreland
    on Apr 23, 2025 · Hidden as resolved
    show commentMore actions
  6. methane commented on Apr 23, 2025

    @methane
    methane
    on Apr 23, 2025
    Member
    More actions

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

    Reacted by Victor Stinner
  7. cfbolz
    added a commit that references this issue on Apr 23, 2025

    gh-132825: Enhance unhashable error messages for dict and set (#132828)

    426449d
  8. cfbolz commented on Apr 23, 2025

    @cfbolz
    cfbolz
    on Apr 23, 2025
    ContributorAuthor
    More actions

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

    Reacted by Victor Stinner and Daniel Hollas
  9. cfbolz
    closed this as completedon Apr 23, 2025
  10. vstinner
    added a commit that references this issue on Apr 23, 2025

    pythongh-132825: Fix typo in dict_unhashable_type() name

    e5f5e23
  11. cfbolz
    added a commit that references this issue on Apr 23, 2025

    gh-132825: Fix typo in dict_unhashable_type() name (#132847)

    926ff69
  12. UltimateLobster commented on Apr 24, 2025

    @UltimateLobster
    UltimateLobster
    on Apr 24, 2025
    More actions

    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.

  13. vstinner commented on Apr 24, 2025

    @vstinner
    vstinner
    on Apr 24, 2025
    Member
    More actions

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

    @merwok
    merwok
    on May 7, 2025
    Member
    More actions

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

    Reacted by Hugo van Kemenade
  15. hugovk commented on May 7, 2025

    @hugovk
    hugovk
    on May 7, 2025
    Member
    More actions

    PR welcome :)

  16. merwok commented on May 7, 2025

    @merwok
    merwok
    on May 7, 2025
    Member
    More actions

    OK #133622

    Reacted by Hugo van Kemenade
  17. cjwatson
    added a commit that references this issue on Jan 7, 2026

    Fix test_set_only_work_in_hashable_types on Python 3.14

    9a2ea19
  18. cjwatson
    added a commit that references this issue on Mar 2, 2026

    Fix test_set_only_work_in_hashable_types on Python 3.14

    f955ab1
  19. lovasoa
    added a commit that references this issue on Mar 2, 2026

    Fix test_set_only_work_in_hashable_types on Python 3.14 (#286)

    1601dab
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 enhancementA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.