Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
typing: Improve typing with assertions.
PIG208aa9d73c9f6d8
diff --git a/zerver/filters.py b/zerver/filters.pyindex 9f2c0a6b036..eb92f006627 100644--- a/zerver/filters.py+++ b/zerver/filters.py@@ -7,7 +7,9 @@ from django.views.debug import SafeExceptionReporterFilterclass ZulipExceptionReporterFilter(SafeExceptionReporterFilter):def get_post_parameters(self, request: HttpRequest) -> Dict[str, Any]:- filtered_post = SafeExceptionReporterFilter.get_post_parameters(self, request).copy()+ post_data = SafeExceptionReporterFilter.get_post_parameters(self, request)+ assert isinstance(post_data, dict)+ filtered_post = post_data.copy()filtered_vars = ["content","secret",diff --git a/zerver/lib/avatar_hash.py b/zerver/lib/avatar_hash.pyindex 26d015428a4..5a1026c99e0 100644--- a/zerver/lib/avatar_hash.py+++ b/zerver/lib/avatar_hash.py@@ -24,6 +24,7 @@ def user_avatar_hash(uid: str) -> str:# The salt probably doesn't serve any purpose now. In the past we# used a hash of the email address, not the user ID, and we salted# it in order to make the hashing scheme different from Gravatar's.+ assert settings.AVATAR_SALT is not Noneuser_key = uid + settings.AVATAR_SALTreturn make_safe_digest(user_key, hashlib.sha1)diff --git a/zerver/lib/home.py b/zerver/lib/home.pyindex e3c1b07163e..46e2e504b53 100644--- a/zerver/lib/home.py+++ b/zerver/lib/home.py@@ -234,6 +234,7 @@ def build_page_params_for_home_page_load(page_params["narrow_topic"] = narrow_topicpage_params["narrow"] = [dict(operator=term[0], operand=term[1]) for term in narrow]page_params["max_message_id"] = max_message_id+ assert isinstance(page_params["user_settings"], dict)page_params["user_settings"]["enable_desktop_notifications"] = Falsepage_params["translation_data"] = get_language_translation_data(request_language)diff --git a/zerver/lib/import_realm.py b/zerver/lib/import_realm.pyindex db4d6d09991..7c625016471 100644--- a/zerver/lib/import_realm.py+++ b/zerver/lib/import_realm.py@@ -827,6 +827,7 @@ def import_uploads(ExtraArgs={"ContentType": content_type, "Metadata": metadata},)else:+ assert settings.LOCAL_UPLOADS_DIR is not Noneif processing_avatars or processing_emojis or processing_realm_icons:file_path = os.path.join(settings.LOCAL_UPLOADS_DIR, "avatars", relative_path)else:diff --git a/zerver/lib/onboarding.py b/zerver/lib/onboarding.pyindex c744d57c177..53933f92ced 100644--- a/zerver/lib/onboarding.py+++ b/zerver/lib/onboarding.py@@ -105,6 +105,7 @@ def send_initial_pms(user: UserProfile) -> None:def send_welcome_bot_response(send_request: SendMessageRequest) -> None:welcome_bot = get_system_bot(settings.WELCOME_BOT, send_request.message.sender.realm_id)human_recipient_id = send_request.message.sender.recipient_id+ assert human_recipient_id is not Noneif Message.objects.filter(sender=welcome_bot, recipient_id=human_recipient_id).count() < 2:content = (_("Congratulations on your first reply!") + " "diff --git a/zerver/lib/queue.py b/zerver/lib/queue.pyindex ac98ac4fe27..7717eff349b 100644--- a/zerver/lib/queue.py+++ b/zerver/lib/queue.py@@ -17,7 +17,7 @@ from pika.channel import Channelfrom pika.spec import Basicfrom tornado import ioloop-from zerver.lib.utils import statsd+from zerver.lib.utils import assert_is_not_none, statsdMAX_REQUEST_RETRIES = 3ChannelT = TypeVar("ChannelT", Channel, BlockingChannel)@@ -50,7 +50,9 @@ class QueueClient(Generic[ChannelT], metaclass=ABCMeta):raise NotImplementedErrordef _get_parameters(self) -> pika.ConnectionParameters:- credentials = pika.PlainCredentials(settings.RABBITMQ_USERNAME, settings.RABBITMQ_PASSWORD)+ credentials = pika.PlainCredentials(+ settings.RABBITMQ_USERNAME, assert_is_not_none(settings.RABBITMQ_PASSWORD)+ )# With BlockingConnection, we are passed# self.rabbitmq_heartbeat=0, which asks to explicitly disablediff --git a/zerver/lib/remote_server.py b/zerver/lib/remote_server.pyindex 0a33332443a..e3197f02768 100644--- a/zerver/lib/remote_server.py+++ b/zerver/lib/remote_server.py@@ -45,6 +45,7 @@ def send_to_push_bouncer(vs. client-side errors like an invalid token."""+ assert settings.PUSH_NOTIFICATION_BOUNCER_URL is not Noneurl = urllib.parse.urljoin(settings.PUSH_NOTIFICATION_BOUNCER_URL, "/api/v1/remotes/" + endpoint)diff --git a/zerver/tests/test_upload.py b/zerver/tests/test_upload.pyindex 929abaa76a8..6fb394fad06 100644--- a/zerver/tests/test_upload.py+++ b/zerver/tests/test_upload.py@@ -75,6 +75,7 @@ from zerver.models import (def destroy_uploads() -> None:+ assert settings.LOCAL_UPLOADS_DIR is not Noneif os.path.exists(settings.LOCAL_UPLOADS_DIR):shutil.rmtree(settings.LOCAL_UPLOADS_DIR)@@ -833,6 +834,7 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase):fp_path = os.path.split(fp_path_id)[0]response = self.client_get(uri)_get_sendfile.cache_clear()+ assert settings.LOCAL_UPLOADS_DIR is not Nonetest_run, worker = os.path.split(os.path.dirname(settings.LOCAL_UPLOADS_DIR))self.assertEqual(response["X-Accel-Redirect"],@@ -1679,6 +1681,7 @@ class LocalStorageTest(UploadSerializeMixin, ZulipTestCase):emoji_file_name=file_name,)+ assert settings.LOCAL_UPLOADS_DIR is not Nonefile_path = os.path.join(settings.LOCAL_UPLOADS_DIR, "avatars", emoji_path)with get_test_image_file("img.png") as image_file, open(file_path + ".original", "rb"diff --git a/zerver/tests/test_users.py b/zerver/tests/test_users.pyindex 4163aa85a9b..b971a31e470 100644--- a/zerver/tests/test_users.py+++ b/zerver/tests/test_users.py@@ -46,6 +46,7 @@ from zerver.lib.test_helpers import (from zerver.lib.topic_mutes import add_topic_mutefrom zerver.lib.upload import upload_avatar_imagefrom zerver.lib.users import Accounts, access_user_by_id, get_accounts_for_email, user_ids_to_users+from zerver.lib.utils import assert_is_not_nonefrom zerver.models import (CustomProfileField,InvalidFakeEmailDomain,@@ -324,7 +325,7 @@ class PermissionTest(ZulipTestCase):"zerver.lib.events.request_event_queue", return_value=None) as mock_request_event_queue:with self.assertRaises(JsonableError):- result = do_events_register(user, get_client("website"), client_gravatar=True)+ do_events_register(user, get_client("website"), client_gravatar=True)self.assertEqual(mock_request_event_queue.call_args_list[0][0][3], False)# client_gravatar is still turned off for admins. In theory,@@ -1410,7 +1411,7 @@ class ActivateTest(ZulipTestCase):)self.assertEqual(ScheduledEmail.objects.count(), 1)email = ScheduledEmail.objects.all().first()- deliver_scheduled_emails(email)+ deliver_scheduled_emails(assert_is_not_none(email))from django.core.mail import outboxself.assert_length(outbox, 1)@@ -1741,7 +1742,7 @@ class BulkUsersTest(ZulipTestCase):"""self.assertIn("gravatar.com",- get_hamlet_avatar(client_gravatar=False),+ assert_is_not_none(get_hamlet_avatar(client_gravatar=False)),)diff --git a/zerver/tornado/handlers.py b/zerver/tornado/handlers.pyindex 16d2b53f8a5..6e24f3c8f80 100644--- a/zerver/tornado/handlers.py+++ b/zerver/tornado/handlers.py@@ -172,6 +172,7 @@ class AsyncDjangoHandler(tornado.web.RequestHandler, base.BaseHandler):# Mark this handler ID as finished for Zulip's own tracking.clear_handler_by_id(self.handler_id)+ assert isinstance(response, HttpResponse)self.write_django_response_as_tornado_response(response)finally:# Tell Django that we're done processing this request on@@ -261,6 +262,7 @@ class AsyncDjangoHandler(tornado.web.RequestHandler, base.BaseHandler):# Explicitly mark requests as varying by cookie, since the# middleware will not have seen a session accesspatch_vary_headers(response, ("Cookie",))+ assert isinstance(response, HttpResponse)self.write_django_response_as_tornado_response(response)finally:# Tell Django we're done processing this request