import socket


def free_port() -> int:
    """
    Return free port on the localhost.

    Returns
    -------
    int
        Free port number on the localhost.

    """
    sock = socket.socket(socket.AF_INET, type=socket.SOCK_STREAM)
    sock.bind(("localhost", 0))
    port: int
    _, port = sock.getsockname()
    sock.close()
    return port