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

Another way this error can occur is when you reload the module with the class in a Jupiter notebook.

Easy solution is to restart the kernel.

http://thomas-cokelaer.info/blog/2011/09/382/

Check out @Mike W's answer for more detail.


You should call super using the UrlManager class as first argument not the URL model. super cannot called be with an unrelated class/type:

From the docs,

super(type[, object-or-type]): Return a proxy object that delegates method calls to a parent or sibling class of type.

So you cannot do:

>>> class D:
...    pass
... 
>>> class C:
...    def __init__(self):
...        super(D, self).__init__()
... 
>>> C()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in __init__
TypeError: super(type, obj): obj must be an instance or subtype of type

You should do:

qs_main = super(UrlManager, self).all(*args, **kwargs)

Or in Python 3:

qs_main = super().all(*args, **kwargs)

Elaborating in @Oğuz Şerbetci's answer, in python3 (not necessary only in Jupyter), when there is the need to reload a library, for example we have class Parent and class Child defined as

class Parent(object):
    def __init__(self):
        # do something

class Child(Parent):
    def __init__(self):
        super(Child, self).__init__(self)

then if you do this

import library.Child
reload(library)

Child()

you will get TypeError: super(type, obj): obj must be an instance or subtype of type, the solution is just to re import the class after the reload

import library.Child
reload(library)
import library.Child

Child()

Tags:

Python

Django