Search code examples
djangodjango-signals

django signals: fail silently? any better way of debugging mistakes?


It seems that django signals has a "fail silently" paradigm.

When I make a small spelling error in my signals function, for example:-

def new_users_handler(send, user, response, details, **kwargs):
    print "new_users_handler function executes"
    user.is_new = True
    if user.is_new:
        if "id" in response:
            from urllib2 import urlopen, HTTPError
            from django.template.defaultfilters import slugify
            from django.core.files.base import ContentFile

            try:
                url = None
                if sender == FacebookBackend:
                    url = "http://graph.facebook.com/%s/picture?type=large" \
                            % response["id"]
                elif sender == google.GoogleOAuth2Backend and "picture" in response:
                    url = response["picture"]
    ......
    socialauth_registered.connect(new_users_handler, sender=None)

I use "send" as my argument instead of "sender", I don't really get any useful error message or debug information in my devserver stdout.

Is there a good way of making sure that signal failures/error messages get shown loud and clear?

In my example above, this could have been a "5 minutes" fix if there was a proper error message telling me that

name "sender" is not defined

but instead, there wasn't any error messages and I was looking everywhere in my code base to try to figure out why my signals isn't getting called... not cool.

Any advice welcome!


Solution

  • Not quite what you are asking for, but your problem could also have been solved with a static analyser like pyflakes.

    From pypi:

    Pyflakes is program to analyze Python programs and detect various errors. It works by parsing the source file, not importing it, so it is safe to use on modules with side effects. It's also much faster.

    sample output:

    tmp.py:9: 'ContentFile' imported but unused
    tmp.py:13: undefined name 'sender'
    

    I have it integrated into my editor (vim, but i've also seen it in several others), highlighting my typos as i go, or on save.