Homogenize custom contract exception definition.

def _register_custom_contracts(contract_name, contract_exceptions):
    """Homogenize custom contract exception definition."""
    # pylint: disable=W0602,W0603
    global _CUSTOM_CONTRACTS
    # Validate arguments and homogenize contract exceptions
    if not isinstance(contract_name, str):
        raise TypeError("Argument `contract_name` is of the wrong type")
    # A contract exception can be a string (only one exception, default
    # exception type) or a dictionary of exception definitions, if there is
    # more than one or if the type if different than the default
    if (
        (not isinstance(contract_exceptions, list))
        and (not isinstance(contract_exceptions, str))
        and (not isinstance(contract_exceptions, dict))
    ):
        raise TypeError("Argument `contract_exceptions` is of the wrong type")
    if isinstance(contract_exceptions, dict):
        contract_exceptions = [contract_exceptions]
    if isinstance(contract_exceptions, list) and any(
        [
            not isinstance(key, str)
            for item in contract_exceptions
            for key in item.keys()
        ]
    ):
        raise TypeError("Contract exception definition is of the wrong type")
    # Validate individual exception definitions
    if isinstance(contract_exceptions, list) and any(
        [
            not (
                (set(item.keys()) == set(["name", "msg"]))
                or (set(item.keys()) == set(["name", "msg", "type"]))
            )
            for item in contract_exceptions
        ]
    ):
        raise TypeError("Contract exception definition is of the wrong type")
    extype = type(ValueError)
    if isinstance(contract_exceptions, list) and any(
        [
            (not isinstance(item["name"], str))
            or (not isinstance(item["msg"], str))
            or (not isinstance(item.get("type", extype), extype))
            for item in contract_exceptions
        ]
    ):
        raise TypeError("Contract exception definition is of the wrong type")
    # Homogenize exception definitions
    if isinstance(contract_exceptions, list):
        homogenized_exdict = dict(
            (
                exdict["name"],
                {
                    "num": exnum,
                    "msg": exdict["msg"],
                    "type": exdict.get("type", RuntimeError),
                    "field": _get_replacement_token(exdict["msg"]),
                },
            )
            for exnum, exdict in enumerate(contract_exceptions)
        )
    else:
        homogenized_exdict = {
            "default": {
                "num": 0,
                "msg": contract_exceptions,
                "type": RuntimeError,
                "field": _get_replacement_token(contract_exceptions),
            }
        }
    # Verify exception names are unique
    if isinstance(contract_exceptions, list) and (
        len(homogenized_exdict) != len(contract_exceptions)
    ):
        raise ValueError("Contract exception names are not unique")
    # Verify that exception messages are unique
    msgs = [exvalue["msg"] for exvalue in homogenized_exdict.values()]
    if len(set(msgs)) != len(msgs):
        raise ValueError("Contract exception messages are not unique")
    # Verify that a custom contract is not being redefined
    if (contract_name in _CUSTOM_CONTRACTS) and (
        _CUSTOM_CONTRACTS[contract_name] != contract_exceptions
    ):
        raise RuntimeError(
            "Attempt to redefine custom contract `{0}`".format(contract_name)
        )
    # Verify that there are at most only two replacement fields, and one of
    # them should be argument_name
    fields = [
        exdict["field"]
        for exdict in homogenized_exdict.values()
        if exdict["field"] is not None
    ]
    if (len(fields) > 2) or (
        (len(fields) == 2)
        and (fields[0] != "argument_name")
        and (fields[1] != "argument_name")
    ):
        raise ValueError(
            "Multiple replacement fields to be substituted by argument value"
        )
    # Register new contract
    _CUSTOM_CONTRACTS[contract_name] = homogenized_exdict
    return contract_exceptions