Fix NamedTuple hash returning 0 on Python 3.14+#980
Open
worksbyfriday wants to merge 1 commit intojcrist:mainfrom
Open
Fix NamedTuple hash returning 0 on Python 3.14+#980worksbyfriday wants to merge 1 commit intojcrist:mainfrom
worksbyfriday wants to merge 1 commit intojcrist:mainfrom
Conversation
In Python 3.14, CPython added hash caching to tuples (gh-131525).
PyTupleObject gained an ob_hash field that is checked by tuple_hash()
before computing: if ob_hash != -1, the cached value is returned
immediately.
When msgspec allocates NamedTuples via tp_alloc (which zero-initializes
memory via calloc), ob_hash is set to 0 — a valid cached hash value.
This causes hash() to return 0 for all msgspec-deserialized NamedTuples,
breaking their use as dictionary keys and in sets.
The fix resets ob_hash to -1 ("not yet computed") immediately after
tp_alloc in all three NamedTuple construction sites: msgpack decoder,
JSON decoder, and convert. The reset is conditional on PY314_PLUS and
compiles to a no-op on earlier Python versions.
Fixes jcrist#967
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.
Summary
Fixes #967.
In Python 3.14, CPython added hash caching to tuples (gh-131525, PR #131529).
PyTupleObjectgained anob_hashfield initialized to-1("not yet computed"). Whentuple_hash()seesob_hash != -1, it returns the cached value immediately.msgspec allocates NamedTuples via
tp_alloc, which zero-initializes memory. This setsob_hashto0— a valid cached hash value — sohash()returns0for all msgspec-deserialized NamedTuples without ever computing the real hash. This breaks dictionary lookups and set membership when deserialized NamedTuples are used as keys.The fix adds
MS_TUPLE_RESET_HASH(op)— a macro that setsob_hash = -1on Python 3.14+ (no-op on earlier versions) — immediately aftertp_allocin all three NamedTuple construction sites:mpack_decode_namedtuple(msgpack decoder)json_decode_namedtuple(JSON decoder)convert_seq_to_namedtuple(convert)Test plan
test_namedtuple_hash_preserved_after_roundtripintest_common.py(covers msgpack + JSON)test_namedtuple_hash_preservedintest_convert.py(covers convert path)hash(original) == hash(decoded)and dict lookup with decoded keysob_hashexists inPyTupleObject