mirror of
https://github.com/zulip/zulip.git
synced 2025-11-04 22:13:26 +00:00
mypy: Use tuples for muted_topics.
We now have our muted topics use tuples internally, which allows us to tighten up the annotation for get_topic_mutes, as well as our schema checking. We want to deprecate sub_validator=None for check_list, so we also introduce check_tuple here. Now we also want to deprecate check_tuple, but it's at least isolated now. We will use this for data structures that are tuples, but which are sent as lists over the wire. Fortunately, we don't have too many of those. The plan is to convert tuples to dictionaries, but backward compatibility may be tricky in some places.
This commit is contained in:
@@ -163,6 +163,24 @@ def check_list(sub_validator: Optional[Validator[ResultT]]=None, length: Optiona
|
||||
return cast(List[ResultT], val)
|
||||
return f
|
||||
|
||||
def check_tuple(sub_validators: List[Validator[ResultT]]) -> Validator[Tuple[Any, ...]]:
|
||||
def f(var_name: str, val: object) -> Tuple[Any, ...]:
|
||||
if not isinstance(val, tuple):
|
||||
raise ValidationError(_('{var_name} is not a tuple').format(var_name=var_name))
|
||||
|
||||
desired_len = len(sub_validators)
|
||||
if desired_len != len(val):
|
||||
raise ValidationError(_('{var_name} should have exactly {desired_len} items').format(
|
||||
var_name=var_name, desired_len=desired_len,
|
||||
))
|
||||
|
||||
for i, sub_validator in enumerate(sub_validators):
|
||||
vname = f'{var_name}[{i}]'
|
||||
sub_validator(vname, val[i])
|
||||
|
||||
return val
|
||||
return f
|
||||
|
||||
# https://zulip.readthedocs.io/en/latest/testing/mypy.html#using-overload-to-accurately-describe-variations
|
||||
@overload
|
||||
def check_dict(required_keys: Iterable[Tuple[str, Validator[object]]]=[],
|
||||
|
||||
Reference in New Issue
Block a user