Skip to content

Commit 7e90e24

Browse files
Fix NamedTuple hash returning 0 on Python 3.14+ (#980)
* Fix NamedTuple hash always returning 0 on Python 3.14+ 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 #967 * Fixup --------- Co-authored-by: Jim Crist-Harif <[email protected]>
1 parent 719a216 commit 7e90e24

3 files changed

Lines changed: 53 additions & 0 deletions

File tree

‎src/msgspec/_core.c‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@
2222
#define PY313_PLUS (PY_VERSION_HEX >= 0x030d0000)
2323
#define PY314_PLUS (PY_VERSION_HEX >= 0x030e0000)
2424

25+
/* In Python 3.14+, tuples cache their hash value in ob_hash. When a tuple is
26+
* allocated via tp_alloc (which zero-initializes memory), ob_hash is 0 — a
27+
* valid cached hash — so tuple_hash() returns 0 without computing the real
28+
* hash. Reset it to -1 ("not yet computed") after allocation. */
29+
#if PY314_PLUS
30+
#define MS_TUPLE_RESET_HASH(op) (((PyTupleObject *)(op))->ob_hash = -1)
31+
#else
32+
#define MS_TUPLE_RESET_HASH(op) ((void)0)
33+
#endif
34+
2535
/* Hint to the compiler not to store `x` in a register since it is likely to
2636
* change. Results in much higher performance on GCC, with smaller benefits on
2737
* clang */
@@ -15637,6 +15647,7 @@ mpack_decode_namedtuple(
1563715647
PyTypeObject *nt_type = (PyTypeObject *)(info->class);
1563815648
PyObject *res = nt_type->tp_alloc(nt_type, nfields);
1563915649
if (res == NULL) goto error;
15650+
MS_TUPLE_RESET_HASH(res);
1564015651
for (Py_ssize_t i = 0; i < nfields; i++) {
1564115652
PyTuple_SET_ITEM(res, i, NULL);
1564215653
}
@@ -17916,6 +17927,7 @@ json_decode_namedtuple(JSONDecoderState *self, TypeNode *type, PathNode *path) {
1791617927
PyTypeObject *nt_type = (PyTypeObject *)(info->class);
1791717928
PyObject *out = nt_type->tp_alloc(nt_type, nfields);
1791817929
if (out == NULL) goto error;
17930+
MS_TUPLE_RESET_HASH(out);
1791917931
for (Py_ssize_t i = 0; i < nfields; i++) {
1792017932
PyTuple_SET_ITEM(out, i, NULL);
1792117933
}
@@ -21079,6 +21091,7 @@ convert_seq_to_namedtuple(
2107921091
PyTypeObject *nt_type = (PyTypeObject *)(info->class);
2108021092
PyObject *out = nt_type->tp_alloc(nt_type, nfields);
2108121093
if (out == NULL) goto error;
21094+
MS_TUPLE_RESET_HASH(out);
2108221095
for (Py_ssize_t i = 0; i < nfields; i++) {
2108321096
PyTuple_SET_ITEM(out, i, NULL);
2108421097
}

‎tests/unit/test_common.py‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2492,6 +2492,29 @@ class Ex(NamedTuple, Generic[T]):
24922492
assert "`$[1][0]`" in str(rec.value)
24932493
assert "Expected `int`, got `str`" in str(rec.value)
24942494

2495+
def test_namedtuple_hash_preserved_after_roundtrip(self, proto):
2496+
"""Hash of deserialized NamedTuple matches hash of original.
2497+
2498+
Regression test for gh-967. In Python 3.14+, tuples cache their hash
2499+
in ob_hash. When msgspec allocates NamedTuples via tp_alloc (which
2500+
zero-initializes memory), ob_hash was left at 0 — a valid cached hash
2501+
— so hash() returned 0 for all deserialized NamedTuples."""
2502+
2503+
class Key(NamedTuple):
2504+
value: int
2505+
2506+
original = Key(value=42)
2507+
encoded = proto.encode(original)
2508+
decoded = proto.decode(encoded, type=Key)
2509+
2510+
assert original == decoded
2511+
assert hash(original) == hash(decoded)
2512+
2513+
# Verify dict lookup works (requires matching hash)
2514+
d = {original: "found"}
2515+
assert decoded in d
2516+
assert d[decoded] == "found"
2517+
24952518

24962519
class TestDataclass:
24972520
def test_encode_dataclass_err_invalid_dataclass_fields(self, proto):

‎tests/unit/test_convert.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,6 +1113,23 @@ class Ex3(NamedTuple):
11131113
with pytest.raises(ValidationError, match="Expected `str`, got `int`"):
11141114
convert(msg, Ex3)
11151115

1116+
def test_namedtuple_hash_preserved(self):
1117+
"""Hash of converted NamedTuple matches hash of original.
1118+
1119+
Regression test for gh-967."""
1120+
1121+
class Key(NamedTuple):
1122+
value: int
1123+
1124+
original = Key(value=42)
1125+
converted = convert([42], Key)
1126+
1127+
assert original == converted
1128+
assert hash(original) == hash(converted)
1129+
1130+
d = {original: "found"}
1131+
assert converted in d
1132+
11161133

11171134
class TestDict:
11181135
def test_any_dict(self, dictcls):

0 commit comments

Comments
 (0)