unit tests for instancecheck and subclasscheck import unittest class abctype def instancecheckcls inst implement issubclasssub cls candidates cls dict getsubclass set cls return anyc in candidates for c in sub mro class integermetaclassabc subclass int class subintinteger pass class typecheckstestunittest testcase def testissubclassinternalself self assertequalinteger subclasscheckint true self assertequalinteger subclasscheckfloat false def testissubclassbuiltinself self assertequalissubclassint integer true self assertequalissubclassint integer true self assertequalissubclassfloat integer false self assertequalissubclassfloat integer false def testisinstancebuiltinself self assertequalisinstance42 integer true self assertequalisinstance42 integer true self assertequalisinstance3 14 integer false self assertequalisinstance3 14 integer false def testisinstanceactualself self assertequalisinstanceinteger integer true self assertequalisinstanceinteger integer true def testissubclassactualself self assertequalissubclassinteger integer true self assertequalissubclassinteger integer true def testsubclassbehaviorself self assertequalissubclasssubint integer true self assertequalissubclasssubint integer true self assertequalissubclasssubint subint true self assertequalissubclasssubint subint true self assertequalissubclassinteger subint false self assertequalissubclassinteger subint false self assertequalissubclassint subint false self assertequalissubclassint subint false self assertequalisinstancesubint integer true self assertequalisinstancesubint integer true self assertequalisinstancesubint subint true self assertequalisinstancesubint subint true self assertequalisinstance42 subint false self assertequalisinstance42 subint false if name main unittest main implement isinstance inst cls implement issubclass sub cls
import unittest


class ABC(type):

    def __instancecheck__(cls, inst):
        
        return any(cls.__subclasscheck__(c)
                   for c in {type(inst), inst.__class__})

    def __subclasscheck__(cls, sub):
        
        candidates = cls.__dict__.get("__subclass__", set()) | {cls}
        return any(c in candidates for c in sub.mro())


class Integer(metaclass=ABC):
    __subclass__ = {int}


class SubInt(Integer):
    pass


class TypeChecksTest(unittest.TestCase):

    def testIsSubclassInternal(self):
        self.assertEqual(Integer.__subclasscheck__(int), True)
        self.assertEqual(Integer.__subclasscheck__(float), False)

    def testIsSubclassBuiltin(self):
        self.assertEqual(issubclass(int, Integer), True)
        self.assertEqual(issubclass(int, (Integer,)), True)
        self.assertEqual(issubclass(float, Integer), False)
        self.assertEqual(issubclass(float, (Integer,)), False)

    def testIsInstanceBuiltin(self):
        self.assertEqual(isinstance(42, Integer), True)
        self.assertEqual(isinstance(42, (Integer,)), True)
        self.assertEqual(isinstance(3.14, Integer), False)
        self.assertEqual(isinstance(3.14, (Integer,)), False)

    def testIsInstanceActual(self):
        self.assertEqual(isinstance(Integer(), Integer), True)
        self.assertEqual(isinstance(Integer(), (Integer,)), True)

    def testIsSubclassActual(self):
        self.assertEqual(issubclass(Integer, Integer), True)
        self.assertEqual(issubclass(Integer, (Integer,)), True)

    def testSubclassBehavior(self):
        self.assertEqual(issubclass(SubInt, Integer), True)
        self.assertEqual(issubclass(SubInt, (Integer,)), True)
        self.assertEqual(issubclass(SubInt, SubInt), True)
        self.assertEqual(issubclass(SubInt, (SubInt,)), True)
        self.assertEqual(issubclass(Integer, SubInt), False)
        self.assertEqual(issubclass(Integer, (SubInt,)), False)
        self.assertEqual(issubclass(int, SubInt), False)
        self.assertEqual(issubclass(int, (SubInt,)), False)
        self.assertEqual(isinstance(SubInt(), Integer), True)
        self.assertEqual(isinstance(SubInt(), (Integer,)), True)
        self.assertEqual(isinstance(SubInt(), SubInt), True)
        self.assertEqual(isinstance(SubInt(), (SubInt,)), True)
        self.assertEqual(isinstance(42, SubInt), False)
        self.assertEqual(isinstance(42, (SubInt,)), False)


if __name__ == "__main__":
    unittest.main()