Skip to content

Commit dc154d5

Browse files
committed
fix(tests): add no-branch pragma for nested async with ClientSession
On Python 3.11+, coverage.py fails to track the ->exit arc from the innermost async with body when it yields inside 3+ nested async with blocks (the async generator frame unwinds through __aexit__ chains differently than 3.10). The codebase already uses # pragma: no branch for this pattern on existing ClientSession fixtures; apply it to the new ASGI-backed fixtures added in the previous commit. Fixes the 5 ->exit branch misses reported on CI 3.11-3.14: test_http_unicode.py:124->exit test_streamable_http.py:979->exit, 1463->exit, 1523->exit, 2130->exit
1 parent b068384 commit dc154d5

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

tests/client/test_http_unicode.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,9 @@ async def unicode_session() -> AsyncGenerator[ClientSession, None]:
121121
transport = httpx.ASGITransport(app=app)
122122
async with httpx.AsyncClient(transport=transport, follow_redirects=True) as http_client:
123123
async with streamable_http_client("http://testserver/mcp", http_client=http_client) as (rs, ws):
124-
async with ClientSession(rs, ws) as session:
124+
async with ClientSession(rs, ws) as session: # pragma: no branch
125+
# ^ coverage.py misses the ->exit arc on 3.11+ when yield is
126+
# nested inside multiple async with blocks
125127
await session.initialize()
126128
yield session
127129

tests/shared/test_streamable_http.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -976,7 +976,7 @@ async def initialized_client_session() -> AsyncGenerator[ClientSession, None]:
976976
async with asgi_client(create_app()) as http_client:
977977
# Use localhost so create_app's TransportSecuritySettings allowlist accepts it
978978
async with streamable_http_client("http://localhost/mcp", http_client=http_client) as (rs, ws):
979-
async with ClientSession(rs, ws) as session:
979+
async with ClientSession(rs, ws) as session: # pragma: no branch
980980
await session.initialize()
981981
yield session
982982

@@ -1460,7 +1460,7 @@ async def context_aware_session(
14601460
"""Initialized ClientSession against an in-process context-aware server (ASGI)."""
14611461
async with asgi_client(create_context_aware_app(), **client_kwargs) as http_client:
14621462
async with streamable_http_client("http://testserver/mcp", http_client=http_client) as (rs, ws):
1463-
async with ClientSession(rs, ws) as session:
1463+
async with ClientSession(rs, ws) as session: # pragma: no branch
14641464
await session.initialize()
14651465
yield session
14661466

@@ -1520,7 +1520,7 @@ async def test_client_includes_protocol_version_header_after_init():
15201520
"""Test that client includes mcp-protocol-version header after initialization."""
15211521
async with asgi_client(create_context_aware_app()) as http_client:
15221522
async with streamable_http_client("http://testserver/mcp", http_client=http_client) as (rs, ws):
1523-
async with ClientSession(rs, ws) as session:
1523+
async with ClientSession(rs, ws) as session: # pragma: no branch
15241524
init_result = await session.initialize()
15251525
negotiated_version = init_result.protocol_version
15261526

@@ -2127,7 +2127,7 @@ async def test_streamable_http_client_mcp_headers_override_defaults() -> None:
21272127
async with asgi_client(create_context_aware_app()) as client:
21282128
assert client.headers.get("accept") == "*/*"
21292129
async with streamable_http_client("http://testserver/mcp", http_client=client) as (rs, ws):
2130-
async with ClientSession(rs, ws) as session:
2130+
async with ClientSession(rs, ws) as session: # pragma: no branch
21312131
await session.initialize()
21322132
headers_data = await _echo_headers(session)
21332133
assert "application/json" in headers_data["accept"]

0 commit comments

Comments
 (0)