Skip to content

Commit

Permalink
chore: apply review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
CaselIT committed Aug 21, 2024
1 parent 3ce8b64 commit 55bf166
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 52 deletions.
102 changes: 54 additions & 48 deletions falcon/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'])


Expand Down
5 changes: 2 additions & 3 deletions falcon/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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: ...
Expand Down
1 change: 0 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 55bf166

Please sign in to comment.