from typing import Optional


def build_netloc(host: str, port: Optional[int] = None) -> str:
    """Create a netloc that can be passed to `url.parse.urlunsplit` while safely handling ipv6 addresses."""
    escaped_host = f"[{host}]" if ":" in host else host
    return escaped_host if port is None else f"{escaped_host}:{port}"