def METHOD_NAME(
        sql_session, participation, task, language, codenames, cls=Submission):
    """Retrieve digests of files with given codenames from latest submission.
    Get the most recent submission of the given contestant on the given
    task and, if it is of the given language, return the digests of its
    files that correspond to the given codenames. In case of UserTests
    lookup also among the user-provided managers.
    sql_session (Session): the SQLAlchemy session to use.
    participation (Participation): the participation whose submissions
        should be considered.
    task (Task): the task whose submissions should be considered.
    language (Language|None): the language the submission has to be in
        for the lookup to be allowed.
    codenames ({str}): the filenames-with-%l that need to be retrieved.
    cls (type): if the UserTest class is given, lookup user tests rather
        than submissions.
    return ({str: str}): for every codename, the digest of the file of
        that codename in the previous submission; if the previous
        submission didn't have that file it won't be included in the
        result; if there is no previous submission or if it isn't in the
        desired language, return an empty result.
    """
    # FIXME Instead of taking the latest submission *if* it is of the
    # right language, take the latest *among* those of that language.
    latest_submission = get_latest_submission(
        sql_session, participation, task=task, cls=cls)
    language_name = language.name if language is not None else None
    if latest_submission is None or language_name != latest_submission.language:
        return dict()
    digests = dict()
    for codename in codenames:
        # The expected behavior of this code is undefined when a task's
        # submission format, its task type's user_managers and {"input"}
        # are not pairwise disjoint sets. That is not supposed to happen
        # and it would probably already create issues upon submission.
        if codename in latest_submission.files:
            digests[codename] = latest_submission.files[codename].digest
        elif cls is UserTest:
            if codename == "input":
                digests["input"] = latest_submission.input
            else:
                if codename.endswith(".%l"):
                    if language is None:
                        raise ValueError("language not given when submission "
                                         "format requires it")
                    filename = (os.path.splitext(codename)[0]
                                + language.source_extension)
                else:
                    filename = codename
                if filename in latest_submission.managers:
                    digests[codename] = \
                        latest_submission.managers[filename].digest
    return digests