开发者

Can someone explain what exactly this error means,TypeError: issubclass() arg 1 must be a class

I have zer开发者_StackOverflow中文版o idea as to why I'm getting this error.


as people said, the 2 arguments of issubclass() should be classes, not instances of an object.

consider this sample:

>>> issubclass( 1, int )
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: issubclass() arg 1 must be a class
>>> issubclass( type(1), int )
True    
>>> isinstance( 1, int )
True

the key is the use of the type() function to get the type of an instance for use with the issubclass() function, which, as noted in another comment, is equivalent to calling isinstance()


It means that you don't provide a class as argument for issubclass(). Both arguments have to be classes. Second argument can also be a tuple of classes.

If you show the code that throws this error, we can help further.


From the documentation:

issubclass(class, classinfo)
Return true if class is a subclass (direct or indirect) of classinfo. A class is considered a subclass of itself. classinfo may be a tuple of class objects, in which case every entry in classinfo will be checked. In any other case, a TypeError exception is raised.


When you use "=" instead of ":" declaring an attibute of class, you get the error: TypeError: issubclass() arg 1 must be a class. The number 1 say you that you have the error in the first argument

this is incorrect:

class AnyClass(BaseClass):
    email = str

this is correct:

class AnyClass(BaseClass):
    email : str


The first argument to issubclass() needs to be of type "class".

http://pyref.infogami.com/issubclass


Basically this method tells you if the first parameter is a subclass of the second. So naturally, both your parameters need to be classes. It appears from your call, that you have called issubclass without any parameters, which confuses the interpreter.

Calling issubclass is like asking the interpreter: "Hey! is this class a subclass of this other class?". However, since you have not provided two classes, you have essentially asked the interpretter: "Hey! I'm not going to show you anything, but tell me if this it's a subclass". This confuses the interpreter and that's why you get this error.


For those using Pydantic or FastAPI and having problems with this error. Here is the answer https://stackoverflow.com/a/70384637/7335848


This error message don´t say where is the real problem, for example in my case the first argument of the class was the right one,the problem was I was using a Literal typing in class property declaration. No errors from linter, no errors anywhere but for cause of this, my class was not able to be converted to another class that was needed in runtime. Just removing this Literal type fixed my error.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