def preserve_req_suffixes(requirement, pinned_library_req):
    """Swap in `pinned_library_req` while preserving `requirement`'s environment marker and comment.

    Parameters
    ----------
    requirement : str
        A line from a pip requirements file.
    pinned_library_req : str
        Library + version pin, e.g. ``"verta==0.20.0"``.

    Returns
    -------
    str

    Examples
    --------
    .. code-block:: python

        assert preserve_req_suffixes(
            "verta;python_version>'2.7' and python_version<'3.9'  # very important!",
            "verta==0.20.0",
        ) == "verta==0.20.0;python_version>'2.7' and python_version<'3.9'  # very important!"

        assert preserve_req_suffixes(
            "verta;python_version<='2.7'",
            "verta==0.20.0",
        ) == "verta==0.20.0;python_version<='2.7'"

        assert preserve_req_suffixes(
            "verta  # very important!",
            "verta==0.20.0",
        ) == "verta==0.20.0 # very important!"

        assert preserve_req_suffixes(
            "verta",
            "verta==0.20.0",
        ) == "verta==0.20.0"

    """
    for delimiter in [
        ";",  # environment marker
        " #",  # comment
    ]:
        if delimiter not in requirement:
            continue
        split_req = requirement.split(delimiter)
        split_req[0] = pinned_library_req
        return delimiter.join(split_req)
    return pinned_library_req