from typing import AnyStr


def ensure_bytes(value: AnyStr) -> bytes:
    """Convert text into bytes, and leaves bytes as-is."""
    if isinstance(value, bytes):
        return value
    if isinstance(value, str):
        return value.encode('utf-8')
    raise TypeError(f"input must be str or bytes, got {type(value).__name__}")