Skip to content

Commit 5e3c5c5

Browse files
aditik0303claude
andcommitted
test(agent-framework): make test_middleware mypy-clean (fix CI lint)
Same pre-existing CI-lint failure (mypy runs over tests): - FakeEvaluator evaluate_* -> (self, *args, **kwargs) -> Any; bare dict -> dict[str, Any]. - typed the duck-typed contexts as so passing a SimpleNamespace to process()'s ChatContext/FunctionInvocationContext param type-checks (one inline context keeps an arg-type ignore). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent be37e85 commit 5e3c5c5

1 file changed

Lines changed: 24 additions & 17 deletions

File tree

packages/uipath-agent-framework/tests/governance/test_middleware.py

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,29 +36,29 @@ class FakeEvaluator:
3636

3737
def __init__(self, block_on: str | None = None) -> None:
3838
self.block_on = block_on
39-
self.calls: List[tuple[str, dict]] = []
39+
self.calls: List[tuple[str, dict[str, Any]]] = []
4040

4141
def _record(self, hook: str, **kwargs: Any) -> None:
4242
self.calls.append((hook, kwargs))
4343
if self.block_on == hook:
4444
raise GovernanceBlockException("blocked") # type: ignore[call-arg]
4545

46-
def evaluate_before_agent(self, **kwargs: Any) -> None:
46+
def evaluate_before_agent(self, *args: Any, **kwargs: Any) -> Any:
4747
self._record("before_agent", **kwargs)
4848

49-
def evaluate_after_agent(self, **kwargs: Any) -> None:
49+
def evaluate_after_agent(self, *args: Any, **kwargs: Any) -> Any:
5050
self._record("after_agent", **kwargs)
5151

52-
def evaluate_before_model(self, **kwargs: Any) -> None:
52+
def evaluate_before_model(self, *args: Any, **kwargs: Any) -> Any:
5353
self._record("before_model", **kwargs)
5454

55-
def evaluate_after_model(self, **kwargs: Any) -> None:
55+
def evaluate_after_model(self, *args: Any, **kwargs: Any) -> Any:
5656
self._record("after_model", **kwargs)
5757

58-
def evaluate_tool_call(self, **kwargs: Any) -> None:
58+
def evaluate_tool_call(self, *args: Any, **kwargs: Any) -> Any:
5959
self._record("tool_call", **kwargs)
6060

61-
def evaluate_after_tool(self, **kwargs: Any) -> None:
61+
def evaluate_after_tool(self, *args: Any, **kwargs: Any) -> Any:
6262
self._record("after_tool", **kwargs)
6363

6464

@@ -238,7 +238,7 @@ async def test_chat_middleware_brackets_call_with_before_and_after():
238238
async def call_next() -> None:
239239
order.append("model_call")
240240

