django_api: Extract send_event_on_commit helper.

django-stubs 4.2.1 gives transaction.on_commit a more accurate type
annotation, but this exposed that mypy can’t handle the lambda default
parameters that we use to recapture loop variables such as

    for stream_id in public_stream_ids:
        peer_user_ids = …
        event = …

        transaction.on_commit(
            lambda event=event, peer_user_ids=peer_user_ids: send_event(
                realm, event, peer_user_ids
            )
        )

https://github.com/python/mypy/issues/15459

A workaround that mypy accepts is

        transaction.on_commit(
            (
                lambda event, peer_user_ids: lambda: send_event(
                    realm, event, peer_user_ids
                )
            )(event, peer_user_ids)
        )

But that’s kind of ugly and potentially error-prone, so let’s make a
helper function for this very common pattern.

        send_event_on_commit(realm, event, peer_user_ids)

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg
2023-06-17 11:53:07 -07:00
committed by Tim Abbott
parent 77c146b8b0
commit 7657cb4a0f
22 changed files with 114 additions and 180 deletions

View File

@@ -48,7 +48,7 @@ from zerver.models import (
bot_owner_user_ids,
get_system_bot,
)
from zerver.tornado.django_api import send_event
from zerver.tornado.django_api import send_event_on_commit
if settings.BILLING_ENABLED:
from corporate.lib.stripe import update_license_ledger_if_needed
@@ -311,20 +311,12 @@ def notify_created_user(user_profile: UserProfile) -> None:
event: Dict[str, Any] = dict(
type="realm_user", op="add", person=person_for_real_email_access_users
)
transaction.on_commit(
lambda event=event: send_event(
user_profile.realm, event, user_ids_with_real_email_access
)
)
send_event_on_commit(user_profile.realm, event, user_ids_with_real_email_access)
if user_ids_without_real_email_access:
assert person_for_without_real_email_access_users is not None
event = dict(type="realm_user", op="add", person=person_for_without_real_email_access_users)
transaction.on_commit(
lambda event=event: send_event(
user_profile.realm, event, user_ids_without_real_email_access
)
)
send_event_on_commit(user_profile.realm, event, user_ids_without_real_email_access)
def created_bot_event(user_profile: UserProfile) -> Dict[str, Any]:
@@ -361,9 +353,7 @@ def created_bot_event(user_profile: UserProfile) -> Dict[str, Any]:
def notify_created_bot(user_profile: UserProfile) -> None:
event = created_bot_event(user_profile)
transaction.on_commit(
lambda: send_event(user_profile.realm, event, bot_owner_user_ids(user_profile))
)
send_event_on_commit(user_profile.realm, event, bot_owner_user_ids(user_profile))
def do_create_user(