Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
typing: Add none-checks for db queries.
Zixuan James Lic572d9be5a3a
diff --git a/corporate/lib/stripe.py b/corporate/lib/stripe.pyindex c670e698d23..3ab4917dd11 100644--- a/corporate/lib/stripe.py+++ b/corporate/lib/stripe.py@@ -504,7 +504,9 @@ def make_end_of_cycle_updates_if_needed(standard_plan_last_ledger = (LicenseLedger.objects.filter(plan=standard_plan).order_by("id").last())+ assert standard_plan_last_ledger is not Nonelicenses_for_plus_plan = standard_plan_last_ledger.licenses_at_next_renewal+ assert licenses_for_plus_plan is not Noneplus_plan_ledger_entry = LicenseLedger.objects.create(plan=plus_plan,is_renewal=True,@@ -1040,6 +1042,7 @@ def estimate_annual_recurring_revenue_by_realm() -> Dict[str, int]: # nocoveragif plan.billing_schedule == CustomerPlan.MONTHLY:renewal_cents *= 12# TODO: Decimal stuff+ assert plan.customer.realm is not Noneannual_revenue[plan.customer.realm.string_id] = int(renewal_cents / 100)return annual_revenue@@ -1048,6 +1051,7 @@ def get_realms_to_default_discount_dict() -> Dict[str, Decimal]:realms_to_default_discount: Dict[str, Any] = {}customers = Customer.objects.exclude(default_discount=None).exclude(default_discount=0)for customer in customers:+ assert customer.realm is not Nonerealms_to_default_discount[customer.realm.string_id] = assert_is_not_none(customer.default_discount)@@ -1116,6 +1120,7 @@ def downgrade_small_realms_behind_on_payments_as_needed() -> None:customers = Customer.objects.all().exclude(stripe_customer_id=None)for customer in customers:realm = customer.realm+ assert realm is not None# For larger realms, we generally want to talk to the customer# before downgrading or cancelling invoices; so this logic only applies with 5.@@ -1172,6 +1177,7 @@ def switch_realm_from_standard_to_plus_plan(realm: Realm) -> None:standard_plan_last_renewal_ledger = (LicenseLedger.objects.filter(is_renewal=True, plan=standard_plan).order_by("id").last())+ assert standard_plan_last_renewal_ledger is not Nonestandard_plan_last_renewal_amount = (standard_plan_last_renewal_ledger.licenses * standard_plan.price_per_license)diff --git a/corporate/tests/test_stripe.py b/corporate/tests/test_stripe.pyindex 8d38f4667ed..aa85c08a16f 100644--- a/corporate/tests/test_stripe.py+++ b/corporate/tests/test_stripe.py@@ -583,10 +583,12 @@ class StripeTestCase(ZulipTestCase):else:expected_session_details["type"] = "free_trial_upgrade_from_billing_page"else:+ last_stripe_payment_intent = PaymentIntent.objects.last()+ assert last_stripe_payment_intent is not Noneexpected_session_details["type"] = "upgrade_from_billing_page"expected_session_details["stripe_payment_intent_id"- ] = PaymentIntent.objects.last().stripe_payment_intent_id+ ] = last_stripe_payment_intent.stripe_payment_intent_idself.assert_details_of_valid_session_from_event_status_endpoint(upgrade_json_response.json()["stripe_session_id"], expected_session_detailsdiff --git a/zerver/lib/import_realm.py b/zerver/lib/import_realm.pyindex 3d065217804..727ba7b4a61 100644--- a/zerver/lib/import_realm.py+++ b/zerver/lib/import_realm.py@@ -1029,6 +1029,7 @@ def do_import_realm(import_dir: Path, subdomain: str, processes: int = 1) -> Rea)for realm_emoji in RealmEmoji.objects.filter(realm=realm):if realm_emoji.author_id is None:+ assert first_user_profile is not Nonerealm_emoji.author_id = first_user_profile.idrealm_emoji.save(update_fields=["author_id"])diff --git a/zerver/tests/test_email_mirror.py b/zerver/tests/test_email_mirror.pyindex ef495db19cf..c61e8560caa 100644--- a/zerver/tests/test_email_mirror.py+++ b/zerver/tests/test_email_mirror.py@@ -567,6 +567,7 @@ class TestEmailMirrorMessagesWithAttachments(ZulipTestCase):message = most_recent_message(user_profile)attachment = Attachment.objects.last()+ assert attachment is not Noneself.assertEqual(list(attachment.messages.values_list("id", flat=True)), [message.id])self.assertEqual(message.sender, get_system_bot(settings.EMAIL_GATEWAY_BOT, stream.realm_id)diff --git a/zerver/tests/test_email_notifications.py b/zerver/tests/test_email_notifications.pyindex fc2e183c8e1..899662d22c4 100644--- a/zerver/tests/test_email_notifications.py+++ b/zerver/tests/test_email_notifications.py@@ -338,6 +338,7 @@ class TestFollowupEmails(ZulipTestCase):cordelia = self.example_user("cordelia")enqueue_welcome_emails(self.example_user("cordelia"), realm_creation=True)scheduled_email = ScheduledEmail.objects.filter(users=cordelia).last()+ assert scheduled_email is not Noneself.assertEqual(orjson.loads(scheduled_email.data)["template_prefix"], "zerver/emails/followup_day1")@@ -359,6 +360,7 @@ class TestFollowupEmails(ZulipTestCase):cordelia.realm.save()enqueue_welcome_emails(self.example_user("cordelia"), realm_creation=True)scheduled_email = ScheduledEmail.objects.filter(users=cordelia).last()+ assert scheduled_email is not Noneself.assertEqual(orjson.loads(scheduled_email.data)["template_prefix"], "zerver/emails/followup_day1")diff --git a/zerver/tests/test_import_export.py b/zerver/tests/test_import_export.pyindex 0ce846004c1..d4e13a09c66 100644--- a/zerver/tests/test_import_export.py+++ b/zerver/tests/test_import_export.py@@ -223,6 +223,7 @@ class ExportFile(ZulipTestCase):realm_emoji = RealmEmoji.objects.get(author=user)file_name = realm_emoji.file_name+ assert file_name is not Noneassert file_name.endswith(".png")emoji_path = f"{realm.id}/emoji/images/{file_name}"@@ -510,6 +511,7 @@ class RealmImportExportTest(ExportFile):content="Thumbs up for export",)message = Message.objects.last()+ assert message is not Noneconsented_user_ids = [self.example_user(user).id for user in ["iago", "hamlet"]]do_add_reaction(self.example_user("iago"), message, "outbox", "1f4e4", Reaction.UNICODE_EMOJI@@ -1517,10 +1519,10 @@ class SingleUserExportTest(ExportFile):reaction_type=None,)reaction = Reaction.objects.order_by("id").last()- assert reaction@checkerdef zerver_reaction(records: List[Record]) -> None:+ assert reaction(exported_reaction,) = recordsself.assertEqual(exported_reaction,diff --git a/zerver/tests/test_link_embed.py b/zerver/tests/test_link_embed.pyindex 1e0fa4f4baf..284c835c569 100644--- a/zerver/tests/test_link_embed.py+++ b/zerver/tests/test_link_embed.py@@ -373,6 +373,7 @@ class PreviewTestCase(ZulipTestCase):embedded_link = f'<a href="{url}" title="The Rock">The Rock</a>'msg = Message.objects.select_related("sender").get(id=msg_id)+ assert msg.rendered_content is not Noneself.assertIn(embedded_link, msg.rendered_content)@responses.activate@@ -462,6 +463,7 @@ class PreviewTestCase(ZulipTestCase):# up-to-date event for edited_url.queue_json_publish(*args, **kwargs)msg = Message.objects.select_related("sender").get(id=msg_id)+ assert msg.rendered_content is not Noneself.assertIn(f'<a href="{edited_url}" title="The Rock">The Rock</a>',msg.rendered_content,@@ -821,6 +823,7 @@ class PreviewTestCase(ZulipTestCase):assert cached_data is not Nonemsg = Message.objects.select_related("sender").get(id=msg_id)+ assert msg.rendered_content is not Noneself.assertIn(cached_data.title, msg.rendered_content)assert cached_data.image is not Noneself.assertIn(re.sub(r"([^\w-])", r"\\\1", cached_data.image), msg.rendered_content)@@ -946,6 +949,7 @@ class PreviewTestCase(ZulipTestCase):self.assertEqual(cached_data, mocked_data)msg.refresh_from_db()+ assert msg.rendered_content is not Noneself.assertIn(f'a data-id="{escape(mocked_data.html)}"', msg.rendered_content)@responses.activatediff --git a/zerver/tests/test_markdown.py b/zerver/tests/test_markdown.pyindex 0d172d1d2e0..3c920bf94c2 100644--- a/zerver/tests/test_markdown.py+++ b/zerver/tests/test_markdown.py@@ -1195,6 +1195,7 @@ class MarkdownTest(ZulipTestCase):realm_emoji = RealmEmoji.objects.filter(realm=realm, name="green_tick", deactivated=False).get()+ assert realm_emoji.file_name is not Noneself.assertEqual(converted.rendered_content,"<p>{}</p>".format(emoji_img(":green_tick:", realm_emoji.file_name, realm.id)),diff --git a/zerver/tests/test_message_edit.py b/zerver/tests/test_message_edit.pyindex ac21a480b54..1c9f2945748 100644--- a/zerver/tests/test_message_edit.py+++ b/zerver/tests/test_message_edit.py@@ -28,6 +28,7 @@ from zerver.lib.user_topics import (set_topic_mutes,topic_is_muted,)+from zerver.lib.utils import assert_is_not_nonefrom zerver.models import Message, Realm, Stream, UserMessage, UserProfile, get_realm, get_stream@@ -767,7 +768,7 @@ class EditMessageTest(EditMessageTestCase):},)self.assert_json_success(result)- history = orjson.loads(Message.objects.get(id=msg_id).edit_history)+ history = orjson.loads(assert_is_not_none(Message.objects.get(id=msg_id).edit_history))self.assertEqual(history[0]["prev_content"], "content 1")self.assertEqual(history[0]["user_id"], hamlet.id)self.assertEqual(@@ -788,7 +789,7 @@ class EditMessageTest(EditMessageTestCase):},)self.assert_json_success(result)- history = orjson.loads(Message.objects.get(id=msg_id).edit_history)+ history = orjson.loads(assert_is_not_none(Message.objects.get(id=msg_id).edit_history))self.assertEqual(history[0]["prev_topic"], "topic 1")self.assertEqual(history[0]["topic"], "topic 2")self.assertEqual(history[0]["user_id"], hamlet.id)@@ -805,7 +806,7 @@ class EditMessageTest(EditMessageTestCase):},)self.assert_json_success(result)- history = orjson.loads(Message.objects.get(id=msg_id).edit_history)+ history = orjson.loads(assert_is_not_none(Message.objects.get(id=msg_id).edit_history))self.assertEqual(history[0]["prev_stream"], stream_1.id)self.assertEqual(history[0]["stream"], stream_2.id)self.assertEqual(history[0]["user_id"], self.example_user("iago").id)@@ -820,7 +821,7 @@ class EditMessageTest(EditMessageTestCase):},)self.assert_json_success(result)- history = orjson.loads(Message.objects.get(id=msg_id).edit_history)+ history = orjson.loads(assert_is_not_none(Message.objects.get(id=msg_id).edit_history))self.assertEqual(history[0]["prev_content"], "content 2")self.assertEqual(history[0]["prev_topic"], "topic 2")self.assertEqual(history[0]["topic"], "topic 3")@@ -845,7 +846,7 @@ class EditMessageTest(EditMessageTestCase):},)self.assert_json_success(result)- history = orjson.loads(Message.objects.get(id=msg_id).edit_history)+ history = orjson.loads(assert_is_not_none(Message.objects.get(id=msg_id).edit_history))self.assertEqual(history[0]["prev_content"], "content 3")self.assertEqual(history[0]["user_id"], hamlet.id)@@ -858,7 +859,7 @@ class EditMessageTest(EditMessageTestCase):},)self.assert_json_success(result)- history = orjson.loads(Message.objects.get(id=msg_id).edit_history)+ history = orjson.loads(assert_is_not_none(Message.objects.get(id=msg_id).edit_history))self.assertEqual(history[0]["prev_topic"], "topic 3")self.assertEqual(history[0]["topic"], "topic 4")self.assertEqual(history[0]["prev_stream"], stream_2.id)@@ -878,7 +879,7 @@ class EditMessageTest(EditMessageTestCase):# Now, we verify that all of the edits stored in the message.edit_history# have the correct data structure- history = orjson.loads(Message.objects.get(id=msg_id).edit_history)+ history = orjson.loads(assert_is_not_none(Message.objects.get(id=msg_id).edit_history))self.assertEqual(history[0]["prev_topic"], "topic 3")self.assertEqual(history[0]["topic"], "topic 4")@@ -1588,7 +1589,9 @@ class EditMessageTest(EditMessageTestCase):# Since edit history is being generated by do_update_message,# it's contents can vary over time; So, to keep this test# future proof, we only verify it's length.- self.assert_length(orjson.loads(msg.edit_history), len_edit_history)+ self.assert_length(+ orjson.loads(assert_is_not_none(msg.edit_history)), len_edit_history+ )for msg_id in [id3, id4]:msg = Message.objects.get(id=msg_id)@@ -1642,18 +1645,20 @@ class EditMessageTest(EditMessageTestCase):msg2 = Message.objects.get(id=id2)msg3 = Message.objects.get(id=id3)- msg1_edit_history = orjson.loads(msg1.edit_history)+ msg1_edit_history = orjson.loads(assert_is_not_none(msg1.edit_history))self.assertTrue("prev_content" in msg1_edit_history[0].keys())for msg in [msg2, msg3]:- self.assertFalse("prev_content" in orjson.loads(msg.edit_history)[0].keys())+ self.assertFalse(+ "prev_content" in orjson.loads(assert_is_not_none(msg.edit_history))[0].keys()+ )for msg in [msg1, msg2, msg3]:self.assertEqual(new_topic,msg.topic_name(),)- self.assert_length(orjson.loads(msg.edit_history), 1)+ self.assert_length(orjson.loads(assert_is_not_none(msg.edit_history)), 1)def test_propagate_topic_forward(self) -> None:self.login("hamlet")diff --git a/zerver/tests/test_scim.py b/zerver/tests/test_scim.pyindex 0a6d6c43dc3..94ca9c57769 100644--- a/zerver/tests/test_scim.py+++ b/zerver/tests/test_scim.py@@ -287,6 +287,7 @@ class TestSCIMUser(SCIMTestCase):self.assertEqual(new_user_count, original_user_count + 1)new_user = UserProfile.objects.last()+ assert new_user is not Noneself.assertEqual(new_user.delivery_email, "[email protected]")self.assertEqual(new_user.full_name, "New User")@@ -315,6 +316,7 @@ class TestSCIMUser(SCIMTestCase):self.assertEqual(new_user_count, original_user_count + 1)new_user = UserProfile.objects.last()+ assert new_user is not Noneself.assertEqual(new_user.delivery_email, "[email protected]")self.assertEqual(new_user.full_name, "New User")