Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/starlette/middleware/__init__.py: 52%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

25 statements  

1from __future__ import annotations 

2 

3from collections.abc import Awaitable, Callable, Iterator 

4from typing import Any, ParamSpec, Protocol 

5 

6P = ParamSpec("P") 

7 

8 

9_Scope = Any 

10_Receive = Callable[[], Awaitable[Any]] 

11_Send = Callable[[Any], Awaitable[None]] 

12# Since `starlette.types.ASGIApp` type differs from `ASGIApplication` from `asgiref` 

13# we need to define a more permissive version of ASGIApp that doesn't cause type errors. 

14_ASGIApp = Callable[[_Scope, _Receive, _Send], Awaitable[None]] 

15 

16 

17class _MiddlewareFactory(Protocol[P]): 

18 def __call__(self, app: _ASGIApp, /, *args: P.args, **kwargs: P.kwargs) -> _ASGIApp: ... # pragma: no cover 

19 

20 

21class Middleware: 

22 def __init__(self, cls: _MiddlewareFactory[P], *args: P.args, **kwargs: P.kwargs) -> None: 

23 self.cls = cls 

24 self.args = args 

25 self.kwargs = kwargs 

26 

27 def __iter__(self) -> Iterator[Any]: 

28 as_tuple = (self.cls, self.args, self.kwargs) 

29 return iter(as_tuple) 

30 

31 def __repr__(self) -> str: 

32 class_name = self.__class__.__name__ 

33 args_strings = [f"{value!r}" for value in self.args] 

34 option_strings = [f"{key}={value!r}" for key, value in self.kwargs.items()] 

35 name = getattr(self.cls, "__name__", "") 

36 args_repr = ", ".join([name] + args_strings + option_strings) 

37 return f"{class_name}({args_repr})"