Repository navigation
Conversation
|
@localzet: Can you please sign the CLA? See previous comment. |
vstinner
left a comment
There was a problem hiding this comment.
dict and set catchs TypeError on hashing to write a hint on the error message: dict_unhashable_type() and set_unhashable_type(). The current TypeError exception is replaced with a new TypeError: notes are discarded. Would it be interesting to copy notes in this case?
Example:
key = ("alice", [])
# hash(key)
d = dict()
d[key] = 1Output:
Traceback (most recent call last):
File "/home/vstinner/python/main/x.py", line 4, in <module>
d[key] = 1
~^^^^^
TypeError: cannot use 'tuple' as a dict key (unhashable type: 'list')
If I uncomment the hash() call, I see the new note when hashing tuple item 1:
Traceback (most recent call last):
File "/home/vstinner/python/main/x.py", line 2, in <module>
hash(key)
~~~~^^^^^
TypeError: unhashable type: 'list'
when hashing tuple item 1
|
Thanks for reviewing this, Victor! I agree that keeping the tuple-item note visible when the tuple is used as a dict key or set element is important. The exception replacement is already tracked in gh-149313, with an existing implementation in PR #149505 (which chains the original exception). I intentionally kept this PR limited to There is a useful distinction between the options: copying Update: I implemented a small note-copying proof of concept in my fork: localzet#2. It preserves the concise outer dict/set message and includes regression tests for tuple-item notes and custom I have not folded those changes into this upstream PR yet to avoid competing with #149505 without coordination. Would you prefer a focused note-copying change as part of this PR, or to resolve dict/set exception preservation separately in gh-149313? |
vstinner
left a comment
There was a problem hiding this comment.
LGTM.
The exception replacement is already tracked in #149313, with an existing implementation in PR #149505 (which chains the original exception). I intentionally kept this PR limited to tuple_hash() to avoid duplicating that work.
Oh ok, if it's already tracked somewhere else, I'm fine with not addressing the issue in this PR.
|
@localzet Thanks for crediting the proposal in the description. Would you mind adding me to |
|
Thank you for the original gh-161433 proposal and for the helpful follow-up, @WakaTaira! I've added WakaTaira to |
Summary
When hashing a tuple fails with
TypeError, add an exception note identifying the index of the tuple item whose hash operation failed. Nested tuples accumulate one note per level. Preserve the original exception, message, existing notes, and traceback.For example, a direct call to
hash(("alice", []))reports the existingunhashable type: 'list'error with the added notewhen hashing tuple item 1.This is an implementation of gh-161433, based on the proposal by @WakaTaira.
Rationale and compatibility
tuple_hash()is changed. Successful hashing and hash values are unaffected.TypeErrorgets an additional note; other exceptions are propagated unchanged.Tests
A single focused test method covers five cases: a direct unhashable item, nested tuples, a custom
TypeError, existing notes, and a non-TypeErrorexception.A CPython
--with-pydebugbuild completed successfully in the fork andtest_tuple,test_dict,test_set, andtest_tracebackall passed:https://github.com/localzet/cpython/actions/runs/42240703650
The fork's lint checks passed on the subsequent change adding the NEWS fragment:
https://github.com/localzet/cpython/actions/runs/42241501515
This PR is ready for review and merging. The review feedback about test organization has been addressed. The original proposal author @WakaTaira is acknowledged in
Misc/ACKS.AI assistance
The initial patch, tests, and PR description were prepared with ChatGPT assistance. The implementation is available for human review and maintenance.