Skip to content

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

Closed
@cfbolz

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

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