Skip to content

Commit 7189479

Browse files
jpalvarezlCopilot
andcommitted
Python: Clarify AG-UI test types
Type conditional exception contexts explicitly and use separate variables for chat and HTTP responses. Preserve the existing test cases and assertions while satisfying Mypy and Zuban. Co-authored-by: Copilot <[email protected]>
1 parent 2eb7dd9 commit 7189479

2 files changed

Lines changed: 15 additions & 11 deletions

File tree

‎python/packages/ag-ui/tests/ag_ui/test_ag_ui_client.py‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import asyncio
66
import json
77
from collections.abc import AsyncGenerator, Awaitable, Mapping, MutableSequence
8-
from contextlib import nullcontext
8+
from contextlib import AbstractContextManager, nullcontext
99
from typing import Any, cast
1010

1111
import httpx
@@ -114,7 +114,7 @@ async def handle_async_request(
114114
http_client = client.http_service.http_client
115115
for index, thread_id in enumerate(("thread_a", second_thread_id)):
116116
messages = [Message(role="user", contents=["Hello"])]
117-
expected_error = (
117+
expected_error: AbstractContextManager[object] = (
118118
pytest.raises(httpx.HTTPStatusError, match="503")
119119
if index == 0 and first_status == 503
120120
else nullcontext()
@@ -220,8 +220,11 @@ async def test_context_manager_closes_owned_http_client(self, raise_in_context:
220220
"""Owned HTTP clients close on both normal and exceptional context exits."""
221221
client = StubAGUIChatClient(endpoint="https://agui.example.test/", timeout=17.0)
222222
http_client = client.http_service.http_client
223+
expected_error: AbstractContextManager[object] = (
224+
pytest.raises(RuntimeError, match="context failed") if raise_in_context else nullcontext()
225+
)
223226
try:
224-
with pytest.raises(RuntimeError, match="context failed") if raise_in_context else nullcontext():
227+
with expected_error:
225228
async with client:
226229
assert not http_client.is_closed
227230
assert http_client.timeout == httpx.Timeout(17.0)
@@ -261,14 +264,17 @@ async def handler(request: httpx.Request) -> httpx.Response:
261264
timeout=17.0,
262265
) as http_client:
263266
client = StubAGUIChatClient(endpoint="https://agui.example.test/", http_client=http_client)
264-
with pytest.raises(RuntimeError, match="context failed") if raise_in_context else nullcontext():
267+
expected_error: AbstractContextManager[object] = (
268+
pytest.raises(RuntimeError, match="context failed") if raise_in_context else nullcontext()
269+
)
270+
with expected_error:
265271
async with client:
266272
assert client.http_service.http_client is http_client
267-
response = await client.get_response(
273+
chat_response = await client.get_response(
268274
[Message(role="user", contents=["Hello"])],
269275
options={"metadata": {"thread_id": "thread_1"}},
270276
)
271-
assert response.text == "ok"
277+
assert chat_response.text == "ok"
272278
if raise_in_context:
273279
raise RuntimeError("context failed")
274280

@@ -278,8 +284,8 @@ async def handler(request: httpx.Request) -> httpx.Response:
278284
assert http_client.cookies["configured"] == "caller-owned"
279285
assert http_client.cookies["response"] == "session-only"
280286
assert http_client.timeout == httpx.Timeout(17.0)
281-
response = await http_client.get("https://agui.example.test/health")
282-
assert response.status_code == 200
287+
http_response = await http_client.get("https://agui.example.test/health")
288+
assert http_response.status_code == 200
283289

284290
assert [request.method for request in requests] == ["POST", "GET"]
285291
assert all(request.headers["authorization"] == "Bearer test-token" for request in requests)

‎python/packages/ag-ui/tests/ag_ui/test_workflow_run.py‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -481,9 +481,7 @@ async def test_workflow_run_approval_pause_closes_open_real_tool_call() -> None:
481481
"""
482482

483483
@executor(id="approval_with_streamed_tool")
484-
async def approval_with_streamed_tool(
485-
message: Any, ctx: WorkflowContext[Any, AgentResponseUpdate]
486-
) -> None:
484+
async def approval_with_streamed_tool(message: Any, ctx: WorkflowContext[Any, AgentResponseUpdate]) -> None:
487485
del message
488486
function_call = Content.from_function_call(
489487
call_id="weather-call",

0 commit comments

Comments
 (0)