Skip to content

Commit c158ef4

Browse files
TechNiickScirlat DanutKludex
authored
Added type annotations to test_error.py (#2462)
* added type annotations to test_error.py * Apply suggestions from code review --------- Co-authored-by: Scirlat Danut <[email protected]> Co-authored-by: Marcelo Trylesinski <[email protected]>
1 parent 93e74a4 commit c158ef4

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

tests/middleware/test_errors.py

+24-15
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
1+
from typing import Any, Callable
2+
13
import pytest
24

35
from starlette.applications import Starlette
46
from starlette.background import BackgroundTask
57
from starlette.middleware.errors import ServerErrorMiddleware
8+
from starlette.requests import Request
69
from starlette.responses import JSONResponse, Response
710
from starlette.routing import Route
11+
from starlette.testclient import TestClient
12+
from starlette.types import Receive, Scope, Send
13+
14+
TestClientFactory = Callable[..., TestClient]
815

916

10-
def test_handler(test_client_factory):
11-
async def app(scope, receive, send):
17+
def test_handler(
18+
test_client_factory: TestClientFactory,
19+
) -> None:
20+
async def app(scope: Scope, receive: Receive, send: Send) -> None:
1221
raise RuntimeError("Something went wrong")
1322

14-
def error_500(request, exc):
23+
def error_500(request: Request, exc: Exception) -> JSONResponse:
1524
return JSONResponse({"detail": "Server Error"}, status_code=500)
1625

1726
app = ServerErrorMiddleware(app, handler=error_500)
@@ -21,8 +30,8 @@ def error_500(request, exc):
2130
assert response.json() == {"detail": "Server Error"}
2231

2332

24-
def test_debug_text(test_client_factory):
25-
async def app(scope, receive, send):
33+
def test_debug_text(test_client_factory: TestClientFactory) -> None:
34+
async def app(scope: Scope, receive: Receive, send: Send) -> None:
2635
raise RuntimeError("Something went wrong")
2736

2837
app = ServerErrorMiddleware(app, debug=True)
@@ -33,8 +42,8 @@ async def app(scope, receive, send):
3342
assert "RuntimeError: Something went wrong" in response.text
3443

3544

36-
def test_debug_html(test_client_factory):
37-
async def app(scope, receive, send):
45+
def test_debug_html(test_client_factory: TestClientFactory) -> None:
46+
async def app(scope: Scope, receive: Receive, send: Send) -> None:
3847
raise RuntimeError("Something went wrong")
3948

4049
app = ServerErrorMiddleware(app, debug=True)
@@ -45,8 +54,8 @@ async def app(scope, receive, send):
4554
assert "RuntimeError" in response.text
4655

4756

48-
def test_debug_after_response_sent(test_client_factory):
49-
async def app(scope, receive, send):
57+
def test_debug_after_response_sent(test_client_factory: TestClientFactory) -> None:
58+
async def app(scope: Scope, receive: Receive, send: Send) -> None:
5059
response = Response(b"", status_code=204)
5160
await response(scope, receive, send)
5261
raise RuntimeError("Something went wrong")
@@ -57,12 +66,12 @@ async def app(scope, receive, send):
5766
client.get("/")
5867

5968

60-
def test_debug_not_http(test_client_factory):
69+
def test_debug_not_http(test_client_factory: TestClientFactory) -> None:
6170
"""
6271
DebugMiddleware should just pass through any non-http messages as-is.
6372
"""
6473

65-
async def app(scope, receive, send):
74+
async def app(scope: Scope, receive: Receive, send: Send) -> None:
6675
raise RuntimeError("Something went wrong")
6776

6877
app = ServerErrorMiddleware(app)
@@ -73,17 +82,17 @@ async def app(scope, receive, send):
7382
pass # pragma: nocover
7483

7584

76-
def test_background_task(test_client_factory):
85+
def test_background_task(test_client_factory: TestClientFactory) -> None:
7786
accessed_error_handler = False
7887

79-
def error_handler(request, exc):
88+
def error_handler(request: Request, exc: Exception) -> Any:
8089
nonlocal accessed_error_handler
8190
accessed_error_handler = True
8291

83-
def raise_exception():
92+
def raise_exception() -> None:
8493
raise Exception("Something went wrong")
8594

86-
async def endpoint(request):
95+
async def endpoint(request: Request) -> Response:
8796
task = BackgroundTask(raise_exception)
8897
return Response(status_code=204, background=task)
8998

0 commit comments

Comments
 (0)