def subclasses(cls, leaves=False):
    """ List all subclasses of the given class, including itself.

    If `leaves=True`, only returns classes which have no subclasses themselves.

    :type cls: type
    :param leaves: Only return leaf classes
    :type leaves: bool
    :rtype: list[type]
    """
    stack = [cls]
    subcls = []
    while stack:
        c = stack.pop()
        c_subs = c.__subclasses__()
        stack.extend(c_subs)
        if not leaves or not c_subs:
            subcls.append(c)
    return subcls