Skip to content

Commit c7af006

Browse files
committed
webhooks: Update comment about typing the webhook decorator.
The previous link was to "extended callable" types, which are deprecated in favor of callback protocols. Unfortunately, defining a protocol class can't express the typing -- we need some sort of variadic generics[1]. Specifically, we wish to support hitting the endpoint with additional parameters; thus, this protocol is insufficient: ``` class WebhookHandler(Protocol): def __call__(request: HttpRequest, api_key: str) -> HttpResponse: ... ``` ...since it prohibits additional parameters. And allowing extra arguments: ``` class WebhookHandler(Protocol): def __call__(request: HttpRequest, api_key: str, *args: object, **kwargs: object) -> HttpResponse: ... ``` ...is similarly problematic, since the view handlers do not support _arbitrary_ keyword arguments. [1] python/typing#193
1 parent 5ce2616 commit c7af006

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

zerver/decorator.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,9 @@ def api_key_only_webhook_view(
324324
webhook_client_name: str,
325325
notify_bot_owner_on_invalid_json: bool=True,
326326
) -> Callable[[Callable[..., HttpResponse]], Callable[..., HttpResponse]]:
327-
# TODO The typing here could be improved by using the Extended Callable types:
328-
# https://mypy.readthedocs.io/en/latest/kinds_of_types.html#extended-callable-types
327+
# Unfortunately, callback protocols are insufficient for this:
328+
# https://mypy.readthedocs.io/en/stable/protocols.html#callback-protocols
329+
# Variadic generics are necessary: https://github.com/python/typing/issues/193
329330
def _wrapped_view_func(view_func: Callable[..., HttpResponse]) -> Callable[..., HttpResponse]:
330331
@csrf_exempt
331332
@has_request_variables

0 commit comments

Comments
 (0)