refactor: Rename check_variable_type.

Rename the validator to check_union, to conform
more to Python typing nomenclature.

And we rename one of the test helpers to the
simpler `check_types`.  (The test helper
was using "variable" in the "var" sense.)
This commit is contained in:
Steve Howell 2020-06-20 08:37:43 +00:00 committed by Steve Howell
parent fbe45fa889
commit cc3d87b078
4 changed files with 14 additions and 14 deletions

View File

@ -263,7 +263,7 @@ def check_dict_only(required_keys: Iterable[Tuple[str, Validator]],
optional_keys: Iterable[Tuple[str, Validator]]=[]) -> Validator:
return check_dict(required_keys, optional_keys, _allow_only_listed_keys=True)
def check_variable_type(allowed_type_funcs: Iterable[Validator]) -> Validator:
def check_union(allowed_type_funcs: Iterable[Validator]) -> Validator:
"""
Use this validator if an argument is of a variable type (e.g. processing
properties that might be strings or booleans).

View File

@ -67,8 +67,8 @@ from zerver.lib.validator import (
check_string_in,
check_string_or_int,
check_string_or_int_list,
check_union,
check_url,
check_variable_type,
equals,
to_non_negative_int,
to_positive_or_allowed_int,
@ -960,15 +960,15 @@ class ValidatorTestCase(TestCase):
nonperson = 'misconfigured data'
self.assertEqual(check_person(nonperson), 'This is not a valid person')
def test_check_variable_type(self) -> None:
def test_check_union(self) -> None:
x: Any = 5
self.assertEqual(check_variable_type([check_string, check_int])('x', x), None)
self.assertEqual(check_union([check_string, check_int])('x', x), None)
x = 'x'
self.assertEqual(check_variable_type([check_string, check_int])('x', x), None)
self.assertEqual(check_union([check_string, check_int])('x', x), None)
x = [{}]
self.assertEqual(check_variable_type([check_string, check_int])('x', x), 'x is not an allowed_type')
self.assertEqual(check_union([check_string, check_int])('x', x), 'x is not an allowed_type')
def test_equals(self) -> None:
x: Any = 5

View File

@ -370,7 +370,7 @@ class ImportExportTest(ZulipTestCase):
record = data['zerver_attachment'][0]
self.assertEqual(record['path_id'], attachment_path_id)
def check_variable_type(user_profile_id: int, realm_id: int) -> None:
def check_types(user_profile_id: int, realm_id: int) -> None:
self.assertEqual(type(user_profile_id), int)
self.assertEqual(type(realm_id), int)
@ -382,7 +382,7 @@ class ImportExportTest(ZulipTestCase):
records = full_data['uploads_dir_records']
self.assertEqual(records[0]['path'], os.path.join(fields[0], fields[1], fields[2]))
self.assertEqual(records[0]['s3_path'], attachment_path_id)
check_variable_type(records[0]['user_profile_id'], records[0]['realm_id'])
check_types(records[0]['user_profile_id'], records[0]['realm_id'])
# Test emojis
fn = os.path.join(full_data['emoji_dir'], emoji_path)
@ -393,7 +393,7 @@ class ImportExportTest(ZulipTestCase):
self.assertTrue('last_modified' in records[0])
self.assertEqual(records[0]['path'], '2/emoji/images/1.png')
self.assertEqual(records[0]['s3_path'], '2/emoji/images/1.png')
check_variable_type(records[0]['user_profile_id'], records[0]['realm_id'])
check_types(records[0]['user_profile_id'], records[0]['realm_id'])
# Test realm logo and icon
records = full_data['realm_icons_dir_records']
@ -420,7 +420,7 @@ class ImportExportTest(ZulipTestCase):
record_s3_path = [record['s3_path'] for record in records]
self.assertIn(original_avatar_path_id, record_path)
self.assertIn(original_avatar_path_id, record_s3_path)
check_variable_type(records[0]['user_profile_id'], records[0]['realm_id'])
check_types(records[0]['user_profile_id'], records[0]['realm_id'])
def test_zulip_realm(self) -> None:
realm = Realm.objects.get(string_id='zulip')

View File

@ -75,7 +75,7 @@ from zerver.lib.validator import (
check_list,
check_string,
check_string_or_int,
check_variable_type,
check_union,
to_non_negative_int,
)
from zerver.models import (
@ -338,7 +338,7 @@ def compose_views(
def remove_subscriptions_backend(
request: HttpRequest, user_profile: UserProfile,
streams_raw: Iterable[str]=REQ("subscriptions", validator=check_list(check_string)),
principals: Optional[Union[List[str], List[int]]]=REQ(validator=check_variable_type([
principals: Optional[Union[List[str], List[int]]]=REQ(validator=check_union([
check_list(check_string), check_list(check_int)]), default=None),
) -> HttpResponse:
@ -408,7 +408,7 @@ def add_subscriptions_backend(
message_retention_days: Union[str, int]=REQ(validator=check_string_or_int,
default="realm_default"),
announce: bool=REQ(validator=check_bool, default=False),
principals: Union[Sequence[str], Sequence[int]]=REQ(validator=check_variable_type([
principals: Union[Sequence[str], Sequence[int]]=REQ(validator=check_union([
check_list(check_string), check_list(check_int)]), default=[]),
authorization_errors_fatal: bool=REQ(validator=check_bool, default=True),
) -> HttpResponse:
@ -674,7 +674,7 @@ def update_subscription_properties_backend(
validator=check_list(
check_dict([("stream_id", check_int),
("property", check_string),
("value", check_variable_type([check_string, check_bool]))]),
("value", check_union([check_string, check_bool]))]),
),
),
) -> HttpResponse: