validators: Improve mypy type annotations.

The main thing here is writing check_string_fixed_length and
check_capped_string as returning a Validator, but we also fix issues
around passing default=None.
This commit is contained in:
Tim Abbott 2018-05-03 21:25:30 -07:00
parent 51517fa188
commit b1ad7593ba
2 changed files with 3 additions and 3 deletions

View File

@ -23,7 +23,7 @@ def REQ(whence: Optional[str] = None,
*,
type: Type[ResultT] = Type[None],
converter: Optional[Callable[[str], ResultT]] = None,
default: Union[_NotSpecified, ResultT] = NotSpecified,
default: Union[_NotSpecified, ResultT, None] = Optional[NotSpecified],
validator: Optional[Validator] = None,
str_validator: Optional[Validator] = None,
argument_type: Optional[str] = None) -> ResultT: ...

View File

@ -55,7 +55,7 @@ def check_required_string(var_name: str, val: object) -> Optional[str]:
def check_short_string(var_name: str, val: object) -> Optional[str]:
return check_capped_string(50)(var_name, val)
def check_capped_string(max_length: int) -> Callable[[str, object], Optional[str]]:
def check_capped_string(max_length: int) -> Validator:
def validator(var_name: str, val: object) -> Optional[str]:
if not isinstance(val, str):
return _('%s is not a string') % (var_name,)
@ -65,7 +65,7 @@ def check_capped_string(max_length: int) -> Callable[[str, object], Optional[str
return None
return validator
def check_string_fixed_length(length: int) -> Callable[[str, object], Optional[str]]:
def check_string_fixed_length(length: int) -> Validator:
def validator(var_name: str, val: object) -> Optional[str]:
if not isinstance(val, str):
return _('%s is not a string') % (var_name,)