Skip to content

Commit 6f80f8d

Browse files
committed
test: move asserts inside async-with for 3.11 coverage instrumentation
coverage.py on Python 3.11 doesn't record statements after an 'async with running_pair(...)' exit when there's a nested 'with anyio.fail_after()' inside. Same workaround as 0a8f0f4 in PR2 — move the asserts inside the async-with block.
1 parent a3b896f commit 6f80f8d

File tree

3 files changed

+55
-55
lines changed

3 files changed

+55
-55
lines changed

tests/server/test_server_context.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ async def server_on_request(dctx: DCtx, method: str, params: Mapping[str, Any] |
4444
conn.__init__(server, has_standalone_channel=True)
4545
with anyio.fail_after(5):
4646
await client.send_raw_request("t", None)
47-
ctx = captured[0]
48-
assert ctx.lifespan.name == "app"
49-
assert ctx.connection is conn
50-
assert ctx.transport.kind == "direct"
51-
assert ctx.can_send_request is True
47+
ctx = captured[0]
48+
assert ctx.lifespan.name == "app"
49+
assert ctx.connection is conn
50+
assert ctx.transport.kind == "direct"
51+
assert ctx.can_send_request is True
5252

5353

5454
@pytest.mark.anyio
@@ -80,8 +80,8 @@ async def server_on_request(dctx: DCtx, method: str, params: Mapping[str, Any] |
8080
) as (client, *_):
8181
with anyio.fail_after(5):
8282
await client.send_raw_request("tools/call", None)
83-
assert crec.requests[0][0] == "sampling/createMessage"
84-
assert isinstance(results[0], CreateMessageResult)
83+
assert crec.requests[0][0] == "sampling/createMessage"
84+
assert isinstance(results[0], CreateMessageResult)
8585

8686

8787
@pytest.mark.anyio
@@ -104,7 +104,7 @@ async def server_on_request(dctx: DCtx, method: str, params: Mapping[str, Any] |
104104
):
105105
with anyio.fail_after(5):
106106
await client.send_raw_request("t", None)
107-
assert isinstance(results[0], ListRootsResult)
107+
assert isinstance(results[0], ListRootsResult)
108108

109109

110110
@pytest.mark.anyio
@@ -126,9 +126,9 @@ async def server_on_request(dctx: DCtx, method: str, params: Mapping[str, Any] |
126126
with anyio.fail_after(5):
127127
await client.send_raw_request("t", None)
128128
await crec.notified.wait()
129-
method, params = crec.notifications[0]
130-
assert method == "notifications/message"
131-
assert params is not None and params["level"] == "debug" and params["data"] == "hello"
129+
method, params = crec.notifications[0]
130+
assert method == "notifications/message"
131+
assert params is not None and params["level"] == "debug" and params["data"] == "hello"
132132

133133

134134
@pytest.mark.anyio
@@ -150,7 +150,7 @@ async def server_on_request(dctx: DCtx, method: str, params: Mapping[str, Any] |
150150
with anyio.fail_after(5):
151151
await client.send_raw_request("t", None)
152152
await crec.notified.wait()
153-
_, params = crec.notifications[0]
154-
assert params is not None
155-
assert params["logger"] == "my.log"
156-
assert params["_meta"] == {"traceId": "t"}
153+
_, params = crec.notifications[0]
154+
assert params is not None
155+
assert params["logger"] == "my.log"
156+
assert params["_meta"] == {"traceId": "t"}

tests/shared/test_context.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ async def server_on_request(ctx: DCtx, method: str, params: Mapping[str, Any] |
3434
async with running_pair(direct_pair, server_on_request=server_on_request) as (client, *_):
3535
with anyio.fail_after(5):
3636
await client.send_raw_request("t", None)
37-
bctx = captured[0]
38-
assert bctx.transport.kind == "direct"
39-
assert isinstance(bctx.cancel_requested, anyio.Event)
40-
assert bctx.can_send_request is True
41-
assert bctx.meta is None
37+
bctx = captured[0]
38+
assert bctx.transport.kind == "direct"
39+
assert isinstance(bctx.cancel_requested, anyio.Event)
40+
assert bctx.can_send_request is True
41+
assert bctx.meta is None
4242

4343

4444
@pytest.mark.anyio
@@ -61,9 +61,9 @@ async def server_on_request(ctx: DCtx, method: str, params: Mapping[str, Any] |
6161
with anyio.fail_after(5):
6262
result = await client.send_raw_request("tools/call", None)
6363
await crec.notified.wait()
64-
assert crec.requests == [("sampling/createMessage", {"x": 1})]
65-
assert crec.notifications == [("notifications/message", {"level": "info"})]
66-
assert result["sample"] == {"echoed": "sampling/createMessage", "params": {"x": 1}}
64+
assert crec.requests == [("sampling/createMessage", {"x": 1})]
65+
assert crec.notifications == [("notifications/message", {"level": "info"})]
66+
assert result["sample"] == {"echoed": "sampling/createMessage", "params": {"x": 1}}
6767

6868

6969
@pytest.mark.anyio
@@ -81,7 +81,7 @@ async def server_on_request(ctx: DCtx, method: str, params: Mapping[str, Any] |
8181
async with running_pair(direct_pair, server_on_request=server_on_request) as (client, *_):
8282
with anyio.fail_after(5):
8383
await client.send_raw_request("t", None, {"on_progress": on_progress})
84-
assert received == [(0.5, 1.0, "halfway")]
84+
assert received == [(0.5, 1.0, "halfway")]
8585

8686

8787
@pytest.mark.anyio
@@ -100,7 +100,7 @@ async def server_on_request(ctx: DCtx, method: str, params: Mapping[str, Any] |
100100
) as (client, *_):
101101
with anyio.fail_after(5):
102102
await client.send_raw_request("t", None)
103-
assert crec.requests == [("ping", None)]
103+
assert crec.requests == [("ping", None)]
104104

105105

106106
@pytest.mark.anyio

tests/shared/test_peer.py

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ async def test_peer_sample_sends_create_message_and_returns_typed_result():
5050
[SamplingMessage(role="user", content=TextContent(type="text", text="hello"))],
5151
max_tokens=10,
5252
)
53-
method, params = rec.seen[0]
54-
assert method == "sampling/createMessage"
55-
assert params is not None and params["maxTokens"] == 10
56-
assert isinstance(result, CreateMessageResult)
57-
assert result.model == "m"
53+
method, params = rec.seen[0]
54+
assert method == "sampling/createMessage"
55+
assert params is not None and params["maxTokens"] == 10
56+
assert isinstance(result, CreateMessageResult)
57+
assert result.model == "m"
5858

5959

6060
@pytest.mark.anyio
@@ -68,10 +68,10 @@ async def test_peer_sample_with_tools_returns_with_tools_result():
6868
max_tokens=5,
6969
tools=[Tool(name="t", input_schema={"type": "object"})],
7070
)
71-
method, params = rec.seen[0]
72-
assert method == "sampling/createMessage"
73-
assert params is not None and params["tools"][0]["name"] == "t"
74-
assert isinstance(result, CreateMessageResultWithTools)
71+
method, params = rec.seen[0]
72+
assert method == "sampling/createMessage"
73+
assert params is not None and params["tools"][0]["name"] == "t"
74+
assert isinstance(result, CreateMessageResultWithTools)
7575

7676

7777
@pytest.mark.anyio
@@ -81,11 +81,11 @@ async def test_peer_elicit_form_sends_elicitation_create_with_form_params():
8181
peer = Peer(client)
8282
with anyio.fail_after(5):
8383
result = await peer.elicit_form("Your name?", requested_schema={"type": "object", "properties": {}})
84-
method, params = rec.seen[0]
85-
assert method == "elicitation/create"
86-
assert params is not None and params["mode"] == "form"
87-
assert params["message"] == "Your name?"
88-
assert isinstance(result, ElicitResult)
84+
method, params = rec.seen[0]
85+
assert method == "elicitation/create"
86+
assert params is not None and params["mode"] == "form"
87+
assert params["message"] == "Your name?"
88+
assert isinstance(result, ElicitResult)
8989

9090

9191
@pytest.mark.anyio
@@ -95,11 +95,11 @@ async def test_peer_elicit_url_sends_elicitation_create_with_url_params():
9595
peer = Peer(client)
9696
with anyio.fail_after(5):
9797
result = await peer.elicit_url("Auth needed", url="https://example.com/auth", elicitation_id="e1")
98-
method, params = rec.seen[0]
99-
assert method == "elicitation/create"
100-
assert params is not None and params["mode"] == "url"
101-
assert params["url"] == "https://example.com/auth"
102-
assert isinstance(result, ElicitResult)
98+
method, params = rec.seen[0]
99+
assert method == "elicitation/create"
100+
assert params is not None and params["mode"] == "url"
101+
assert params["url"] == "https://example.com/auth"
102+
assert isinstance(result, ElicitResult)
103103

104104

105105
@pytest.mark.anyio
@@ -109,11 +109,11 @@ async def test_peer_list_roots_sends_roots_list_and_returns_typed_result():
109109
peer = Peer(client)
110110
with anyio.fail_after(5):
111111
result = await peer.list_roots()
112-
method, _ = rec.seen[0]
113-
assert method == "roots/list"
114-
assert isinstance(result, ListRootsResult)
115-
assert len(result.roots) == 1
116-
assert str(result.roots[0].uri) == "file:///workspace"
112+
method, _ = rec.seen[0]
113+
assert method == "roots/list"
114+
assert isinstance(result, ListRootsResult)
115+
assert len(result.roots) == 1
116+
assert str(result.roots[0].uri) == "file:///workspace"
117117

118118

119119
@pytest.mark.anyio
@@ -123,9 +123,9 @@ async def test_peer_list_roots_with_meta_sends_meta_in_params():
123123
peer = Peer(client)
124124
with anyio.fail_after(5):
125125
await peer.list_roots(meta={"traceId": "t1"})
126-
method, params = rec.seen[0]
127-
assert method == "roots/list"
128-
assert params == {"_meta": {"traceId": "t1"}}
126+
method, params = rec.seen[0]
127+
assert method == "roots/list"
128+
assert params == {"_meta": {"traceId": "t1"}}
129129

130130

131131
def test_dump_params_merges_meta_over_model_meta():
@@ -159,6 +159,6 @@ async def test_peer_ping_sends_ping_and_returns_none():
159159
peer = Peer(client)
160160
with anyio.fail_after(5):
161161
result = await peer.ping()
162-
method, _ = rec.seen[0]
163-
assert method == "ping"
164-
assert result is None
162+
method, _ = rec.seen[0]
163+
assert method == "ping"
164+
assert result is None

0 commit comments

Comments
 (0)