def last_candidate(
        self, valid_localities: Optional[List[Locality]] = None
    ) -> Optional[MacroCandidate]:
        """
        Obtain the last (highest precedence) MacroCandidate from the CandidateList of any locality in valid_localities.
        If valid_localities is not specified, return the last MacroCandidate of any locality.
        """
        if not self:
            return None
        self.sort()

        if valid_localities is None:
            return self[-1]

        for candidate in reversed(self):
            if candidate.locality in valid_localities:
                return candidate

        return None

        Obtain the last (highest precedence) MacroCandidate from the CandidateList of any locality in valid_localities.
        If valid_localities is not specified, return the last MacroCandidate of any locality.
        
core/dbt/contracts/graph/manifest.py