from typing import Union


def ensure_bytes(str_or_bytes: Union[str, bytes],
                 encoding="utf-8", errors="strict") -> bytes:
    """Convert input to bytes if it's str."""
    if isinstance(str_or_bytes, str):
        return str_or_bytes.encode(encoding=encoding, errors=errors)

    if not isinstance(str_or_bytes, bytes):
        raise TypeError("Type bytes required")

    return str_or_bytes