def isinstance_or_subclass(inst, cls):
    """ Utility for safely checking isinstance and issubclass. """
    if isinstance(inst, cls):
        return True
    try:
        if issubclass(inst, cls):
            return True
    except TypeError:
        pass
    return False