fix(engine): explicit type guard in resolve_module for malformed snapshots

This commit is contained in:
DumiFlex
2026-04-26 16:20:52 +03:00
parent f33ac4c980
commit da0daf47e0
2 changed files with 14 additions and 0 deletions
+2
View File
@@ -44,6 +44,8 @@ def register_handler(handler: type[ModuleHandler]) -> None:
def resolve_module(snapshot: dict[str, Any], ctx: Any) -> dict[str, str]:
"""Route a snapshot to its registered handler and return the resolution."""
type_id = snapshot.get("type")
if not isinstance(type_id, str):
raise UnknownModuleType(type_id)
handler = _HANDLERS.get(type_id)
if handler is None:
raise UnknownModuleType(type_id)
+12
View File
@@ -118,3 +118,15 @@ def test_register_handler_overwrites_existing_for_same_type_id():
def test_unknown_module_type_inherits_keyerror():
"""UnknownModuleType must be a KeyError subclass for caller convenience."""
assert issubclass(UnknownModuleType, KeyError)
def test_resolve_missing_type_key_raises():
"""Snapshots without a `type` key are malformed; raise UnknownModuleType."""
with pytest.raises(UnknownModuleType):
resolve_module({"payload": {}, "instance": {}}, ctx=None)
def test_resolve_non_string_type_raises():
"""Snapshots with a non-string type value are malformed."""
with pytest.raises(UnknownModuleType):
resolve_module({"type": 42, "payload": {}, "instance": {}}, ctx=None)