votes up 6

Signal receivers must accept keyword arguments (**kwargs).

Package:
django
github stars 59414
Exception Class:
ValueError

Raise code

        # If DEBUG is on, check that we got a good receiver
        if settings.configured and settings.DEBUG:
            assert callable(receiver), "Signal receivers must be callable."

            # Check for **kwargs
            if not func_accepts_kwargs(receiver):
                raise ValueError("Signal receivers must accept keyword arguments (**kwargs).")

        if dispatch_uid:
            lookup_key = (dispatch_uid, _make_id(sender))
        else:
            lookup_key = (_make_id(receiver), _make_id(sender))

        if weak:
😲  Walkingbet is Android app that pays you real bitcoins for a walking. Withdrawable real money bonus is available now, hurry up! 🚶

Ways to fix

votes up 2 votes down

Error Code:

from django.dispatch import Signal, receiver
from django.conf import settings

settings.configure()
settings.configured = True
settings.DEBUG = True

# Create a custom signal
custom_signal = Signal()

@receiver(custom_signal )
def pong(kwargs):
  pass

custom_signal.connect(pong) # <-ValueError: Signal receivers must accept keyword arguments (**kwargs).

Fix code:

from django.dispatch import Signal, receiver
from django.conf import settings

settings.configure()
settings.configured = True
settings.DEBUG = True

# Create a custom signal
custom_signal = Signal()

@receiver(custom_signal)
def pong(**kwargs): # <- You might have missed in this part, where your receiver expecting a variable number of keyword arguments dictionary 
  pass

custom_signal.connect(pong) 

Explanation:

If you see a connect method of Signal class, you'll get-

# Check for **kwargs
if not func_accepts_kwargs(receiver):
	raise ValueError("Signal receivers must accept keyword arguments (**kwargs).")

function func_accepts_kwargs contain this piece of code-

def func_accepts_kwargs(func):
  """Return True if function 'func' accepts keyword arguments **kwargs."""
  return any(
    p for p in _get_callable_parameters(func)
    if p.kind == p.VAR_KEYWORD
  )

This func_accepts_kwargs function is returning True, if func accepts keyword arguments **kwargs, if not then an error will raise.

May 31, 2021 Rna1h answer
Rna1h 1.1k

Add a possible fix

Please authorize to post fix