From 756af7518c2a20f1364623dc9bc4bc9453a725a9 Mon Sep 17 00:00:00 2001 From: "neiljp (Neil Pilgrim)" Date: Sun, 24 Dec 2017 02:06:17 +0000 Subject: [PATCH] REQ: Add type parameter to act as hint for typing. --- zerver/lib/request.py | 8 ++++++-- zerver/lib/request.pyi | 3 ++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/zerver/lib/request.py b/zerver/lib/request.py index 4d98fffd43..9524adfe13 100644 --- a/zerver/lib/request.py +++ b/zerver/lib/request.py @@ -14,7 +14,7 @@ from zerver.lib.exceptions import JsonableError, ErrorCode from django.http import HttpRequest, HttpResponse -from typing import Any, Callable +from typing import Any, Callable, Type class RequestVariableMissingError(JsonableError): code = ErrorCode.REQUEST_VARIABLE_MISSING @@ -50,7 +50,7 @@ class REQ: def __init__(self, whence: str=None, *, converter: Callable[[Any], Any]=None, default: Any=NotSpecified, validator: Callable[[Any], Any]=None, - argument_type: str=None) -> None: + argument_type: str=None, type: Type=None) -> None: """whence: the name of the request variable that should be used for this parameter. Defaults to a request variable of the same name as the parameter. @@ -68,6 +68,10 @@ class REQ: argument_type: pass 'body' to extract the parsed JSON corresponding to the request body + + type: a hint to typing (using mypy) what the type of this parameter is. + Currently only typically necessary if default=None and the type cannot + be inferred in another way (eg. via converter). """ self.post_var_name = whence diff --git a/zerver/lib/request.pyi b/zerver/lib/request.pyi index 360a7565aa..bc308dce7f 100644 --- a/zerver/lib/request.pyi +++ b/zerver/lib/request.pyi @@ -6,7 +6,7 @@ # scan the parameter list for REQ objects and patch the parameters as the true # types. -from typing import Any, Callable, Text, TypeVar, Optional, Union +from typing import Any, Callable, Text, TypeVar, Optional, Union, Type from django.http import HttpResponse from zerver.lib.exceptions import JsonableError as JsonableError @@ -24,6 +24,7 @@ NotSpecified = _NotSpecified() def REQ(whence: Optional[str] = None, *, + type: Type[ResultT] = None, converter: Optional[Callable[[str], ResultT]] = None, default: Union[_NotSpecified, ResultT] = NotSpecified, validator: Optional[Validator] = None,