from typing import Dict
from typing import Set


def attach_specified_version(line: str, selected_versions: Dict[str, str], reported: Set[str]) -> str:
    """
    :param line: a line from a requirements file
    :param selected_versions: desired version of each package
    :param reported: set of packages names for which changes have been reported.
    :return: version of line with relevant selected version (if present; otherwise line is unchanged).
    """
    line = line.rstrip()
    fields = line.split("==")
    if len(fields) == 2 and fields[0] in selected_versions:
        updated = f"{fields[0]}=={selected_versions[fields[0]]}"
        if updated != line:
            line = updated
            print(f"Upgrading {fields[0]} from {fields[1]} to {selected_versions[fields[0]]}")
            reported.add(fields[0])
    return line