Skip to content

Commit

Permalink
Add ResponderOrResource and Action types
Browse files Browse the repository at this point in the history
  • Loading branch information
copalco committed Nov 5, 2023
1 parent d776554 commit c29d0c6
Showing 1 changed file with 19 additions and 20 deletions.
39 changes: 19 additions & 20 deletions falcon/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,13 @@
r'^on_({})(_\w+)?$'.format('|'.join(method.lower() for method in COMBINED_METHODS))
)

SynchronousResource = t.Callable[..., t.Any]
AsynchronousResource = t.Callable[..., t.Awaitable[t.Any]]
Resource = t.Union[SynchronousResource, AsynchronousResource]
ResponderOrResource = t.Union[t.Callable, type]
Action = t.Callable


def before(
action: Resource, *args: t.Any, is_async: bool = False, **kwargs: t.Any
) -> t.Callable[[Resource], Resource]:
action: Action, *args: t.Any, is_async: bool = False, **kwargs: t.Any
) -> t.Callable[[ResponderOrResource], ResponderOrResource]:
"""Execute the given action function *before* the responder.
The `params` argument that is passed to the hook
Expand Down Expand Up @@ -90,7 +89,7 @@ def do_something(req, resp, resource, params):
*action*.
"""

def _before(responder_or_resource: Resource) -> Resource:
def _before(responder_or_resource: ResponderOrResource) -> ResponderOrResource:
if isinstance(responder_or_resource, type):
resource = responder_or_resource

Expand All @@ -100,7 +99,7 @@ def _before(responder_or_resource: Resource) -> Resource:
# responder in the do_before_all closure; otherwise, they
# will capture the same responder variable that is shared
# between iterations of the for loop, above.
def let(responder: Resource = responder) -> None:
def let(responder: ResponderOrResource = responder) -> None:
do_before_all = _wrap_with_before(
responder, action, args, kwargs, is_async
)
Expand All @@ -121,8 +120,8 @@ def let(responder: Resource = responder) -> None:


def after(
action: Resource, *args: t.Any, is_async: bool = False, **kwargs: t.Any
) -> t.Callable[[Resource], Resource]:
action: Action, *args: t.Any, is_async: bool = False, **kwargs: t.Any
) -> t.Callable[[Action], Action]:
"""Execute the given action function *after* the responder.
Args:
Expand Down Expand Up @@ -155,14 +154,14 @@ def after(
*action*.
"""

def _after(responder_or_resource: Resource) -> Resource:
def _after(responder_or_resource: ResponderOrResource) -> ResponderOrResource:
if isinstance(responder_or_resource, type):
resource = responder_or_resource

for responder_name, responder in getmembers(resource, callable):
if _DECORABLE_METHOD_NAME.match(responder_name):

def let(responder: Resource = responder) -> None:
def let(responder: ResponderOrResource = responder) -> None:
do_after_all = _wrap_with_after(
responder, action, args, kwargs, is_async
)
Expand All @@ -188,12 +187,12 @@ def let(responder: Resource = responder) -> None:


def _wrap_with_after(
responder: Resource,
action: Resource,
responder: ResponderOrResource,
action: Action,
action_args: t.Any,
action_kwargs: t.Any,
is_async: bool,
) -> Resource:
) -> ResponderOrResource:
"""Execute the given action function after a responder method.
Args:
Expand Down Expand Up @@ -222,7 +221,7 @@ def _wrap_with_after(

@wraps(responder)
async def do_after(
self: Resource,
self: ResponderOrResource,
req: asgi.Request,
resp: asgi.Response,
*args: t.Any,
Expand All @@ -239,7 +238,7 @@ async def do_after(

@wraps(responder)
def do_after(
self: Resource,
self: ResponderOrResource,
req: wsgi.Request,
resp: wsgi.Response,
*args: t.Any,
Expand All @@ -255,8 +254,8 @@ def do_after(


def _wrap_with_before(
responder: Resource,
action: Resource,
responder: ResponderOrResource,
action: Action,
action_args: t.Tuple[t.Any, ...],
action_kwargs: t.Dict[str, t.Any],
is_async: bool,
Expand Down Expand Up @@ -289,7 +288,7 @@ def _wrap_with_before(

@wraps(responder)
async def do_before(
self: Resource,
self: ResponderOrResource,
req: asgi.Request,
resp: asgi.Response,
*args: t.Any,
Expand All @@ -306,7 +305,7 @@ async def do_before(

@wraps(responder)
def do_before(
self: Resource,
self: ResponderOrResource,
req: wsgi.Request,
resp: wsgi.Response,
*args: t.Any,
Expand Down

0 comments on commit c29d0c6

Please sign in to comment.