import typing


def extract_aliases_for_entity(entities: dict, qid: str) -> typing.Tuple[list, list]:
    """Extracts aliases for a Wikidata entity.

    A helper function for completing the entity dictionaries. For the argument QID
    (entity), we extract the aliases as provided by Wikidata as well as extract a list
    of expanded aliases. These additional aliases are found by taking entities of
    those that "perform" or "do" the entity. For example, if the QID is
    Q6607 (guitar), the additional aliases will contain "guitarist". This was done
    for end tasks (e.g. reading comprehension),  where for the question "What
    instrument does XXX play?" a model may answer "guitarist", which we deem correct.
    Of course, doing this makes the answer set nosier, but in our qualitative
    results, we found this not to be an issue.

    Arguments:
        entities: ``dict`` A dictionary of Wikidata entity information.
        qid: ``str`` A single entity ID.
    Returns:
        aliases: ``list`` A list of aliases for the entity provided by Wikidata.
        additional_aliases: ``list`` A list of additional aliases mined by traversing
            relations of the current entity.
    """
    aliases = []
    additional_aliases = set()
    if qid in entities:
        aliases = entities[qid]["aliases"]

        # Add in aliases for the people who participate in the answer
        # e.g. for the alias guitar, we also add in guitarist
        for value in entities[qid]["pids"].get("P3095", {}).get(
            "values", []
        ) + entities[qid]["pids"].get("P1535", {}).get("values", []):
            pqid = value["qid"]  # pqid = participant QID
            if pqid in entities:
                additional_aliases.update(entities[pqid]["aliases"])

    return aliases, list(sorted(additional_aliases))