Skip to content

API

Everything here is importable from signal_guard. Test helpers are in signal_guard.testing.

Allowing what is intended

signal_guard.allow_bypass

allow_bypass(*models: type[Model], reason: str)

Allow bulk writes that skip receivers, and record why.

with allow_bypass(Order, reason="import reindexes in one batch"):
    Order.objects.bulk_create(rows)

With no models it allows every model. Works as a decorator on sync functions and as async with in async code.

signal_guard.allow_side_effect

allow_side_effect(*, reason: str)

Allow side effects before the commit, and record why.

For the rare side effect that must happen inside the transaction, such as a vendor call whose refusal has to roll the delete back:

@receiver(pre_delete, sender=Subscription)
@allow_side_effect(reason="a refused cancel must roll the delete back")
def cancel_at_vendor(sender, instance, **kwargs):
    vendor.cancel(instance.vendor_id)

Works as a context manager, a decorator on sync functions and as async with in async code.

signal_guard.bulk_safe

bulk_safe(func: F) -> F

Mark a receiver as safe to skip during bulk writes.

Use it for receivers whose work does not matter for bulk writes, such as a cache warm-up that a nightly job redoes anyway. Order relative to @receiver does not matter.

Receivers

signal_guard.receiver_on_commit

receiver_on_commit(
    signal: Signal | list[Signal] | tuple[Signal, ...],
    *,
    using: str | None = None,
    robust: bool = False,
    **connect_kwargs: Any,
) -> Callable[[F], F]

Connect a receiver that runs after the current transaction commits.

@receiver_on_commit(post_save, sender=Order)
def send_confirmation(sender, instance, created, **kwargs):
    if created:
        send_order_email.delay(instance.pk)

Works like @receiver: it takes the same sender, weak and dispatch_uid arguments, and the receiver gets the same arguments the signal sent. The call is handed to transaction.on_commit() on the database the signal names in using (model signals do), else the using given here, else the default database. Outside a transaction it runs at once, as on_commit() does. If the transaction rolls back, it never runs.

robust=True is passed on to on_commit(): an exception in this receiver is logged instead of stopping the other commit callbacks.

The decorated function is returned unchanged, so it can still be called directly in tests.

Exceptions and warnings

signal_guard.SignalGuardError

Base class for errors raised by django-signal-guard.

signal_guard.SignalBypassError

SignalBypassError(
    message: str,
    *,
    model: type[Model],
    method: str,
    receivers: tuple[SkippedReceiver, ...],
)

A bulk write would skip pre_save or post_save receivers.

Raised before the write reaches the database, so nothing is written.

signal_guard.SignalBypassWarning

Warning category used when SIGNAL_GUARD['BYPASS'] == 'warn'.

signal_guard.SignalRecursionError

SignalRecursionError(
    message: str, *, chain: tuple[Hop, ...]
)

A receiver re-entered itself for the same object a second time.

chain lists each signal sent along the way, outermost first.

signal_guard.SignalRecursionWarning

Warning category used when SIGNAL_GUARD['RECURSION'] == 'warn'.

signal_guard.SideEffectBeforeCommitError

SideEffectBeforeCommitError(
    message: str, *, call: str, receiver: str | None
)

A receiver caused a side effect inside a transaction that has not committed.

Raised before the side effect happens, so nothing is sent.

signal_guard.SideEffectBeforeCommitWarning

Warning category used when SIGNAL_GUARD['SIDE_EFFECTS'] == 'warn'.

Details carried by exceptions

SkippedReceiver dataclass

SkippedReceiver(
    signal_name: str,
    path: str,
    location: str | None,
    module: str = "",
    any_sender: bool = False,
    create_only: bool = False,
)

A receiver a bulk write skips, as listed in SignalBypassError.receivers.

path is its dotted path, location the file:line it is defined at, when known.

signal_guard.tracing.Hop dataclass

Hop(
    signal_name: str,
    target: str,
    receiver: str | None,
    receiver_location: str | None,
    call: str | None,
    sent_at: str | None,
)

One signal sent along a recursion chain, in SignalRecursionError.chain.

target names the object, such as shop.Order #12. receiver is the receiver that sent the next signal, call how (such as Order.save()) and sent_at the file:line it did so from. These are None for the last hop, and when the receiver could not be found.