REQ: Add type parameter to act as hint for typing.

This commit is contained in:
neiljp (Neil Pilgrim) 2017-12-24 02:06:17 +00:00 committed by Greg Price
parent f184249191
commit 756af7518c
2 changed files with 8 additions and 3 deletions

View File

@ -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

View File

@ -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,