Skip to content

Commit 97ee03f

Browse files
committed
Reduced caplog.at_level layers from 4 to 3.
1 parent 445e6e3 commit 97ee03f

1 file changed

Lines changed: 18 additions & 9 deletions

File tree

tests/server/mcpserver/test_server.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1545,15 +1545,20 @@ async def test_get_prompt_missing_args_logs_warning_without_traceback(
15451545
@mcp.prompt()
15461546
def prompt_fn(name: str) -> str: ... # pragma: no branch.
15471547

1548-
with caplog.at_level(logging.WARNING, logger="mcp.server.mcpserver.server"):
1549-
async with Client(mcp, mode="legacy") as client:
1550-
with pytest.raises(MCPError, match="Missing required arguments"):
1551-
await client.get_prompt("prompt_fn")
1548+
# In Python 3.14, coverage.py undercounts a branch when `caplog.at_level`
1549+
# wraps `async with Client(...): with pytest.raises(...): await ...` as
1550+
# a 4th nesting level around a single `await` statement (3 levels of
1551+
# nesting is OK; 4 is not). So `caplog.set_level` avoids extra `with` layer.
1552+
caplog.set_level(logging.WARNING, logger="mcp.server.mcpserver.server")
1553+
async with Client(mcp, mode="legacy") as client:
1554+
with pytest.raises(MCPError, match="Missing required arguments"):
1555+
await client.get_prompt("prompt_fn")
15521556

15531557
server_records = [r for r in caplog.records if r.name == "mcp.server.mcpserver.server"]
15541558
assert len(server_records) == 1
15551559

1556-
# ValueError should have warning log without exc_info.
1560+
# Missing-argument PromptValidationError, which is a ValueError subclass,
1561+
# logs as a plain warning, with no exc_info/traceback at all.
15571562
assert server_records[0].levelno == logging.WARNING
15581563
assert not server_records[0].exc_info
15591564

@@ -1568,10 +1573,14 @@ async def test_get_prompt_unexpected_error_still_logs_traceback(self, caplog: py
15681573
def prompt_fn() -> str:
15691574
raise KeyError("boom")
15701575

1571-
with caplog.at_level(logging.WARNING, logger="mcp.server.mcpserver.server"):
1572-
async with Client(mcp, mode="legacy") as client:
1573-
with pytest.raises(MCPError):
1574-
await client.get_prompt("prompt_fn")
1576+
# In Python 3.14, coverage.py undercounts a branch when `caplog.at_level`
1577+
# wraps `async with Client(...): with pytest.raises(...): await ...` as
1578+
# a 4th nesting level around a single `await` statement (3 levels of
1579+
# nesting is OK; 4 is not). So `caplog.set_level` avoids extra `with` layer.
1580+
caplog.set_level(logging.WARNING, logger="mcp.server.mcpserver.server")
1581+
async with Client(mcp, mode="legacy") as client:
1582+
with pytest.raises(MCPError):
1583+
await client.get_prompt("prompt_fn")
15751584

15761585
server_records = [r for r in caplog.records if r.name == "mcp.server.mcpserver.server"]
15771586
assert len(server_records) == 1

0 commit comments

Comments
 (0)