from typing import Any
from typing import Optional
from typing import IO
import yaml


def dump_yaml(data: Any, stream: Optional[IO[str]] = None) -> Optional[str]:
    """Dump YAML to stream or return as string."""
    if stream is not None:
        return yaml.safe_dump(data, stream, default_flow_style=False)
    else:
        return yaml.safe_dump(data, default_flow_style=False)