From 55bf166895879ee98374bbc373baf534e71d9d24 Mon Sep 17 00:00:00 2001 From: Federico Caselli Date: Wed, 21 Aug 2024 13:05:42 +0200 Subject: [PATCH] chore: apply review suggestions --- falcon/hooks.py | 102 +++++++++++++++++++++++++---------------------- falcon/typing.py | 5 +-- setup.cfg | 1 - 3 files changed, 56 insertions(+), 52 deletions(-) diff --git a/falcon/hooks.py b/falcon/hooks.py index 1b2b2ea6d..fb377e6c8 100644 --- a/falcon/hooks.py +++ b/falcon/hooks.py @@ -46,55 +46,61 @@ from falcon.typing import Responder from falcon.typing import SyncResponderMethod - # TODO: if is_async is removed these protocol would no longer be needed, since - # ParamSpec could be used together with Concatenate to use a simple Callable - # to type the before and after functions. This approach was prototyped in - # https://github.com/falconry/falcon/pull/2234 - class SyncBeforeFn(Protocol): - def __call__( - self, - req: wsgi.Request, - resp: wsgi.Response, - resource: Resource, - params: Dict[str, Any], - *args: Any, - **kwargs: Any, - ) -> None: ... - - class AsyncBeforeFn(Protocol): - def __call__( - self, - req: asgi.Request, - resp: asgi.Response, - resource: Resource, - params: Dict[str, Any], - *args: Any, - **kwargs: Any, - ) -> Awaitable[None]: ... - - BeforeFn = Union[SyncBeforeFn, AsyncBeforeFn] - - class SyncAfterFn(Protocol): - def __call__( - self, - req: wsgi.Request, - resp: wsgi.Response, - resource: Resource, - *args: Any, - **kwargs: Any, - ) -> None: ... - - class AsyncAfterFn(Protocol): - def __call__( - self, - req: asgi.Request, - resp: asgi.Response, - resource: Resource, - *args: Any, - **kwargs: Any, - ) -> Awaitable[None]: ... - AfterFn = Union[SyncAfterFn, AsyncAfterFn] +# TODO: if is_async is removed these protocol would no longer be needed, since +# ParamSpec could be used together with Concatenate to use a simple Callable +# to type the before and after functions. This approach was prototyped in +# https://github.com/falconry/falcon/pull/2234 +class SyncBeforeFn(Protocol): + def __call__( + self, + req: wsgi.Request, + resp: wsgi.Response, + resource: Resource, + params: Dict[str, Any], + *args: Any, + **kwargs: Any, + ) -> None: ... + + +class AsyncBeforeFn(Protocol): + def __call__( + self, + req: asgi.Request, + resp: asgi.Response, + resource: Resource, + params: Dict[str, Any], + *args: Any, + **kwargs: Any, + ) -> Awaitable[None]: ... + + +BeforeFn = Union[SyncBeforeFn, AsyncBeforeFn] + + +class SyncAfterFn(Protocol): + def __call__( + self, + req: wsgi.Request, + resp: wsgi.Response, + resource: Resource, + *args: Any, + **kwargs: Any, + ) -> None: ... + + +class AsyncAfterFn(Protocol): + def __call__( + self, + req: asgi.Request, + resp: asgi.Response, + resource: Resource, + *args: Any, + **kwargs: Any, + ) -> Awaitable[None]: ... + + +AfterFn = Union[SyncAfterFn, AsyncAfterFn] _R = TypeVar('_R', bound=Union['Responder', 'Resource']) diff --git a/falcon/typing.py b/falcon/typing.py index 1c92686c7..6bb5315fc 100644 --- a/falcon/typing.py +++ b/falcon/typing.py @@ -29,7 +29,6 @@ ) if TYPE_CHECKING: - import falcon as wsgi from falcon import asgi from falcon.request import Request from falcon.response import Response @@ -73,8 +72,8 @@ class SyncResponderMethod(Protocol): def __call__( self, resource: Resource, - req: wsgi.Request, - resp: wsgi.Response, + req: Request, + resp: Response, *args: Any, **kwargs: Any, ) -> None: ... diff --git a/setup.cfg b/setup.cfg index 5bfe8c99a..04a693b0f 100644 --- a/setup.cfg +++ b/setup.cfg @@ -54,7 +54,6 @@ include_package_data = True packages = find: python_requires = >=3.8 install_requires = - typing-extensions; python_version<"3.8" tests_require = testtools requests