typing: Avoid redefinition of incompatible QuerySets.

The pattern of using the same variable to apply filters
or alter the `QuerySet` in other ways might produce `QuerySet`s
with incompatible types. This behavior is not allowed by mypy.

Signed-off-by: Zixuan James Li <p359101898@gmail.com>
This commit is contained in:
Zixuan James Li
2022-06-23 14:21:54 -04:00
committed by Tim Abbott
parent 88f6f3fc0f
commit 6c7b2d621e
7 changed files with 14 additions and 21 deletions

View File

@@ -571,8 +571,9 @@ def delete_user_profile_caches(user_profiles: Iterable["UserProfile"]) -> None:
def delete_display_recipient_cache(user_profile: "UserProfile") -> None:
from zerver.models import Subscription # We need to import here to avoid cyclic dependency.
recipient_ids = Subscription.objects.filter(user_profile=user_profile)
recipient_ids = recipient_ids.values_list("recipient_id", flat=True)
recipient_ids = Subscription.objects.filter(user_profile=user_profile).values_list(
"recipient_id", flat=True
)
keys = [display_recipient_cache_key(rid) for rid in recipient_ids]
keys.append(display_recipient_bulk_get_users_by_id_cache_key(user_profile.id))
cache_delete_many(keys)

View File

@@ -560,10 +560,8 @@ def handle_missedmessage_emails(
usermessage__user_profile_id=user_profile,
id__in=message_ids,
usermessage__flags=~UserMessage.flags.read,
)
# Cancel missed-message emails for deleted messages
messages = [um for um in messages if um.content != "(deleted)"]
# Cancel missed-message emails for deleted messages
).exclude(content="(deleted)")
if not messages:
return

View File

@@ -164,7 +164,7 @@ def fetch_initial_state_data(
# `max_message_id` is primarily used for generating `local_id`
# values that are higher than this. We likely can eventually
# remove this parameter from the API.
user_messages = []
user_messages = None
if user_profile is not None:
user_messages = (
UserMessage.objects.filter(user_profile=user_profile)

View File

@@ -1245,8 +1245,8 @@ def export_partial_message_files(
user_profile_id__in=consented_user_ids
).values_list("recipient_id", flat=True)
recipient_ids = set(public_stream_recipient_ids) | set(consented_recipient_ids)
recipient_ids_for_us = get_ids(response["zerver_recipient"]) & recipient_ids
recipient_ids_set = set(public_stream_recipient_ids) | set(consented_recipient_ids)
recipient_ids_for_us = get_ids(response["zerver_recipient"]) & recipient_ids_set
else:
recipient_ids_for_us = get_ids(response["zerver_recipient"])
# For a full export, we have implicit consent for all users in the export.

View File

@@ -140,10 +140,7 @@ server via `ps -ef` or reading bash history. Prefer
if options["users"] is None:
return []
emails = {email.strip() for email in options["users"].split(",")}
user_profiles = []
for email in emails:
user_profiles.append(self.get_user(email, realm))
return user_profiles
return [self.get_user(email, realm) for email in emails]
def get_user(self, email: str, realm: Optional[Realm]) -> UserProfile:

View File

@@ -177,12 +177,12 @@ def get_status_dict_by_realm(
# a realm with 0 active users.
return {}
mobile_query = query_for_ids(
mobile_query_ids = query_for_ids(
query=mobile_query,
user_ids=user_profile_ids,
field="user_id",
)
mobile_user_ids = set(mobile_query)
mobile_user_ids = set(mobile_query_ids)
return get_status_dicts_for_rows(presence_rows, mobile_user_ids, slim_presence)

View File

@@ -121,11 +121,9 @@ def exclude_topic_mutes(
# by not considering topic mutes outside the stream.
query = query.filter(stream_id=stream_id)
rows = list(
query.values(
"recipient_id",
"topic_name",
)
rows = query.values(
"recipient_id",
"topic_name",
)
if not rows:
@@ -153,7 +151,6 @@ def build_topic_mute_checker(user_profile: UserProfile) -> Callable[[int, str],
"recipient_id",
"topic_name",
)
rows = list(rows)
tups = set()
for row in rows: