René's URL Explorer Experiment


Title: Exception notes discarded from TypeError within __hash__() · Issue #149313 · python/cpython · GitHub

Open Graph Title: Exception notes discarded from TypeError within __hash__() · Issue #149313 · python/cpython

X Title: Exception notes discarded from TypeError within __hash__() · Issue #149313 · python/cpython

Description: Bug report Bug description: Context I'm maintaining this library that requires users to construct deeply nested structures that are used as dict keys. To alleviate the annoyance of figuring out where a non-hashable value has been introdu...

Open Graph Description: Bug report Bug description: Context I'm maintaining this library that requires users to construct deeply nested structures that are used as dict keys. To alleviate the annoyance of figuring out whe...

X Description: Bug report Bug description: Context I'm maintaining this library that requires users to construct deeply nested structures that are used as dict keys. To alleviate the annoyance of figuring out...

Opengraph URL: https://github.com/python/cpython/issues/149313

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Exception notes discarded from TypeError within __hash__()","articleBody":"# Bug report\n\n### Bug description:\n\n## Context\n\nI'm maintaining this library that requires users to construct deeply nested structures that are used as dict keys. To alleviate the annoyance of figuring out where a non-hashable value has been introduced into the key structure, I started using dedicated data structures with a `__hash__()`, which adds location information to the exception's notes:\n\n```python\nfrom dataclasses import dataclass\nfrom dataclasses import field\n\n@dataclass\nclass KeyStruct:\n    name: str\n    first: object = field(repr=False)\n    second: object = field(repr=False)\n\n    def __hash__(self) -> int:\n        try:\n            h1 = hash(self.first)\n        except TypeError as e:\n            e.add_note(f\"within {self}.first\")\n            raise\n\n        try:\n            h2 = hash(self.second)\n        except TypeError as e:\n            e.add_note(f\"within {self}.second\")\n            raise\n\n        return hash((self.name, h1, h2))\n```\n\nSo when such a structure is used, information about the location of the offending value can be provided:\n\n```python\ns = KeyStruct(\"foo\", 1, KeyStruct(\"bar\", 2, KeyStruct(\"spam\", 3, [])))\ns_dict = {s: \"hello\"}\n```\n\n```\nTraceback (most recent call last):\n  File \"/Users/michi/Documents/Projects/Declarative_Python/declarative-python/src/hello.py\", line 43, in \u003cmodule>\n    s_dict = {s: \"hello\"}\n             ^^^^^^^^^^^^\n  File \"/Users/michi/Documents/Projects/Declarative_Python/declarative-python/src/hello.py\", line 34, in __hash__\n    h2 = hash(self.second)\n  File \"/Users/michi/Documents/Projects/Declarative_Python/declarative-python/src/hello.py\", line 34, in __hash__\n    h2 = hash(self.second)\n  File \"/Users/michi/Documents/Projects/Declarative_Python/declarative-python/src/hello.py\", line 28, in __hash__\n    h1 = hash(self.first)\nTypeError: unhashable type: 'list'\nwithin KeyStruct(name='spam').first\nwithin KeyStruct(name='bar').second\nwithin KeyStruct(name='foo').second\n```\n\nIt is not very beautiful, but very helpful. :)\n\n## The Bug\n\nThe above output is from Python 3.13. The output changes with Python 3.14 (3.14.4 on macOS 14.8.5):\n\n```\nTraceback (most recent call last):\n  File \"/Users/michi/Documents/Projects/Declarative_Python/declarative-python/src/hello.py\", line 43, in \u003cmodule>\n    s_dict = {s: \"hello\"}\n             ^^^^^^^^^^^^\nTypeError: cannot use 'KeyStruct' as a dict key (unhashable type: 'list')\n```\n\nI suspect that the source of this is the improved error messages introduced with https://github.com/python/cpython/issues/132825. If I interpret [this code](https://github.com/python/cpython/pull/132828/changes#diff-b08a47ddc5bc20b2e99ac2e5aa199ca24a56b994e7bc64e918513356088c20aeR2279) correctly, the `dict` and `set` methods that need to hash a key will catch and discard the raised `TypeError` and only re-use the exception message.\n\nIMHO his is unfortunate and I consider it a regression.\n\nI'm not sure what the best way to solve this would be:\n\n- As an experienced Python programmer, I would have expected the implementation of `dict` and `set` to attach the existing exception as `__cause__` on the new exception. That would solve my use case.\n\n- An alternative would be to copy the notes from the old to the new exception. If you consider the notes a part of something I'll call the \"extended exception message\", the code would conceptually replace the exception, modifying, but retaining its \"extended exception message\" in full.\n\n  But this sill has the drawback of discarding the original stack trace, which I also consider valuable in many cases.\n\n- Update the passage describing the improvement in [What’s new in Python 3.14](https://docs.python.org/3/whatsnew/3.14.html) to document the change in behavior and provide a workaround. E.g.:\n\n  > - Improved error message when trying to add an instance of an unhashable type to a [`dict`](https://docs.python.org/3/library/stdtypes.html#dict) or [`set`](https://docs.python.org/3/library/stdtypes.html#set). (Contributed by CF Bolz-Tereick and Victor Stinner in [gh-132828](https://github.com/python/cpython/issues/132828).)\n  >\n  >   (code block)\n  >\n  >   In those instances, the original `TypeError` and its notes and stack trace are replaced. You can raise a subclass of `TypeError` from a `__hash__()` implementation to opt-out of this feature.\n\n  AFAICT, the improvement is not documented elsewhere in Python's documentation.\n\n### CPython versions tested on:\n\n3.14\n\n### Operating systems tested on:\n\nmacOS\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-149505\n* gh-161536\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/Feuermurmel","@type":"Person","name":"Feuermurmel"},"datePublished":"2026-05-03T09:25:25Z","url":"https://github.com/python/cpython/issues/149313"}

