From 8cfacbf8aa5587ab2e9a886f9069d0a922f928f5 Mon Sep 17 00:00:00 2001 From: Alex Vandiver Date: Wed, 19 Aug 2020 15:20:05 -0700 Subject: [PATCH] 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] https://github.com/python/typing/issues/193 --- zerver/decorator.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/zerver/decorator.py b/zerver/decorator.py index 6cb5cf59db..4e9e2a884d 100644 --- a/zerver/decorator.py +++ b/zerver/decorator.py @@ -324,8 +324,9 @@ def api_key_only_webhook_view( webhook_client_name: str, notify_bot_owner_on_invalid_json: bool=True, ) -> Callable[[Callable[..., HttpResponse]], Callable[..., HttpResponse]]: - # TODO The typing here could be improved by using the Extended Callable types: - # https://mypy.readthedocs.io/en/latest/kinds_of_types.html#extended-callable-types + # Unfortunately, callback protocols are insufficient for this: + # https://mypy.readthedocs.io/en/stable/protocols.html#callback-protocols + # Variadic generics are necessary: https://github.com/python/typing/issues/193 def _wrapped_view_func(view_func: Callable[..., HttpResponse]) -> Callable[..., HttpResponse]: @csrf_exempt @has_request_variables