I have a class that inherits from ABC
, and does not have any abstractmethod
.
I would like to check if it's an abstract class, and am currently stumped.
Determine if a Python class is an Abstract Base Class or Concrete prescribes using inspect.isabstract
. However, this only works if an abstractmethod
has been used.
How can I detect if a class inherits directly from ABC
, without using inspect.isabstract
?
Test Cases
# I need this to be flagged as abstract
class AbstractBaseClassNoAbsMethod(ABC):
pass
# This is currently flaggable with `inspect.isabstract`
class AbstractBaseClassWithAbsMethod(ABC):
@abstractmethod
def some_method(self):
"""Abstract method."""
# I need this to be flagged as not abstract
class ChildClassFromNoAbsMethod(AbstractBaseClassNoAbsMethod):
pass
I considered using issubclass(some_class, ABC)
, but this is True
, even for the above ChildClassFromNoAbsMethod
.
Current Best Solution
My current best solution works using __bases__
. This is basically just listing parent classes, see How to get the parents of a Python class?
def my_isabstract(obj) -> bool:
"""Get if ABC is in the object's __bases__ attribute."""
try:
return ABC in obj.__bases__
except AttributeError:
return False
This is a viable solution. I am not sure if there is a better/more standard way out there.