mirror of
https://github.com/zulip/zulip.git
synced 2025-11-04 14:03:30 +00:00
python: Reformat with Black, except quotes.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
committed by
Tim Abbott
parent
5028c081cb
commit
11741543da
@@ -38,6 +38,7 @@ class RequestConfusingParmsError(JsonableError):
|
||||
def msg_format() -> str:
|
||||
return _("Can't decide between '{var_name1}' and '{var_name2}' arguments")
|
||||
|
||||
|
||||
class RequestVariableMissingError(JsonableError):
|
||||
code = ErrorCode.REQUEST_VARIABLE_MISSING
|
||||
data_fields = ['var_name']
|
||||
@@ -49,6 +50,7 @@ class RequestVariableMissingError(JsonableError):
|
||||
def msg_format() -> str:
|
||||
return _("Missing '{var_name}' argument")
|
||||
|
||||
|
||||
class RequestVariableConversionError(JsonableError):
|
||||
code = ErrorCode.REQUEST_VARIABLE_INVALID
|
||||
data_fields = ['var_name', 'bad_value']
|
||||
@@ -61,15 +63,18 @@ class RequestVariableConversionError(JsonableError):
|
||||
def msg_format() -> str:
|
||||
return _("Bad value for '{var_name}': {bad_value}")
|
||||
|
||||
|
||||
# Used in conjunction with @has_request_variables, below
|
||||
ResultT = TypeVar('ResultT')
|
||||
|
||||
|
||||
class _REQ(Generic[ResultT]):
|
||||
# NotSpecified is a sentinel value for determining whether a
|
||||
# default value was specified for a request variable. We can't
|
||||
# use None because that could be a valid, user-specified default
|
||||
class _NotSpecified:
|
||||
pass
|
||||
|
||||
NotSpecified = _NotSpecified()
|
||||
|
||||
def __init__(
|
||||
@@ -81,10 +86,10 @@ class _REQ(Generic[ResultT]):
|
||||
validator: Optional[Validator[ResultT]] = None,
|
||||
str_validator: Optional[Validator[ResultT]] = None,
|
||||
argument_type: Optional[str] = None,
|
||||
intentionally_undocumented: bool=False,
|
||||
documentation_pending: bool=False,
|
||||
intentionally_undocumented: bool = False,
|
||||
documentation_pending: bool = False,
|
||||
aliases: Sequence[str] = [],
|
||||
path_only: bool=False
|
||||
path_only: bool = False,
|
||||
) -> None:
|
||||
"""whence: the name of the request variable that should be used
|
||||
for this parameter. Defaults to a request variable of the
|
||||
@@ -124,8 +129,13 @@ class _REQ(Generic[ResultT]):
|
||||
self.documentation_pending = documentation_pending
|
||||
self.path_only = path_only
|
||||
|
||||
assert converter is None or (validator is None and str_validator is None), 'converter and validator are mutually exclusive'
|
||||
assert validator is None or str_validator is None, 'validator and str_validator are mutually exclusive'
|
||||
assert converter is None or (
|
||||
validator is None and str_validator is None
|
||||
), 'converter and validator are mutually exclusive'
|
||||
assert (
|
||||
validator is None or str_validator is None
|
||||
), 'validator and str_validator are mutually exclusive'
|
||||
|
||||
|
||||
# This factory function ensures that mypy can correctly analyze REQ.
|
||||
#
|
||||
@@ -148,10 +158,11 @@ def REQ(
|
||||
intentionally_undocumented: bool = ...,
|
||||
documentation_pending: bool = ...,
|
||||
aliases: Sequence[str] = ...,
|
||||
path_only: bool = ...
|
||||
path_only: bool = ...,
|
||||
) -> ResultT:
|
||||
...
|
||||
|
||||
|
||||
# Overload 2: validator
|
||||
@overload
|
||||
def REQ(
|
||||
@@ -162,10 +173,11 @@ def REQ(
|
||||
intentionally_undocumented: bool = ...,
|
||||
documentation_pending: bool = ...,
|
||||
aliases: Sequence[str] = ...,
|
||||
path_only: bool = ...
|
||||
path_only: bool = ...,
|
||||
) -> ResultT:
|
||||
...
|
||||
|
||||
|
||||
# Overload 3: no converter/validator, default: str or unspecified, argument_type=None
|
||||
@overload
|
||||
def REQ(
|
||||
@@ -176,10 +188,11 @@ def REQ(
|
||||
intentionally_undocumented: bool = ...,
|
||||
documentation_pending: bool = ...,
|
||||
aliases: Sequence[str] = ...,
|
||||
path_only: bool = ...
|
||||
path_only: bool = ...,
|
||||
) -> str:
|
||||
...
|
||||
|
||||
|
||||
# Overload 4: no converter/validator, default=None, argument_type=None
|
||||
@overload
|
||||
def REQ(
|
||||
@@ -190,10 +203,11 @@ def REQ(
|
||||
intentionally_undocumented: bool = ...,
|
||||
documentation_pending: bool = ...,
|
||||
aliases: Sequence[str] = ...,
|
||||
path_only: bool = ...
|
||||
path_only: bool = ...,
|
||||
) -> Optional[str]:
|
||||
...
|
||||
|
||||
|
||||
# Overload 5: argument_type="body"
|
||||
@overload
|
||||
def REQ(
|
||||
@@ -204,10 +218,11 @@ def REQ(
|
||||
intentionally_undocumented: bool = ...,
|
||||
documentation_pending: bool = ...,
|
||||
aliases: Sequence[str] = ...,
|
||||
path_only: bool = ...
|
||||
path_only: bool = ...,
|
||||
) -> ResultT:
|
||||
...
|
||||
|
||||
|
||||
# Implementation
|
||||
def REQ(
|
||||
whence: Optional[str] = None,
|
||||
@@ -217,23 +232,27 @@ def REQ(
|
||||
validator: Optional[Validator[ResultT]] = None,
|
||||
str_validator: Optional[Validator[ResultT]] = None,
|
||||
argument_type: Optional[str] = None,
|
||||
intentionally_undocumented: bool=False,
|
||||
documentation_pending: bool=False,
|
||||
intentionally_undocumented: bool = False,
|
||||
documentation_pending: bool = False,
|
||||
aliases: Sequence[str] = [],
|
||||
path_only: bool = False
|
||||
path_only: bool = False,
|
||||
) -> ResultT:
|
||||
return cast(ResultT, _REQ(
|
||||
whence,
|
||||
converter=converter,
|
||||
default=default,
|
||||
validator=validator,
|
||||
str_validator=str_validator,
|
||||
argument_type=argument_type,
|
||||
intentionally_undocumented=intentionally_undocumented,
|
||||
documentation_pending=documentation_pending,
|
||||
aliases=aliases,
|
||||
path_only=path_only,
|
||||
))
|
||||
return cast(
|
||||
ResultT,
|
||||
_REQ(
|
||||
whence,
|
||||
converter=converter,
|
||||
default=default,
|
||||
validator=validator,
|
||||
str_validator=str_validator,
|
||||
argument_type=argument_type,
|
||||
intentionally_undocumented=intentionally_undocumented,
|
||||
documentation_pending=documentation_pending,
|
||||
aliases=aliases,
|
||||
path_only=path_only,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
arguments_map: Dict[str, List[str]] = defaultdict(list)
|
||||
|
||||
@@ -259,7 +278,7 @@ def has_request_variables(view_func: ViewFuncT) -> ViewFuncT:
|
||||
if default_param_values is None:
|
||||
default_param_values = ()
|
||||
num_default_params = len(default_param_values)
|
||||
default_param_names = view_func.__code__.co_varnames[num_params - num_default_params:]
|
||||
default_param_names = view_func.__code__.co_varnames[num_params - num_default_params :]
|
||||
|
||||
post_params = []
|
||||
|
||||
@@ -274,9 +293,11 @@ def has_request_variables(view_func: ViewFuncT) -> ViewFuncT:
|
||||
|
||||
# Record arguments that should be documented so that our
|
||||
# automated OpenAPI docs tests can compare these against the code.
|
||||
if (not value.intentionally_undocumented
|
||||
and not value.documentation_pending
|
||||
and not value.path_only):
|
||||
if (
|
||||
not value.intentionally_undocumented
|
||||
and not value.documentation_pending
|
||||
and not value.path_only
|
||||
):
|
||||
arguments_map[view_func_full_name].append(value.post_var_name)
|
||||
|
||||
@wraps(view_func)
|
||||
@@ -371,6 +392,7 @@ def has_request_variables(view_func: ViewFuncT) -> ViewFuncT:
|
||||
|
||||
local = threading.local()
|
||||
|
||||
|
||||
def get_current_request() -> Optional[HttpRequest]:
|
||||
"""Returns the current HttpRequest object; this should only be used by
|
||||
logging frameworks, which have no other access to the current
|
||||
@@ -380,9 +402,11 @@ def get_current_request() -> Optional[HttpRequest]:
|
||||
"""
|
||||
return getattr(local, 'request', None)
|
||||
|
||||
|
||||
def set_request(req: HttpRequest) -> None:
|
||||
setattr(local, 'request', req)
|
||||
|
||||
|
||||
def unset_request() -> None:
|
||||
if hasattr(local, 'request'):
|
||||
delattr(local, 'request')
|
||||
|
||||
Reference in New Issue
Block a user