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

Question:

I have zero idea as to why I’m getting this error.

Asked By: gizgok

||

Answers:

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.

Answered By: Felix Kling

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

http://pyref.infogami.com/issubclass

Answered By: James Sumners

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()

Answered By: Adrien Plisson

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.

Answered By: inspectorG4dget

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
Answered By: Francisco Velez

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

Answered By: elano7

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.

Answered By: Rolan Gonzalez
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.