This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author sfreilich
Recipients sfreilich
Date 2020-06-25.12:48:17
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
Currently, if you (for example) put a dict as a value in a set or key in a dict, you get:

TypeError: unhashable type: 'dict'

I'm pretty sure this wording goes back a long time, but I've noticed that Python beginners tend to find this really confusing. It fits into a pattern of error messages where you have to explain why the error message is worded the way it is after you explain why the error message occurs at all. There are many instances of:

It would be clearer if the message was something like:

TypeError: 'dict' can not be used as a set value because it is an unhashable type.

(Or "dict key" instead of "set value".)

The exception is raised in PyObject_Hash, so that doesn't have some of the context about how/why hash was called. That's called in a lot of places.

Possibly, PyObject_Hash and PyObject_HashNotImplemented could take the format string passed to PyErr_Format as an optional second argument, defaulting to the current behavior? Then certain callers (in particular, the set and dict constructor, set and dict methods that add set values or add/modify dict keys) could provide clearer error messages.
Date User Action Args
2020-06-25 12:48:17sfreilichsetrecipients: + sfreilich
2020-06-25 12:48:17sfreilichsetmessageid: <>
2020-06-25 12:48:17sfreilichlinkissue41114 messages
2020-06-25 12:48:17sfreilichcreate