mirror of https://github.com/zulip/zulip.git
mypy: Adjust validator.py to use object rather than Any.
This commit is contained in:
parent
331a3c3447
commit
72eba6a0ce
|
@ -28,41 +28,41 @@ for any particular type of object.
|
|||
from django.utils.translation import ugettext as _
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.core.validators import validate_email, URLValidator
|
||||
from typing import Any, Callable, Iterable, Optional, Tuple, TypeVar, Text
|
||||
from typing import Callable, Iterable, Optional, Tuple, TypeVar, Text
|
||||
|
||||
from zerver.lib.request import JsonableError
|
||||
|
||||
Validator = Callable[[str, Any], Optional[str]]
|
||||
Validator = Callable[[str, object], Optional[str]]
|
||||
|
||||
def check_string(var_name: str, val: Any) -> Optional[str]:
|
||||
def check_string(var_name: str, val: object) -> Optional[str]:
|
||||
if not isinstance(val, str):
|
||||
return _('%s is not a string') % (var_name,)
|
||||
return None
|
||||
|
||||
def check_short_string(var_name: str, val: Any) -> Optional[str]:
|
||||
def check_short_string(var_name: str, val: object) -> Optional[str]:
|
||||
max_length = 200
|
||||
if len(val) >= max_length:
|
||||
return _("{var_name} is longer than {max_length}.".format(
|
||||
var_name=var_name, max_length=max_length))
|
||||
return check_string(var_name, val)
|
||||
|
||||
def check_int(var_name: str, val: Any) -> Optional[str]:
|
||||
def check_int(var_name: str, val: object) -> Optional[str]:
|
||||
if not isinstance(val, int):
|
||||
return _('%s is not an integer') % (var_name,)
|
||||
return None
|
||||
|
||||
def check_float(var_name: str, val: Any) -> Optional[str]:
|
||||
def check_float(var_name: str, val: object) -> Optional[str]:
|
||||
if not isinstance(val, float):
|
||||
return _('%s is not a float') % (var_name,)
|
||||
return None
|
||||
|
||||
def check_bool(var_name: str, val: Any) -> Optional[str]:
|
||||
def check_bool(var_name: str, val: object) -> Optional[str]:
|
||||
if not isinstance(val, bool):
|
||||
return _('%s is not a boolean') % (var_name,)
|
||||
return None
|
||||
|
||||
def check_none_or(sub_validator: Validator) -> Validator:
|
||||
def f(var_name: str, val: Any) -> Optional[str]:
|
||||
def f(var_name: str, val: object) -> Optional[str]:
|
||||
if val is None:
|
||||
return None
|
||||
else:
|
||||
|
@ -70,7 +70,7 @@ def check_none_or(sub_validator: Validator) -> Validator:
|
|||
return f
|
||||
|
||||
def check_list(sub_validator: Optional[Validator], length: Optional[int]=None) -> Validator:
|
||||
def f(var_name: str, val: Any) -> Optional[str]:
|
||||
def f(var_name: str, val: object) -> Optional[str]:
|
||||
if not isinstance(val, list):
|
||||
return _('%s is not a list') % (var_name,)
|
||||
|
||||
|
@ -90,7 +90,7 @@ def check_list(sub_validator: Optional[Validator], length: Optional[int]=None) -
|
|||
|
||||
def check_dict(required_keys: Iterable[Tuple[str, Validator]],
|
||||
_allow_only_listed_keys: bool=False) -> Validator:
|
||||
def f(var_name: str, val: Any) -> Optional[str]:
|
||||
def f(var_name: str, val: object) -> Optional[str]:
|
||||
if not isinstance(val, dict):
|
||||
return _('%s is not a dict') % (var_name,)
|
||||
|
||||
|
@ -123,15 +123,15 @@ def check_variable_type(allowed_type_funcs: Iterable[Validator]) -> Validator:
|
|||
`allowed_type_funcs`: the check_* validator functions for the possible data
|
||||
types for this variable.
|
||||
"""
|
||||
def enumerated_type_check(var_name: str, val: Any) -> Optional[str]:
|
||||
def enumerated_type_check(var_name: str, val: object) -> Optional[str]:
|
||||
for func in allowed_type_funcs:
|
||||
if not func(var_name, val):
|
||||
return None
|
||||
return _('%s is not an allowed_type') % (var_name,)
|
||||
return enumerated_type_check
|
||||
|
||||
def equals(expected_val: Any) -> Validator:
|
||||
def f(var_name: str, val: Any) -> Optional[str]:
|
||||
def equals(expected_val: object) -> Validator:
|
||||
def f(var_name: str, val: object) -> Optional[str]:
|
||||
if val != expected_val:
|
||||
return (_('%(variable)s != %(expected_value)s (%(value)s is wrong)') %
|
||||
{'variable': var_name,
|
||||
|
|
Loading…
Reference in New Issue