Repository: pypa/pip
File: src/pip/_internal/resolution/resolvelib/provider.py (Python)

Code before:
        The lower the return value is, the more preferred this group of
        arguments is.

        Currently pip considers the following in order:

        * Prefer if any of the known requirements is "direct", e.g. points to an
          explicit URL.
        * If equal, prefer if any requirement is "pinned", i.e. contains
          operator ``===`` or ``==``.
        * Order user-specified requirements by the order they are specified.
        * If equal, prefers "non-free" requirements, i.e. contains at least one
          operator, such as ``>=`` or ``<``.
        * If equal, order alphabetically for consistency (helps debuggability).
        """
        try:
            next(iter(information[identifier]))
        except StopIteration:
            # There is no information for this identifier, so there's no known
            # candidates.
            has_information = False
        else:
            has_information = True

        if has_information:
            lookups = (r.get_candidate_lookup() for r, _ in information[identifier])
            _icandidates, ireqs = zip(*lookups)
        else:
            _icandidates, ireqs = (), ()

        operators: list[tuple[str, str]] = [
            (specifier.operator, specifier.version)
            for specifier_set in (ireq.specifier for ireq in ireqs if ireq)
            for specifier in specifier_set
        ]

        direct = any(
            isinstance(r, ExplicitRequirement) for r, _ in information[identifier]
        )
        pinned = any(((op[:2] == "==") and ("*" not in ver)) for op, ver in operators)
        unfree = bool(operators)
        requested_order = self._user_requested.get(identifier, math.inf)

        # Requires-Python has only one candidate and the check is basically
        # free, so we always do it first to avoid needless work if it fails.
        requires_python = identifier == REQUIRES_PYTHON_IDENTIFIER

        # Prefer the causes of backtracking on the assumption that the problem
        # resolving the dependency tree is related to the failures that caused
        # the backtracking
        backtrack_cause = self.is_backtrack_cause(identifier, backtrack_causes)


Review comment (suggestion):
From what I can tell, `_icandidates` is unused here. I don't think the leading underscore is sufficient indication of this - if you're not comfortable re-using `_` to mean "a value we're ignoring", then can this at least have a comment?

Actually, even better, since `candidate` was originally used to calculate `direct`, can we not do that in one pass now?

```
# Go through the information. For each requirement, check if it's explicit, and get the
# InstallRequirement (the second element) from get_candidate_lookup.
directs, ireqs = (
    isinstance(r, ExplicitRequirement), r.get_candidate_lookup()[1]
    for r, _ in information[identifier]
)
direct = any(directs)
```

Clearly, the comment can be improved by someone who actually knows what this stuff is trying to do 🙂

As a side note, I find the proliferation of types here, in particular named tuples, very confusing. After some research, I established that `lookups` is an iterable of `CandidateLookup` values, which are tuples of candidate and install requirement values. But working that out needed an editor with "Go to definition" functionality. I'd strongly prefer a comment here that explained what's going on, for the casual reader/reviewer.

Code after:
        backtrack_causes: Sequence["PreferenceInformation"],
    ) -> Iterable[str]:
        """Produce a subset of identifiers that should be considered before others.

        Currently pip narrows the following selection:
            * Requires-Python, if present is always returned by itself
            * Backtrack causes are considered next because they can be identified
              in linear time here, whereas because get_preference() is called
              for each identifier, it would be quadratic to check for them there.
              Further, the current backtrack causes likely need to be resolved
              before other requirements as a resolution can't be found while
              there is a conflict.
        """
        backtrack_identifiers = set()
        for info in backtrack_causes:
            backtrack_identifiers.add(info.requirement.name)
            if info.parent is not None:
                backtrack_identifiers.add(info.parent.name)

        current_backtrack_causes = []
        for identifier in identifiers:
            # Requires-Python has only one candidate and the check is basically
            # free, so we always do it first to avoid needless work if it fails.
            # This skips calling get_preference() for all other identifiers.
            if identifier == REQUIRES_PYTHON_IDENTIFIER:
                return [identifier]

            # Check if this identifier is a backtrack cause
            if identifier in backtrack_identifiers:
                current_backtrack_causes.append(identifier)
                continue

        if current_backtrack_causes:
            return current_backtrack_causes

        return identifiers

    def get_preference(
        self,
        identifier: str,
        resolutions: Mapping[str, Candidate],
        candidates: Mapping[str, Iterator[Candidate]],
        information: Mapping[str, Iterable["PreferenceInformation"]],
        backtrack_causes: Sequence["PreferenceInformation"],
    ) -> "Preference":
        """Produce a sort key for given requirement based on preference.

        The lower the return value is, the more preferred this group of
        arguments is.

        Currently pip considers the following in order: