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:
😲 Agile task management is now easier than calling a taxi. #Tracklify
🙏 Scream for help to Ukraine
Today, 14th August 2022, Russia continues bombing and firing Ukraine. Don't trust Russia, they are bombing us and brazenly lying in same time they are not doing this 😠, civilians and children are dying too! We are screaming and asking exactly you to help us, we want to survive, our families, children, older ones.
Please spread the information, and ask your governemnt to stop Russia by any means. We promise to work extrahard after survival to make the world safer place for all.

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