Python super() behavior not dependable

Thomas Thorogood picture Thomas Thorogood · Mar 15, 2012 · Viewed 17.6k times · Source

For some reason, the super() method is not always behaving as expected, opting to return:

TypeError('super(type, obj): obj must be an instance or subtype of type)'

I understand what the error means. I do not understand why it is coming up as an error. Here's the snippet of code that is breaking. All objects in the system are new style objects.

What's really interesting is that this error does not always show up. I don't know what's causing it. The super() method in Retrieval is passing the Retrieval class, and then itself as an object, which is, as far as I'm aware,exactly how super() is supposed to be invoked.

Any thoughts at all?

In file DBConnection.py:

class DBAdminConnection(object):
    def __init__(self):
        self.user = DBUserConnection().user 
        self.submissions = DBSubmissionConnection()

In file Retrieval.py

class Retrieval(DBConnection.DBAdminConnection): 
    def __init__(self, username=None, password=None, unique_key=None):
        super(Retrieval,self).__init__()
        if username and password:
            self.username = username
            self.user.login(username,password, config.DATABASE)
            if self.user.error:
                raise UserLoginError(username)
        self.unique_key = unique_key

Answer

Eduardo Ivanec picture Eduardo Ivanec · Mar 15, 2012

Are you reloading modules somehow in the middle of things? If so, that may explain this error.

isinstance(self,DBAdminConnection) may become false after reloading modules because of the changes to memory references, apparently.

Edit: if you're running your web.py app under mod_wsgi, make sure you're disabling autoreload:

app = web.application(urls, globals(), autoreload=False)