241-
context = SimpleNamespace(
241+
context: Any = SimpleNamespace(
242242
messages=[_msg("old"), _msg("the question")],
243243
result=SimpleNamespace(text="the answer"),
244244
)
@@ -255,7 +255,9 @@ async def test_chat_middleware_caps_text():
255255
ev = FakeEvaluator()
256256
mw = GovernanceChatMiddleware(_make_callbacks(ev))
257257
huge = "x" * (_BEFORE_MODEL_TEXT_CAP + 5000)
258-
context = SimpleNamespace(messages=[_msg(huge)], result=SimpleNamespace(text=""))
258+
context: Any = SimpleNamespace(
259+
messages=[_msg(huge)], result=SimpleNamespace(text="")
260+
)
259261
await mw.process(context, _noop_next)
260262
assert len(ev.calls[0][1]["model_input"]) <= _BEFORE_MODEL_TEXT_CAP
261263

@@ -269,7 +271,9 @@ async def test_after_model_runs_even_when_model_call_raises():
269271
async def boom_next() -> None:
270272
raise RuntimeError("model exploded")
271273

272-
context = SimpleNamespace(messages=[_msg("hi")], result=SimpleNamespace(text=""))
274+
context: Any = SimpleNamespace(
275+
messages=[_msg("hi")], result=SimpleNamespace(text="")
276+
)
273277
with pytest.raises(RuntimeError, match="model exploded"):
274278
await mw.process(context, boom_next)
275279
assert [h for h, _ in ev.calls] == ["before_model", "after_model"]
@@ -280,7 +284,7 @@ async def test_streaming_governs_finalized_response_via_result_hook():
280284
AFTER_MODEL runs from a stream_result_hook on the finalized ChatResponse."""
281285
ev = FakeEvaluator()
282286
mw = GovernanceChatMiddleware(_make_callbacks(ev))
283-
context = SimpleNamespace(
287+
context: Any = SimpleNamespace(
284288
messages=[_msg("the question")],
285289
stream=True,
286290
stream_result_hooks=[],
@@ -312,7 +316,7 @@ async def test_function_middleware_passes_name_args_and_result():
312316
async def call_next() -> None:
313317
order.append("tool_call")
314318

315-
context = SimpleNamespace(
319+
context: Any = SimpleNamespace(
316320
function=FakeTool("transfer"),
317321
arguments={"amount": 50},
318322
result={"status": "ok"},
@@ -332,7 +336,7 @@ async def test_function_middleware_coerces_pydantic_args():
332336
ev = FakeEvaluator()
333337
mw = GovernanceFunctionMiddleware(_make_callbacks(ev))
334338
args = SimpleNamespace(model_dump=lambda: {"x": 1})
335-
context = SimpleNamespace(function=FakeTool("t"), arguments=args, result=None)
339+
context: Any = SimpleNamespace(function=FakeTool("t"), arguments=args, result=None)
336340
await mw.process(context, _noop_next)
337341
assert ev.calls[0][1]["tool_args"] == {"x": 1}
338342
assert ev.calls[1][1]["tool_result"] == "" # None result → ""
@@ -345,7 +349,7 @@ async def test_after_tool_runs_even_when_tool_call_raises():
345349
async def boom_next() -> None:
346350
raise RuntimeError("tool exploded")
347351

348-
context = SimpleNamespace(function=FakeTool("t"), arguments={}, result=None)
352+
context: Any = SimpleNamespace(function=FakeTool("t"), arguments={}, result=None)
349353
with pytest.raises(RuntimeError, match="tool exploded"):
350354
await mw.process(context, boom_next)
351355
assert [h for h, _ in ev.calls] == ["tool_call", "after_tool"]
@@ -374,7 +378,7 @@ async def test_block_in_before_model_aborts_before_call_next():
374378
async def call_next() -> None:
375379
called["next"] = True
376380

377-
context = SimpleNamespace(messages=[_msg("hi")], result=None)
381+
context: Any = SimpleNamespace(messages=[_msg("hi")], result=None)
378382
with pytest.raises(GovernanceBlockException):
379383
await mw.process(context, call_next)
380384
assert called["next"] is False # tool/model never ran
@@ -388,7 +392,7 @@ async def test_block_in_before_tool_aborts_before_call_next():
388392
async def call_next() -> None:
389393
called["next"] = True
390394

391-
context = SimpleNamespace(function=FakeTool("t"), arguments={}, result=None)
395+
context: Any = SimpleNamespace(function=FakeTool("t"), arguments={}, result=None)
392396
with pytest.raises(GovernanceBlockException):
393397
await mw.process(context, call_next)
394398
assert called["next"] is False
@@ -402,5 +406,8 @@ def evaluate_before_model(self, **_: Any) -> None:
402406
cb = GovernanceCallbacks(evaluator=Boom(), agent_name="a", session_id="s") # type: ignore[arg-type]
403407
mw = GovernanceChatMiddleware(cb)
404408
with caplog.at_level(logging.WARNING):
405-
await mw.process(SimpleNamespace(messages=[_msg("x")], result=None), _noop_next)
409+
await mw.process(
410+
SimpleNamespace(messages=[_msg("x")], result=None), # type: ignore[arg-type]
411+
_noop_next,
412+
)
406413
assert any("governance check failed" in r.message for r in caplog.records)

0 commit comments

Comments
 (0)