55import asyncio
66import json
77from collections .abc import AsyncGenerator , Awaitable , Mapping , MutableSequence
8- from contextlib import nullcontext
8+ from contextlib import AbstractContextManager , nullcontext
99from typing import Any , cast
1010
1111import 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 )
0 commit comments