Skip to content

Commit aa7869a

Browse files
committed
add type hints
1 parent 3fa26a5 commit aa7869a

File tree

1 file changed

+12
-18
lines changed

1 file changed

+12
-18
lines changed

tests/shared/test_sse.py

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
import socket
33
import time
44
import anyio
5+
from starlette.requests import Request
56
import uvicorn
67
import pytest
78
from pydantic import AnyUrl
89
import httpx
9-
from typing import AsyncGenerator
10+
from typing import AsyncGenerator, Generator
1011
from starlette.applications import Starlette
1112
from starlette.routing import Mount, Route
1213

@@ -56,7 +57,7 @@ async def handle_read_resource(uri: AnyUrl) -> str | bytes:
5657
)
5758

5859
@self.list_tools()
59-
async def handle_list_tools():
60+
async def handle_list_tools() -> list[Tool]:
6061
return [
6162
Tool(
6263
name="test_tool",
@@ -66,7 +67,7 @@ async def handle_list_tools():
6667
]
6768

6869
@self.call_tool()
69-
async def handle_call_tool(name: str, args: dict):
70+
async def handle_call_tool(name: str, args: dict) -> list[TextContent]:
7071
return [TextContent(type="text", text=f"Called {name}")]
7172

7273

@@ -76,7 +77,7 @@ def make_server_app() -> Starlette:
7677
sse = SseServerTransport("/messages/")
7778
server = TestServer()
7879

79-
async def handle_sse(request):
80+
async def handle_sse(request: Request) -> None:
8081
async with sse.connect_sse(
8182
request.scope, request.receive, request._send
8283
) as streams:
@@ -94,14 +95,7 @@ async def handle_sse(request):
9495
return app
9596

9697

97-
@pytest.fixture(autouse=True)
98-
def space_around_test():
99-
time.sleep(0.1)
100-
yield
101-
time.sleep(0.1)
102-
103-
104-
def run_server(server_port: int):
98+
def run_server(server_port: int) -> None:
10599
app = make_server_app()
106100
server = uvicorn.Server(
107101
config=uvicorn.Config(
@@ -118,7 +112,7 @@ def run_server(server_port: int):
118112

119113

120114
@pytest.fixture()
121-
def server(server_port: int):
115+
def server(server_port: int) -> Generator[None, None, None]:
122116
proc = multiprocessing.Process(
123117
target=run_server, kwargs={"server_port": server_port}, daemon=True
124118
)
@@ -161,11 +155,11 @@ async def http_client(server, server_url) -> AsyncGenerator[httpx.AsyncClient, N
161155

162156
# Tests
163157
@pytest.mark.anyio
164-
async def test_raw_sse_connection(http_client: httpx.AsyncClient):
158+
async def test_raw_sse_connection(http_client: httpx.AsyncClient) -> None:
165159
"""Test the SSE connection establishment simply with an HTTP client."""
166160
async with anyio.create_task_group() as tg:
167161

168-
async def connection_test():
162+
async def connection_test() -> None:
169163
async with http_client.stream("GET", "/sse") as response:
170164
assert response.status_code == 200
171165
assert (
@@ -189,7 +183,7 @@ async def connection_test():
189183

190184

191185
@pytest.mark.anyio
192-
async def test_sse_client_basic_connection(server, server_url):
186+
async def test_sse_client_basic_connection(server: None, server_url: str) -> None:
193187
async with sse_client(server_url + "/sse") as streams:
194188
async with ClientSession(*streams) as session:
195189
# Test initialization
@@ -215,7 +209,7 @@ async def initialized_sse_client_session(
215209
@pytest.mark.anyio
216210
async def test_sse_client_happy_request_and_response(
217211
initialized_sse_client_session: ClientSession,
218-
):
212+
) -> None:
219213
session = initialized_sse_client_session
220214
response = await session.read_resource(uri=AnyUrl("foobar://should-work"))
221215
assert len(response.contents) == 1
@@ -226,7 +220,7 @@ async def test_sse_client_happy_request_and_response(
226220
@pytest.mark.anyio
227221
async def test_sse_client_exception_handling(
228222
initialized_sse_client_session: ClientSession,
229-
):
223+
) -> None:
230224
session = initialized_sse_client_session
231225
with pytest.raises(McpError, match="OOPS! no resource with that URI was found"):
232226
await session.read_resource(uri=AnyUrl("xxx://will-not-work"))

0 commit comments

Comments
 (0)