Title: Show which tuple item is unhashable when a nested dict key or set element is rejected · Issue #161433 · python/cpython · GitHub
Open Graph Title: Show which tuple item is unhashable when a nested dict key or set element is rejected · Issue #161433 · python/cpython
X Title: Show which tuple item is unhashable when a nested dict key or set element is rejected · Issue #161433 · python/cpython
Description: Feature or enhancement {([1],): 1} looks contrived, but this comes up in real code as soon as a tuple key is built from existing data: row = {"user": "alice", "tags": ["a", "b"]} groups = {} groups.setdefault((row["user"], row["tags"]), ...
Open Graph Description: Feature or enhancement {([1],): 1} looks contrived, but this comes up in real code as soon as a tuple key is built from existing data: row = {"user": "alice", "tags": ["a", "b"]} groups = {} groups...
X Description: Feature or enhancement {([1],): 1} looks contrived, but this comes up in real code as soon as a tuple key is built from existing data: row = {"user": "alice", "tags": ...
Opengraph URL: https://github.com/python/cpython/issues/161433
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Show which tuple item is unhashable when a nested dict key or set element is rejected","articleBody":"### Feature or enhancement\n\n`{([1],): 1}` looks contrived, but this comes up in real code as soon as a tuple\nkey is built from existing data:\n\n```python\nrow = {\"user\": \"alice\", \"tags\": [\"a\", \"b\"]}\ngroups = {}\ngroups.setdefault((row[\"user\"], row[\"tags\"]), [])\n```\n\n```\nTypeError: cannot use 'tuple' as a dict key (unhashable type: 'list')\n```\n\nTuples are normally valid keys, so the message appears to reject the tuple because\nof a list that isn't visibly related to it. Nothing says that the list is inside\nthe tuple, or where. This was pointed out on gh-132825 and again in the PEP 814\nthread, and it is harder to follow with deeper nesting or longer tuples.\n\nI'd suggest adding a note in the error path of `tuple_hash`, following what the\n`json` module already does for nested values:\n\n```pycon\n>>> json.dumps({\"a\": [1, object()]})\nTypeError: Object of type object is not JSON serializable\nwhen serializing list item 1\nwhen serializing dict item 'a'\n\n>>> hash((\"alice\", [\"a\", \"b\"])) # proposed\nTypeError: unhashable type: 'list'\nwhen hashing tuple item 1\n```\n\nThe note would be added only after an item's hash has failed, so successful hashing\nis unaffected. Nested tuples would produce one note per level.\n\n**Dependency on gh-149313:** the dict and set paths currently replace the exception\nand discard its notes, so the note would only become visible for dict keys and set\nelements once that issue is fixed. Until then, only direct `hash()` calls would\nshow it.\n\nReproduced on 3.14.6 and 3.15.0b2.\n\nI noticed this behavior and found it confusing myself, and the proposal is my own. I used Claude to help write this up in English and to re-check the behavior.\n\n### Has this already been discussed elsewhere?\n\nThis is a minor feature, which does not need previous discussion elsewhere\n\n### Links to previous discussion of this feature:\n\n- gh-132825 (comment on nested keys): https://github.com/python/cpython/issues/132825#issuecomment-2826319275\n- PEP 814 thread: https://discuss.python.org/t/pep-814-add-frozendict-built-in-type/104854/16\n- gh-149313\n\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-161496\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/WakaTaira","@type":"Person","name":"WakaTaira"},"datePublished":"2026-11-05T16:30:37Z","url":"https://github.com/python/cpython/issues/161433"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:7f5f6a3c-562a-9e99-6157-f9ef996803ad |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B18A:291B58:30E378F:3E08177:6B02028D |
| html-safe-nonce | a79e6fb8c36ce6f4cf2e78c7d44df288128a6d9926b1286e70696f9131b9d9a7 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMThBOjI5MUI1ODozMEUzNzhGOjNFMDgxNzc6NkIwMjAyOEQiLCJ2aXNpdG9yX2lkIjoiMzA3MjE5ODQwMjIwNTk0MjQxMyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 886bf4cd8dc7a380650a0f93efc1fb8fafb52f23536c6d291916fc615dac02fc |
| hovercard-subject-tag | issue:6227300668 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/161433/issue_layout |
| twitter:image | https://opengraph.githubassets.com/0d66c627e2ba0c7c419898f77eed6b2ceb471002c3f42dd51fac4e13d8e50c46/python/cpython/issues/161433 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/0d66c627e2ba0c7c419898f77eed6b2ceb471002c3f42dd51fac4e13d8e50c46/python/cpython/issues/161433 |
| og:image:alt | Feature or enhancement {([1],): 1} looks contrived, but this comes up in real code as soon as a tuple key is built from existing data: row = {"user": "alice", "tags": ["a", "b"]} groups = {} groups... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | WakaTaira |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width