def same_string_type_as(type_source, string, encoding):
    """
    Return a string of the same type as `type_source` with the content from
    `string`.

    If the `type_source` and `string` don't have the same type, use `encoding`
    to encode or decode, whatever operation is needed.
    """
    if isinstance(type_source, bytes) and isinstance(string, str):
        return string.encode(encoding)
    elif isinstance(type_source, str) and isinstance(string, bytes):
        return string.decode(encoding)
    else:
        return string