Skip to content

Commit c443396

Browse files
authored
Relax strictness on Middleware type (#3059)
1 parent 85bf027 commit c443396

1 file changed

Lines changed: 12 additions & 10 deletions

File tree

starlette/middleware/__init__.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,32 @@
11
from __future__ import annotations
22

33
import sys
4-
from collections.abc import Iterator
5-
from typing import Any, Protocol
4+
from collections.abc import Awaitable, Iterator
5+
from typing import Any, Callable, Protocol
66

77
if sys.version_info >= (3, 10): # pragma: no cover
88
from typing import ParamSpec
99
else: # pragma: no cover
1010
from typing_extensions import ParamSpec
1111

12-
from starlette.types import ASGIApp
1312

1413
P = ParamSpec("P")
1514

1615

16+
_Scope = Any
17+
_Receive = Callable[[], Awaitable[Any]]
18+
_Send = Callable[[Any], Awaitable[None]]
19+
# Since `starlette.types.ASGIApp` type differs from `ASGIApplication` from `asgiref`
20+
# we need to define a more permissive version of ASGIApp that doesn't cause type errors.
21+
_ASGIApp = Callable[[_Scope, _Receive, _Send], Awaitable[None]]
22+
23+
1724
class _MiddlewareFactory(Protocol[P]):
18-
def __call__(self, app: ASGIApp, /, *args: P.args, **kwargs: P.kwargs) -> ASGIApp: ... # pragma: no cover
25+
def __call__(self, app: _ASGIApp, /, *args: P.args, **kwargs: P.kwargs) -> _ASGIApp: ... # pragma: no cover
1926

2027

2128
class Middleware:
22-
def __init__(
23-
self,
24-
cls: _MiddlewareFactory[P],
25-
*args: P.args,
26-
**kwargs: P.kwargs,
27-
) -> None:
29+
def __init__(self, cls: _MiddlewareFactory[P], *args: P.args, **kwargs: P.kwargs) -> None:
2830
self.cls = cls
2931
self.args = args
3032
self.kwargs = kwargs

0 commit comments

Comments
 (0)