From 63e9ae83896d7281fd969bb572e7887883d07e01 Mon Sep 17 00:00:00 2001 From: Zixuan James Li <359101898@qq.com> Date: Sun, 29 May 2022 15:52:25 -0400 Subject: [PATCH] typing: Apply trivial fixes to adjust edge cases in typing. Add none-checks, rename variables (to avoid redefinition of the same variable with different types error), add necessary type annotations. This is a part of #18777. Signed-off-by: Zixuan James Li <359101898@qq.com> --- tools/lib/provision_inner.py | 1 + zerver/actions/realm_linkifiers.py | 1 + zerver/actions/streams.py | 3 ++- zerver/context_processors.py | 4 ++-- zerver/data_import/rocketchat.py | 6 +++--- zerver/lib/streams.py | 4 ++-- zerver/models.py | 1 + zerver/tests/test_auth_backends.py | 1 + zerver/tests/test_import_export.py | 4 +++- zerver/tests/test_message_send.py | 1 + 10 files changed, 17 insertions(+), 9 deletions(-) diff --git a/tools/lib/provision_inner.py b/tools/lib/provision_inner.py index b2091b57f5..67474fea40 100755 --- a/tools/lib/provision_inner.py +++ b/tools/lib/provision_inner.py @@ -270,6 +270,7 @@ def main(options: argparse.Namespace) -> int: destroy_leaked_test_databases, ) + assert settings.RABBITMQ_PASSWORD is not None if options.is_force or need_to_run_configure_rabbitmq([settings.RABBITMQ_PASSWORD]): run_as_root(["scripts/setup/configure-rabbitmq"]) write_new_digest( diff --git a/zerver/actions/realm_linkifiers.py b/zerver/actions/realm_linkifiers.py index d208f35912..ea1485fbd8 100644 --- a/zerver/actions/realm_linkifiers.py +++ b/zerver/actions/realm_linkifiers.py @@ -44,6 +44,7 @@ def do_remove_linkifier( if pattern is not None: RealmFilter.objects.get(realm=realm, pattern=pattern).delete() else: + assert id is not None RealmFilter.objects.get(realm=realm, id=id).delete() notify_linkifiers(realm) diff --git a/zerver/actions/streams.py b/zerver/actions/streams.py index 912ee8a7af..28999f6f81 100644 --- a/zerver/actions/streams.py +++ b/zerver/actions/streams.py @@ -979,7 +979,8 @@ def do_rename_stream(stream: Stream, new_name: str, user_profile: UserProfile) - ).decode(), ) - recipient_id = stream.recipient_id + assert stream.recipient_id is not None + recipient_id: int = stream.recipient_id messages = Message.objects.filter(recipient_id=recipient_id).only("id") # Update the display recipient and stream, which are easy single diff --git a/zerver/context_processors.py b/zerver/context_processors.py index 3e0278e4ae..34776bf9b7 100644 --- a/zerver/context_processors.py +++ b/zerver/context_processors.py @@ -1,4 +1,4 @@ -from typing import Any, Dict, Optional +from typing import Any, Dict, Mapping, Optional from urllib.parse import urljoin from django.conf import settings @@ -29,7 +29,7 @@ from zproject.backends import ( require_email_format_usernames, ) -DEFAULT_PAGE_PARAMS = { +DEFAULT_PAGE_PARAMS: Mapping[str, Any] = { "development_environment": settings.DEVELOPMENT, "webpack_public_path": staticfiles_storage.url(settings.WEBPACK_BUNDLES), } diff --git a/zerver/data_import/rocketchat.py b/zerver/data_import/rocketchat.py index 8e91d06783..c4cd14a9d4 100644 --- a/zerver/data_import/rocketchat.py +++ b/zerver/data_import/rocketchat.py @@ -326,9 +326,9 @@ def build_reactions( # For the Unicode emoji codes, we use equivalent of # function 'emoji_name_to_emoji_code' in 'zerver/lib/emoji' here - for reaction in reactions: - emoji_name = reaction["name"] - user_id = reaction["user_id"] + for reaction_dict in reactions: + emoji_name = reaction_dict["name"] + user_id = reaction_dict["user_id"] # Check in realm emoji if emoji_name in realmemoji: emoji_code = realmemoji[emoji_name] diff --git a/zerver/lib/streams.py b/zerver/lib/streams.py index f2abbd4a6b..df8295621c 100644 --- a/zerver/lib/streams.py +++ b/zerver/lib/streams.py @@ -515,10 +515,10 @@ def public_stream_user_ids(stream: Stream) -> Set[int]: guest_subscriptions = get_active_subscriptions_for_stream_id( stream.id, include_deactivated_users=False ).filter(user_profile__role=UserProfile.ROLE_GUEST) - guest_subscriptions = { + guest_subscriptions_ids = { sub["user_profile_id"] for sub in guest_subscriptions.values("user_profile_id") } - return set(active_non_guest_user_ids(stream.realm_id)) | guest_subscriptions + return set(active_non_guest_user_ids(stream.realm_id)) | guest_subscriptions_ids def can_access_stream_user_ids(stream: Stream) -> Set[int]: diff --git a/zerver/models.py b/zerver/models.py index b82e31f5d1..e15ffe90bd 100644 --- a/zerver/models.py +++ b/zerver/models.py @@ -2703,6 +2703,7 @@ def get_huddle_recipient(user_profile_ids: Set[int]) -> Recipient: # we hit another cache to get the recipient. We may want to # unify our caching strategy here. huddle = get_huddle(list(user_profile_ids)) + assert huddle.recipient is not None return huddle.recipient diff --git a/zerver/tests/test_auth_backends.py b/zerver/tests/test_auth_backends.py index c4bce484ed..f8e1980237 100644 --- a/zerver/tests/test_auth_backends.py +++ b/zerver/tests/test_auth_backends.py @@ -6724,6 +6724,7 @@ class EmailValidatorTestCase(ZulipTestCase): "fred+5555@zulip.com", get_realm_email_validator(realm), ) + assert error is not None self.assertIn("containing + are not allowed", error) cordelia_email = cordelia.delivery_email diff --git a/zerver/tests/test_import_export.py b/zerver/tests/test_import_export.py index 206686d601..649dcde1df 100644 --- a/zerver/tests/test_import_export.py +++ b/zerver/tests/test_import_export.py @@ -905,7 +905,9 @@ class RealmImportExportTest(ExportFile): # test recipients def get_recipient_stream(r: Realm) -> Recipient: - return Stream.objects.get(name="Verona", realm=r).recipient + recipient = Stream.objects.get(name="Verona", realm=r).recipient + assert recipient is not None + return recipient def get_recipient_user(r: Realm) -> Recipient: return UserProfile.objects.get(full_name="Iago", realm=r).recipient diff --git a/zerver/tests/test_message_send.py b/zerver/tests/test_message_send.py index 6409af571c..2f718cf0de 100644 --- a/zerver/tests/test_message_send.py +++ b/zerver/tests/test_message_send.py @@ -1544,6 +1544,7 @@ class StreamMessagesTest(ZulipTestCase): stream = get_stream("Denmark", realm) topic_name = "lunch" recipient = stream.recipient + assert recipient is not None sending_client = make_client(name="test suite") for i in range(num_extra_users):