Skip to content

Commit

Permalink
REQ: Add type parameter to act as hint for typing.
Browse files Browse the repository at this point in the history
  • Loading branch information
neiljp authored and gnprice committed Feb 13, 2018
1 parent f184249 commit 756af75
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
8 changes: 6 additions & 2 deletions zerver/lib/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

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
Expand Down Expand Up @@ -50,7 +50,7 @@ class _NotSpecified:

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.
Expand All @@ -68,6 +68,10 @@ def __init__(self, whence: str=None, *, converter: Callable[[Any], Any]=None,
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
Expand Down
3 changes: 2 additions & 1 deletion zerver/lib/request.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down

0 comments on commit 756af75

Please sign in to comment.