From d2af20eb2ad3d03e5d8babe0ba1257963a463a36 Mon Sep 17 00:00:00 2001 From: PIG208 <359101898@qq.com> Date: Mon, 26 Jul 2021 22:46:53 +0800 Subject: [PATCH] typing: Do explicit type conversion when appropriate. When calling some functions or assigning values to certain attributes, the arguments/right operand do not match the exact type that the functions/attributes expect, and thus we fix that by converting types beforehand. --- analytics/tests/test_stats_views.py | 6 +++++- analytics/views/activity_common.py | 2 +- corporate/tests/test_stripe.py | 4 ++-- zerver/lib/actions.py | 2 +- zilencer/management/commands/populate_db.py | 7 ++++--- 5 files changed, 13 insertions(+), 8 deletions(-) diff --git a/analytics/tests/test_stats_views.py b/analytics/tests/test_stats_views.py index fcc36087ca..67e403495b 100644 --- a/analytics/tests/test_stats_views.py +++ b/analytics/tests/test_stats_views.py @@ -207,7 +207,11 @@ class TestGetChartData(ZulipTestCase): client2 = Client.objects.create(name="client 2") client3 = Client.objects.create(name="client 3") client4 = Client.objects.create(name="client 4") - self.insert_data(stat, [client4.id, client3.id, client2.id], [client3.id, client1.id]) + self.insert_data( + stat, + [str(client4.id), str(client3.id), str(client2.id)], + [str(client3.id), str(client1.id)], + ) result = self.client_get( "/json/analytics/chart_data", {"chart_name": "messages_sent_by_client"} ) diff --git a/analytics/views/activity_common.py b/analytics/views/activity_common.py index cca5e154c9..d752b01ddd 100644 --- a/analytics/views/activity_common.py +++ b/analytics/views/activity_common.py @@ -110,7 +110,7 @@ def get_user_activity_summary(records: List[QuerySet]) -> Dict[str, Dict[str, An for record in records: client = record.client.name - query = record.query + query = str(record.query) update("use", record) diff --git a/corporate/tests/test_stripe.py b/corporate/tests/test_stripe.py index 0d63c04e53..12a5b5b4be 100644 --- a/corporate/tests/test_stripe.py +++ b/corporate/tests/test_stripe.py @@ -3237,12 +3237,12 @@ class BillingHelpersTest(ZulipTestCase): def test_get_realms_to_default_discount_dict(self) -> None: Customer.objects.create(realm=get_realm("zulip"), stripe_customer_id="cus_1") lear_customer = Customer.objects.create(realm=get_realm("lear"), stripe_customer_id="cus_2") - lear_customer.default_discount = 30 + lear_customer.default_discount = Decimal(30) lear_customer.save(update_fields=["default_discount"]) zephyr_customer = Customer.objects.create( realm=get_realm("zephyr"), stripe_customer_id="cus_3" ) - zephyr_customer.default_discount = 0 + zephyr_customer.default_discount = Decimal(0) zephyr_customer.save(update_fields=["default_discount"]) self.assertEqual( diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py index 0bfd7fd91e..b4cab794b8 100644 --- a/zerver/lib/actions.py +++ b/zerver/lib/actions.py @@ -466,7 +466,7 @@ def process_new_human_user( mit_beta_user = realm.is_zephyr_mirror_realm if prereg_user is not None: - streams = prereg_user.streams.all() + streams: List[Stream] = list(prereg_user.streams.all()) acting_user: Optional[UserProfile] = prereg_user.referred_by else: streams = [] diff --git a/zilencer/management/commands/populate_db.py b/zilencer/management/commands/populate_db.py index c7fd1dc2e0..f0e14544e6 100644 --- a/zilencer/management/commands/populate_db.py +++ b/zilencer/management/commands/populate_db.py @@ -9,6 +9,7 @@ import bmemcached import orjson from django.conf import settings from django.contrib.sessions.models import Session +from django.core.files.base import File from django.core.management import call_command from django.core.management.base import BaseCommand, CommandParser from django.db import connection @@ -557,7 +558,7 @@ class Command(BaseCommand): # across platforms. subscriptions_list: List[Tuple[UserProfile, Recipient]] = [] - profiles: Sequence[UserProfile] = ( + profiles: Sequence[UserProfile] = list( UserProfile.objects.select_related().filter(is_bot=False).order_by("email") ) @@ -716,7 +717,7 @@ class Command(BaseCommand): # Create a test realm emoji. IMAGE_FILE_PATH = static_path("images/test-images/checkbox.png") with open(IMAGE_FILE_PATH, "rb") as fp: - check_add_realm_emoji(zulip_realm, "green_tick", iago, fp) + check_add_realm_emoji(zulip_realm, "green_tick", iago, File(fp)) if not options["test_suite"]: # Populate users with some bar data @@ -1018,7 +1019,7 @@ def generate_and_send_messages( elif message_type == Recipient.STREAM: # Pick a random subscriber to the stream message.sender = random.choice( - Subscription.objects.filter(recipient=message.recipient) + list(Subscription.objects.filter(recipient=message.recipient)) ).user_profile message.subject = random.choice(possible_topics[message.recipient.id]) saved_data["subject"] = message.subject