Skip to content

Commit 844f084

Browse files
committed
Use MiddewareType for middlewares
1 parent d755851 commit 844f084

File tree

5 files changed

+14
-6
lines changed

5 files changed

+14
-6
lines changed

starlette/applications.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from starlette.requests import Request
1010
from starlette.responses import Response
1111
from starlette.routing import BaseRoute, Router
12-
from starlette.types import ASGIApp, Receive, Scope, Send
12+
from starlette.types import ASGIApp, MiddewareType, Receive, Scope, Send
1313

1414

1515
class Starlette:
@@ -138,7 +138,7 @@ def host(
138138
self.router.host(host, app=app, name=name)
139139

140140
def add_middleware(
141-
self, middleware_class: type, **options: typing.Any
141+
self, middleware_class: typing.Type[MiddewareType], **options: typing.Any
142142
) -> None: # pragma: no cover
143143
self.user_middleware.insert(0, Middleware(middleware_class, **options))
144144
self.middleware_stack = self.build_middleware_stack()

starlette/middleware/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import typing
22

3+
from starlette.types import MiddewareType
4+
35

46
class Middleware:
5-
def __init__(self, cls: type, **options: typing.Any) -> None:
7+
def __init__(self, cls: typing.Type[MiddewareType], **options: typing.Any) -> None:
68
self.cls = cls
79
self.options = options
810

starlette/types.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@
77
Send = typing.Callable[[Message], typing.Awaitable[None]]
88

99
ASGIApp = typing.Callable[[Scope, Receive, Send], typing.Awaitable[None]]
10+
11+
MiddewareType = ASGIApp
12+
"""Type alias used for middleware types."""

tests/middleware/test_base.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import contextvars
2+
import typing
23
from contextlib import AsyncExitStack
34

45
import anyio
@@ -10,7 +11,7 @@
1011
from starlette.middleware.base import BaseHTTPMiddleware
1112
from starlette.responses import PlainTextResponse, StreamingResponse
1213
from starlette.routing import Route, WebSocketRoute
13-
from starlette.types import ASGIApp, Receive, Scope, Send
14+
from starlette.types import ASGIApp, MiddewareType, Receive, Scope, Send
1415

1516

1617
class CustomMiddleware(BaseHTTPMiddleware):
@@ -193,7 +194,7 @@ async def dispatch(self, request, call_next):
193194
),
194195
],
195196
)
196-
def test_contextvars(test_client_factory, middleware_cls: type):
197+
def test_contextvars(test_client_factory, middleware_cls: typing.Type[MiddewareType]):
197198
# this has to be an async endpoint because Starlette calls run_in_threadpool
198199
# on sync endpoints which has it's own set of peculiarities w.r.t propagating
199200
# contextvars (it propagates them forwards but not backwards)

tests/middleware/test_middleware.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
from starlette.middleware import Middleware
2+
from starlette.types import Receive, Scope, Send
23

34

45
class CustomMiddleware:
5-
pass
6+
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
7+
return None # pragma: no cover
68

79

810
def test_middleware_repr():

0 commit comments

Comments
 (0)