Validate one exception specification contract tuple/list.

def _format_arg(arg):
    """Validate one exception specification contract tuple/list."""
    # Check that the argument conforms to one of the acceptable types, a string
    # (when the default exception type is used), an exception type (when the
    # default exception message is used) or a list/tuple to specify both
    # exception type and exception message
    if not (_isexception(arg) or isinstance(arg, (str, tuple, list))):
        raise TypeError("Illegal custom contract exception definition")
    # Check that when the argument is a list or tuple, they only have at most
    # 2 items, the exception type and the exception message
    if isinstance(arg, (tuple, list)) and ((not arg) or (len(arg) > 2)):
        raise TypeError("Illegal custom contract exception definition")
    # When only an exception message is given (and the default RuntimeError
    # exception type is used), check that the message is not empty
    if isinstance(arg, str) and (not arg):
        raise ValueError("Empty custom contract exception message")
    # When only an exception message is defined,
    # use the default exception type
    if isinstance(arg, str):
        return {"msg": arg, "type": RuntimeError}
    # When only an exception type is defined,
    # use the default exception message
    if _isexception(arg):
        return {"msg": "Argument `*[argument_name]*` is not valid", "type": arg}
    # If a list/tuple definition is used, check that if is not a string, it is
    # a valid exception type (i.e. that it actually raises an exception)
    if (len(arg) == 1) and (not isinstance(arg[0], str)) and (not _isexception(arg[0])):
        raise TypeError("Illegal custom contract exception definition")
    if (len(arg) == 2) and (
        not (
            (isinstance(arg[0], str) and _isexception(arg[1]))
            or (isinstance(arg[1], str) and _isexception(arg[0]))
        )
    ):
        raise TypeError("Illegal custom contract exception definition")
    # Check that the exception definition has a non-empty exception message
    # when a list/tuple definition is used
    if (len(arg) == 1) and isinstance(arg[0], str) and (not arg[0]):
        raise ValueError("Empty custom contract exception message")
    if (len(arg) == 2) and (
        (isinstance(arg[0], str) and (not arg[0]))
        or (isinstance(arg[1], str) and (not arg[1]))
    ):
        raise ValueError("Empty custom contract exception message")
    # Return conforming dictionary with default exception type and exception
    # message applied (if necessary)
    if len(arg) == 1:
        return {
            "msg": (
                arg[0]
                if isinstance(arg[0], str)
                else "Argument `*[argument_name]*` is not valid"
            ),
            "type": arg[0] if _isexception(arg[0]) else RuntimeError,
        }
    # len(arg) == 2
    return {
        "msg": arg[0] if isinstance(arg[0], str) else arg[1],
        "type": arg[0] if _isexception(arg[0]) else arg[1],
    }