I want to test if an object is an instance of a subclass but not an instance of the parent class. Example:
class MyClass():
pass
class Sub1(MyClass):
pass
class Sub2(MyClass):
pass
obj = Sub1()
assert isinstance(obj, Sub1 | Sub2) and not isinstance(obj, MyClass)
Is there a test that scales for the case of many subclasses and is less cumbersome than isinstance(obj, Sub1 | Sub2 | ... | SubN)
.
issubclass(type(obj), MyClass)
is compact, but does not exclude objects that are instances of MyClass
because a class is considered a subclass of itself.