react-profiling0
react-import-mapreact,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-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:94db9080-1b28-dfeb-b397-885d011e3990
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idE30E:76F02:3B6E59F:4BF559F:6B020387
html-safe-nonce861cbfd34ac832bb42efceffadf7a3a7027dc1793a3416822cad727e941fdf75
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFMzBFOjc2RjAyOjNCNkU1OUY6NEJGNTU5Rjo2QjAyMDM4NyIsInZpc2l0b3JfaWQiOiI4MDU2Mzc3MzEzNDg4NjY3NTI3IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmacedd24285686023e62ed2538794078947641dc3e1900cc58f3ad67d569cbcad68
hovercard-subject-tagissue:4371384086
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/149313/issue_layout
twitter:imagehttps://opengraph.githubassets.com/83a442980aaf733996c9bfb9402169114dc8ac0bf03714aea29cd52d04ae18f5/python/cpython/issues/149313
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/83a442980aaf733996c9bfb9402169114dc8ac0bf03714aea29cd52d04ae18f5/python/cpython/issues/149313
og:image:altBug report Bug description: Context I'm maintaining this library that requires users to construct deeply nested structures that are used as dict keys. To alleviate the annoyance of figuring out whe...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameFeuermurmel
hostnamegithub.com
expected-hostnamegithub.com
None87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456
turbo-cache-controlno-preview
go-importgithub.com/python/cpython git https://github.com/python/cpython.git
octolytics-dimension-user_id1525981
octolytics-dimension-user_loginpython
octolytics-dimension-repository_id81598961
octolytics-dimension-repository_nwopython/cpython
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id81598961
octolytics-dimension-repository_network_root_nwopython/cpython
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release423dbb54c5e7bfecc838eb387c1435eb3d0e760d
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/python/cpython/issues/149313#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F149313
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F149313
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=python%2Fcpython
Reloadhttps://github.com/python/cpython/issues/149313
Reloadhttps://github.com/python/cpython/issues/149313
Reloadhttps://github.com/python/cpython/issues/149313
pythonhttps://github.com/python
cpythonhttps://github.com/python/cpython
Codehttps://github.com/python/cpython
Issues5k+ (5k+)https://github.com/python/cpython/issues
Pull requests2.4k (2.4k)https://github.com/python/cpython/pulls
Actionshttps://github.com/python/cpython/actions
Projectshttps://github.com/python/cpython/projects
Security and qualityhttps://github.com/python/cpython/security
Insightshttps://github.com/python/cpython/insights
https://github.com/python/cpython
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/149313
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/149313
#149505https://github.com/python/cpython/pull/149505
Exception notes discarded from TypeError within __hash__()https://github.com/python/cpython/issues/149313#top
#149505https://github.com/python/cpython/pull/149505
interpreter-core(Objects, Python, Grammar, and Parser dirs)https://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22interpreter-core%22
type-bugAn unexpected behavior, bug, or errorhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-bug%22
https://github.com/Feuermurmel
Feuermurmelhttps://github.com/Feuermurmel
on May 3, 2026https://github.com/python/cpython/issues/149313#issue-4371384086
#132825https://github.com/python/cpython/issues/132825
this codehttps://github.com/python/cpython/pull/132828/changes#diff-b08a47ddc5bc20b2e99ac2e5aa199ca24a56b994e7bc64e918513356088c20aeR2279
What’s new in Python 3.14https://docs.python.org/3/whatsnew/3.14.html
dicthttps://docs.python.org/3/library/stdtypes.html#dict
sethttps://docs.python.org/3/library/stdtypes.html#set
gh-132828https://github.com/python/cpython/issues/132828
gh-149313: Preserve exception notes/cause in dict/set unhashable type… #149505https://github.com/python/cpython/pull/149505
gh-149313: Keep hash error details for dict keys and set elements #161536https://github.com/python/cpython/pull/161536
Feuermurmelhttps://github.com/Feuermurmel
type-bugAn unexpected behavior, bug, or errorhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-bug%22
on May 3, 2026https://github.com/python/cpython/issues/149313#event-25105086164
picnixzhttps://github.com/picnixz
interpreter-core(Objects, Python, Grammar, and Parser dirs)https://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22interpreter-core%22
on May 3, 2026https://github.com/python/cpython/issues/149313#event-25106860183
https://github.com/anujbharambe
anujbharambehttps://github.com/anujbharambe
on May 7, 2026https://github.com/python/cpython/issues/149313#issuecomment-4399413482
https://github.com/WakaTaira
WakaTairahttps://github.com/WakaTaira
on Oct 27, 2026https://github.com/python/cpython/issues/149313#issuecomment-6440465619
87589dahttps://github.com/python/cpython/commit/87589dab6f676f015c52a3d25f298b4aa10ed814
gh-132825https://github.com/python/cpython/issues/132825
localzethttps://github.com/localzet
on Nov 7, 2026https://github.com/python/cpython/issues/149313#event-35454318305
pythongh-149313: Construct replacement directly and report unraisable…https://github.com/localzet/cpython/commit/5b3c99ef084b287356710510219a755b5f15618c
pythongh-149313https://github.com/python/cpython/issues/149313
5b3c99ehttps://github.com/localzet/cpython/commit/5b3c99ef084b287356710510219a755b5f15618c
pythongh-149313: Separate hashing regression scenarios and assert for…https://github.com/localzet/cpython/commit/7f54ba747ffeea6db0e0c01b70484bf346cf6754
pythongh-149313https://github.com/python/cpython/issues/149313
7f54ba7https://github.com/localzet/cpython/commit/7f54ba747ffeea6db0e0c01b70484bf346cf6754
pythongh-149313: Address TypeError replacement and review test feedbackhttps://github.com/localzet/cpython/commit/e4e57a4aa1f0cd1f9708e89a6a32cbb373c272f2
pythongh-149313https://github.com/python/cpython/issues/149313
e4e57a4https://github.com/localzet/cpython/commit/e4e57a4aa1f0cd1f9708e89a6a32cbb373c272f2
WakaTairahttps://github.com/WakaTaira
on Nov 8, 2026https://github.com/python/cpython/issues/149313#event-35482245102
Merge branch 'main' into pythongh-149313-preserve-hash-exception-infohttps://github.com/WakaTaira/cpython/commit/60db6f3bb6c077bd92d3c070e140b3bb9f2ae01d
pythongh-149313https://github.com/python/cpython/issues/149313
60db6f3https://github.com/WakaTaira/cpython/commit/60db6f3bb6c077bd92d3c070e140b3bb9f2ae01d
pythongh-149313: Address review commentshttps://github.com/WakaTaira/cpython/commit/293f80d7a7562cda7227940eb4ff0169fffc6b11
pythongh-149313https://github.com/python/cpython/issues/149313
293f80dhttps://github.com/WakaTaira/cpython/commit/293f80d7a7562cda7227940eb4ff0169fffc6b11
pythongh-149313: Add "Patch by" to the NEWS entryhttps://github.com/WakaTaira/cpython/commit/e51cad1203ebfcb18541b6e19ff9363fb067ae21
pythongh-149313https://github.com/python/cpython/issues/149313
e51cad1https://github.com/WakaTaira/cpython/commit/e51cad1203ebfcb18541b6e19ff9363fb067ae21
Sign up for freehttps://github.com/signup?return_to=https://github.com/python/cpython/issues/149313
Sign in to commenthttps://github.com/login?return_to=https://github.com/python/cpython/issues/149313
interpreter-core(Objects, Python, Grammar, and Parser dirs)https://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22interpreter-core%22
type-bugAn unexpected behavior, bug, or errorhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-bug%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.