Skip to content

Commit

Permalink
message_send: Remove TypeGuard.
Browse files Browse the repository at this point in the history
The type safety of a TypeGuard is unchecked by mypy.  While this
particular TypeGuard is safe given the current context, one could
imagine future changes that make it unsafe, so it’s preferable to
avoid unchecked constructs whenever possible.

Signed-off-by: Anders Kaseorg <[email protected]>
  • Loading branch information
andersk authored and timabbott committed Jul 16, 2022
1 parent 39efe6f commit 7d8be67
Showing 1 changed file with 7 additions and 16 deletions.
23 changes: 7 additions & 16 deletions zerver/actions/message_send.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
Collection,
Dict,
List,
Literal,
Optional,
Sequence,
Set,
Expand All @@ -27,7 +26,6 @@
from django.utils.timezone import now as timezone_now
from django.utils.translation import gettext as _
from django.utils.translation import override as override_language
from typing_extensions import TypeGuard

from zerver.actions.uploads import do_claim_attachments
from zerver.lib.addressee import Addressee
Expand Down Expand Up @@ -171,24 +169,16 @@ class RecipientInfoResult(TypedDict):
all_bot_user_ids: Set[int]


class BaseActiveUserDict(TypedDict):
class ActiveUserDict(TypedDict):
id: int
enable_online_push_notifications: bool
enable_offline_email_notifications: bool
enable_offline_push_notifications: bool
long_term_idle: bool


class ActiveUserDict(BaseActiveUserDict):
is_bot: bool
bot_type: Optional[int]


class ActiveBotUserDict(BaseActiveUserDict):
is_bot: Literal[True]
bot_type: int


def get_recipient_info(
*,
realm_id: int,
Expand Down Expand Up @@ -343,9 +333,6 @@ def get_ids_for(f: Callable[[ActiveUserDict], bool]) -> Set[int]:
"""Only includes users on the explicit message to line"""
return {row["id"] for row in rows if f(row)} & message_to_user_id_set

def is_service_bot(row: ActiveUserDict) -> TypeGuard[ActiveBotUserDict]:
return row["is_bot"] and (row["bot_type"] in UserProfile.SERVICE_BOT_TYPES)

active_user_ids = get_ids_for(lambda r: True)
online_push_user_ids = get_ids_for(
lambda r: r["enable_online_push_notifications"],
Expand All @@ -363,7 +350,7 @@ def is_service_bot(row: ActiveUserDict) -> TypeGuard[ActiveBotUserDict]:

# Service bots don't get UserMessage rows.
um_eligible_user_ids = get_ids_for(
lambda r: not is_service_bot(r),
lambda r: not r["is_bot"] or r["bot_type"] not in UserProfile.SERVICE_BOT_TYPES,
)

long_term_idle_user_ids = get_ids_for(
Expand All @@ -384,7 +371,11 @@ def is_service_bot(row: ActiveUserDict) -> TypeGuard[ActiveBotUserDict]:
row["id"] for row in rows if row["is_bot"] and row["bot_type"] == UserProfile.DEFAULT_BOT
}

service_bot_tuples = [(row["id"], row["bot_type"]) for row in rows if is_service_bot(row)]
service_bot_tuples = [
(row["id"], row["bot_type"])
for row in rows
if row["is_bot"] and row["bot_type"] in UserProfile.SERVICE_BOT_TYPES
]

# We also need the user IDs of all bots, to avoid trying to send push/email
# notifications to them. This set will be directly sent to the event queue code
Expand Down

0 comments on commit 7d8be67

Please sign in to comment.