Determine if a Python class is an Abstract Base Class or Concrete

import inspect
print(inspect.isabstract(object))                  # False
print(inspect.isabstract(MessageDisplay))          # True
print(inspect.isabstract(FriendlyMessageDisplay))  # True
print(inspect.isabstract(FriendlyMessagePrinter))  # False

This checks that the internal flag TPFLAGS_IS_ABSTRACT is set in the class object, so it can’t be fooled as easily as your implementation:

class Fake:
    __abstractmethods__ = 'bluh'

print(is_abstract(Fake), inspect.isabstract(Fake)) # True, False

Leave a Comment