Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions msgspec/_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -8342,6 +8342,9 @@ TypedDictInfo_error_missing(TypedDictInfo *self, PyObject *dict, PathNode *path)
}
}
}
// Should be unreachable, but may happen if the TypedDict info
// is inconsistent.
assert(0);
}

static int
Expand Down
7 changes: 7 additions & 0 deletions msgspec/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,13 @@ def get_typeddict_info(obj):
hints[k] = v.__args__[0]
else:
hints[k] = v

# This can happen if there is a bug in the TypedDict implementation;
# such a bug was present in Python 3.14.
if not all(k in hints for k in required):
raise RuntimeError(
f"Required set {required} contains keys that are no in hints: {hints.keys()}"
)
return hints, required


Expand Down
18 changes: 18 additions & 0 deletions tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2112,6 +2112,24 @@ class Ex(Base, total=False):
dec.decode(proto.encode({"b": "two"}))
assert "Object missing required field `a`" == str(rec.value)

@pytest.mark.parametrize("use_typing_extensions", [False, True])
def test_broken_typeddict(self, proto, use_typing_extensions):
# Check that we don't crash if a TypedDict has incorrect
# introspection data.
if use_typing_extensions:
tex = pytest.importorskip("typing_extensions")
cls = tex.TypedDict
else:
cls = TypedDict

class Ex(cls, total=False):
c: str
Ex.__annotations__ = {"c": "str"}
Ex.__required_keys__ = {"a", "b"}

with pytest.raises(RuntimeError):
proto.Decoder(Ex)

@pytest.mark.parametrize("use_typing_extensions", [False, True])
def test_required_and_notrequired(self, proto, use_typing_extensions):
if use_typing_extensions:
Expand Down