(replacement, original, key, full_key)
Checks that `replacement`, which is intended to replace `original` is of
    the right type. The type is correct if it matches exactly or is one of a few
    cases in which the type can be easily coerced.
Checks that `replacement`, which is intended to replace `original` is of
    the right type. The type is correct if it matches exactly or is one of a few
    cases in which the type can be easily coerced.
Checks that replacement which is intended to replace original is of the right type . The type is correct if it matches exactly or is one of a few cases in which the type can be easily coerced .
def _check_and_coerce_cfg_value_type(replacement, original, key, full_key):
    """Checks that `replacement`, which is intended to replace `original` is of
    the right type. The type is correct if it matches exactly or is one of a few
    cases in which the type can be easily coerced.
    """
    original_type = type(original)
    replacement_type = type(replacement)

    # The types must match (with some exceptions)
    if replacement_type == original_type:
        return replacement

    # If either of them is None, allow type conversion to one of the valid types
    if (replacement_type == type(None) and original_type in _VALID_TYPES) or (
        original_type == type(None) and replacement_type in _VALID_TYPES
    ):
        return replacement

    # Cast replacement from from_type to to_type if the replacement and original
    # types match from_type and to_type
    def conditional_cast(from_type, to_type):
        if replacement_type == from_type and original_type == to_type:
            return True, to_type(replacement)
        else:
            return False, None

    # Conditionally casts
    # list <-> tuple
    casts = [(tuple, list), (list, tuple)]
    # For py2: allow converting from str (bytes) to a unicode string
    try:
        casts.append((str, unicode))  # noqa: F821
    except Exception:
        pass

    for (from_type, to_type) in casts:
        converted, converted_value = conditional_cast(from_type, to_type)
        if converted:
            return converted_value

    raise ValueError(
        "Type mismatch ({} vs. {}) with values ({} vs. {}) for config "
        "key: {}".format(
            original_type, replacement_type, original, replacement, full_key
        )
    )
def _check_and_coerce_cfg_value_type ( replacement , original , key , full_key ) : original_type = type ( original ) replacement_type = type ( replacement ) # The types must match (with some exceptions) if replacement_type == original_type : return replacement # If either of them is None, allow type conversion to one of the valid types if ( replacement_type == type ( None ) and original_type in _VALID_TYPES ) or ( original_type == type ( None ) and replacement_type in _VALID_TYPES ) : return replacement # Cast replacement from from_type to to_type if the replacement and original # types match from_type and to_type def conditional_cast ( from_type , to_type ) : if replacement_type == from_type and original_type == to_type : return True , to_type ( replacement ) else : return False , None # Conditionally casts # list <-> tuple casts = [ ( tuple , list ) , ( list , tuple ) ] # For py2: allow converting from str (bytes) to a unicode string try : casts . append ( ( str , unicode ) ) # noqa: F821 except Exception : pass for ( from_type , to_type ) in casts : converted , converted_value = conditional_cast ( from_type , to_type ) if converted : return converted_value raise ValueError ( "Type mismatch ({} vs. {}) with values ({} vs. {}) for config " "key: {}" . format ( original_type , replacement_type , original , replacement , full_key ) )