Repository navigation
Conversation
The TypeError raised when hashing a dict key or a set element is replaced with a TypeError describing the container, which discards the traceback, the cause and the notes of the original exception. Chain the original exception as the cause of the replacement if it has a traceback or a cause. Otherwise, chaining would only repeat its message, so copy its notes instead. __context__ is intentionally not checked: a TypeError raised from C code inside an except block gets the handled exception as its context, and the replacement gets the same context anyway. An error raised when formatting the new message is left unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01YSkzESiXGfCM1jUTyLpxHo
WakaTaira
marked this pull request as ready for review
November 7, 2026 07:16
WakaTaira
requested review from
markshannon,
methane and
rhettinger
as code owners
November 7, 2026 07:16
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
dictandsetreplace theTypeErrorfrom a failed key hash with one naming the container, which has dropped the original traceback, cause and notes since 3.14. The original exception now survives when it holds something worth keeping, and{[]: 1}keeps its single-line message.TypeErrorhas__hash__)__cause__Output for a
__hash__that raises aTypeErrorwith the notewithin Key.first:Before this change, only the last line and its traceback appear.
Start with
_PyErr_ReplaceTypeErrorinPython/errors.c. Three decisions are worth checking:__context__does not trigger chaining. A C-levelTypeErrorraised inside anexceptblock gets the handled exception as its context, so checking it brings the duplicatedunhashable type: 'list'message back. The replacement gets the same context anyway.{([1],): 1}, as described in the Exception notes discarded from TypeError within __hash__() #149313 discussion.__str__raises, that error propagates with its own cause.Verified on Linux with a debug build:
./python -m test -j0, where onlytest_perf_profilerandtest_profilingfail because of the local NixOS on WSL environment, and-R 3:3ontest_dictandtest_set. I did not test the free-threaded build locally; it passes in this PR's CI.A personal note: gh-161433 was my first issue on a project this large. I had a tuple-note prototype ready locally and planned to wait for comments before opening a PR. When I woke up, @localzet had already opened #161496, and @anujbharambe's #149505 and @localzet's fork experiment were there to learn from. This PR builds on both of them, and I wrote it now because I wanted to see this change through to a PR myself.
I used Claude Code to help with the implementation and tests, and I reviewed the result and can explain it.
🤖 Generated with Claude Code
https://claude.ai/code/session_01YSkzESiXGfCM1jUTyLpxHo