-
-
Notifications
You must be signed in to change notification settings - Fork 960
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Paweł Rubin <[email protected]>
- Loading branch information
1 parent
23c81da
commit 866a15f
Showing
7 changed files
with
77 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,38 @@ | ||
import typing | ||
from typing import Any, Iterator, Protocol, Type | ||
|
||
from typing_extensions import ParamSpec | ||
|
||
from starlette.types import ASGIApp, Receive, Scope, Send | ||
|
||
P = ParamSpec("P") | ||
|
||
|
||
class _MiddlewareClass(Protocol[P]): | ||
def __init__(self, app: ASGIApp, *args: P.args, **kwargs: P.kwargs) -> None: | ||
... # pragma: no cover | ||
|
||
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: | ||
... # pragma: no cover | ||
|
||
|
||
class Middleware: | ||
def __init__(self, cls: type, **options: typing.Any) -> None: | ||
def __init__( | ||
self, | ||
cls: Type[_MiddlewareClass[P]], | ||
*args: P.args, | ||
**kwargs: P.kwargs, | ||
) -> None: | ||
self.cls = cls | ||
self.options = options | ||
self.args = args | ||
self.kwargs = kwargs | ||
|
||
def __iter__(self) -> typing.Iterator[typing.Any]: | ||
as_tuple = (self.cls, self.options) | ||
def __iter__(self) -> Iterator[Any]: | ||
as_tuple = (self.cls, self.args, self.kwargs) | ||
return iter(as_tuple) | ||
|
||
def __repr__(self) -> str: | ||
class_name = self.__class__.__name__ | ||
option_strings = [f"{key}={value!r}" for key, value in self.options.items()] | ||
args_repr = ", ".join([self.cls.__name__] + option_strings) | ||
args_strings = [f"{value!r}" for value in self.args] | ||
option_strings = [f"{key}={value!r}" for key, value in self.kwargs.items()] | ||
args_repr = ", ".join([self.cls.__name__] + args_strings + option_strings) | ||
return f"{class_name}({args_repr})" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,22 @@ | ||
from starlette.middleware import Middleware | ||
from starlette.types import ASGIApp, Receive, Scope, Send | ||
|
||
|
||
class CustomMiddleware: | ||
pass | ||
class CustomMiddleware: # pragma: no cover | ||
def __init__(self, app: ASGIApp, foo: str, *, bar: int) -> None: | ||
self.app = app | ||
self.foo = foo | ||
self.bar = bar | ||
|
||
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: | ||
await self.app(scope, receive, send) | ||
|
||
def test_middleware_repr(): | ||
middleware = Middleware(CustomMiddleware) | ||
assert repr(middleware) == "Middleware(CustomMiddleware)" | ||
|
||
def test_middleware_repr() -> None: | ||
middleware = Middleware(CustomMiddleware, "foo", bar=123) | ||
assert repr(middleware) == "Middleware(CustomMiddleware, 'foo', bar=123)" | ||
|
||
|
||
def test_middleware_iter() -> None: | ||
cls, args, kwargs = Middleware(CustomMiddleware, "foo", bar=123) | ||
assert (cls, args, kwargs) == (CustomMiddleware, ("foo",), {"bar": 123}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters