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

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

20 statements  

1from __future__ import annotations 

2 

3import sys 

4from typing import Any, Iterator, Protocol 

5 

6if sys.version_info >= (3, 10): # pragma: no cover 

7 from typing import ParamSpec 

8else: # pragma: no cover 

9 from typing_extensions import ParamSpec 

10 

11from starlette.types import ASGIApp, Receive, Scope, Send 

12 

13P = ParamSpec("P") 

14 

15 

16class _MiddlewareClass(Protocol[P]): 

17 def __init__(self, app: ASGIApp, *args: P.args, **kwargs: P.kwargs) -> None: ... # pragma: no cover 

18 

19 async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: ... # pragma: no cover 

20 

21 

22class Middleware: 

23 def __init__( 

24 self, 

25 cls: type[_MiddlewareClass[P]], 

26 *args: P.args, 

27 **kwargs: P.kwargs, 

28 ) -> None: 

29 self.cls = cls 

30 self.args = args 

31 self.kwargs = kwargs 

32 

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

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

35 return iter(as_tuple) 

36 

37 def __repr__(self) -> str: 

38 class_name = self.__class__.__name__ 

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

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

41 args_repr = ", ".join([self.cls.__name__] + args_strings + option_strings) 

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