Reputation: 12417
Why do I get the following error, and how do I resolve it?
TypeError: super(type, obj): obj must be an instance or subtype of type
Upvotes: 90
Views: 197031
Reputation: 8864
TypeError: super(type, obj): obj must be an instance or subtype of type
is also raised (erronously) when calling super()
in a decorated method of a slotted dataclass:
from dataclasses import dataclass
def decorated(method):
def wrapped(self, *args, **kwargs):
return method(self, *args, **kwargs)
return wrapped
@dataclass(slots=True)
class A:
def f(self):
pass
@dataclass(slots=True)
class B(A):
@decorated
def f(self):
super().f()
B().f()
The same happens when using attrs instead of dataclasses, see https://github.com/python-attrs/attrs/issues/1038.
The issue is due to cell rewriting for slotted classes in attrs and dataclasses.
The workaround is to either use super(B, self)
or not use slots.
Upvotes: 2
Reputation: 31
Simply restart the kernel if it cannot fetch the values. It worked with this code:
import datetime as dt
start = dt.datetime(2018,1,2)
end = dt.datetime(2022,12,29)
import pandas_datareader.data as pdr
stk_tickers = ['AAPL','MSFT','AMZN','TSLA','GOOG']
ccy_tickers = ['DEXJPUS','DEXUSUK']
idx_tickers = ['SP500','DJIA','VIXCLS']
yfin.pdr_override()
stk_data = pdr.get_data_yahoo(stk_tickers,start,end)
ccy_data = pdr.get_data_fred(ccy_tickers,start,end)
idx_data = pdr.get_data_fred(idx_tickers,start,end)
Upvotes: 2
Reputation: 147
class A_net(nn.Module):
def __init__(self):
super(A_net, self).__init__()
# define layers and architecture
def forward(self, x):
# define forward pass
The class name should match the written class inside the super function. In this case, A_net is the class. It works for me.
Upvotes: 0
Reputation: 786
So I just pasted in a form in forms.py.
I just made a fast look to see if I needed to change anything, but I didn't see that.
Then I got this super(type, obj): obj must be an instance or subtype of type error, so I searched for it on the browser, but before I checked any of the answers I looked one more time and this time I spotted the issue.
As you can see, many answers on this question says it was wrong with the super
. Yes it was the same issue for me.
make sure that you look if you have any super
and see if the class added matches with the class. At least that's what I did.
Before and After
Where I spotted it in my code
forms.py
Before:
class userProfileForm(ModelForm):
class Meta:
model = user_profile
fields = ("user", "rating", )
def __init__(self, *args, **kwargs):
# https://stackoverflow.com/a/6866387/15188026
hide_condition = kwargs.pop('hide_condition',None)
super(ProfileForm, self).__init__(*args, **kwargs)
if hide_condition:
self.fields['user'].widget = HiddenInput()
After:
class userProfileForm(ModelForm):
class Meta:
model = user_profile
fields = ("user", "rating", )
def __init__(self, *args, **kwargs):
# https://stackoverflow.com/a/6866387/15188026
hide_condition = kwargs.pop('hide_condition',None)
super(userProfileForm, self).__init__(*args, **kwargs)
if hide_condition:
self.fields['user'].widget = HiddenInput()
You see that the super got changed to the class name
Upvotes: 1
Reputation: 61
Similar to @Eldamir, I solved it by realizing I had written two classes with the same name, and the second one was overwriting the first.
If that's the case, change the name of one of the classes.
Upvotes: 1
Reputation: 97
This error also pops out when you simply do not instantiate child class , and try to call a method on a class itself, like in :
class Parent:
def method():
pass
class Child(Parent):
def method():
super().method()
P = Parent()
C = Child
C.method()
Upvotes: 4
Reputation: 341
The best solution that I have found for this problem is only available using python 3. You then don't need to specify the arguments of "super", then you won't have the error any more writing your class like this :
class D:
pass
class C(D):
def __init__(self):
super().__init__()# no arguments given to super()
Upvotes: 8
Reputation: 2067
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.
Upvotes: 188
Reputation: 6549
For Jupyter only
You can get his issue in because reload
logic have some bugs (issue)
Here is a simple solution/workaround that works for me until issue is not fixed
1001xx
at the bottom of the file which you call in the cell Upvotes: 9
Reputation: 1403
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()
Upvotes: 32
Reputation: 78556
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)
Upvotes: 38
Reputation: 10062
Another interesting way is if a merge of branches has duplicated the class, so that in the file you have two definitions for the same name, e.g.
class A(Foo):
def __init__(self):
super(A, self).__init__()
#...
class A(Foo):
def __init__(self):
super(A, self).__init__()
#...
If you try to create an instance from a static reference to the first definition of A, once it tries to call super
, inside the __init__
method, A
will refer to the second definition of A
, since it has been overwritten. The solution - ofcourse - is to remove the duplicate definition of the class, so it doesn't get overwritten.
This may seem like something that would never happen, but it just happened to me, when I wasn't paying close enough attention to the merge of two branches. My tests failed with the error message described in the question, so I thought I'd leave my findings here, even though it doesn't exactly answer the specific question.
Upvotes: 7