from typing import Union


def as_str(o: Union[str, None], base_type=str, raise_=True):
    """
    Return `o` if already a str, otherwise return the string value for `o`.
    If `o` is None or an empty string, return `default` instead.

    If `o` cannot be converted to an str, raise an error if `raise_` is true,
    other return `default` instead.

    """
    if isinstance(o, base_type):
        return o

    if o is None:
        return base_type()

    try:
        return base_type(o)

    except ValueError:

        if raise_:
            raise

        return base_type()