I have zero idea as to why I’m getting this error.
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:
classis a subclass (direct or indirect) of
classinfo. A class is considered a subclass of itself.
classinfomay be a tuple of class objects, in which case every entry in
classinfowill be checked. In any other case, a
TypeErrorexception is raised.
The first argument to
issubclass() needs to be of type “class”.
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
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.
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.
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
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.