Skip to content

Commit

Permalink
Improve sorting logic
Browse files Browse the repository at this point in the history
  • Loading branch information
uranusjr committed Oct 13, 2020
1 parent 19ec89e commit a279a9c
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions src/pip/_internal/resolution/resolvelib/found_candidates.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import functools
import itertools
import operator

from pip._vendor.six.moves import collections_abc # type: ignore

from pip._internal.utils.compat import lru_cache
from pip._internal.utils.typing import MYPY_CHECK_RUNNING

if MYPY_CHECK_RUNNING:
from typing import Any, Callable, Iterator, Optional, Set
from typing import Callable, Iterator, Optional, Set

from pip._vendor.packaging.version import _BaseVersion

Expand All @@ -24,11 +24,6 @@ def _deduplicated_by_version(candidates):
yield candidate


def _replaces_sort_key(installed, candidate):
# type: (Candidate, Candidate) -> Any
return (candidate.version, candidate is installed)


def _insert_installed(installed, others):
# type: (Candidate, Iterator[Candidate]) -> Iterator[Candidate]
"""Iterator for ``FoundCandidates``.
Expand All @@ -37,12 +32,15 @@ def _insert_installed(installed, others):
already-installed package. Candidates from index are returned in their
normal ordering, except replaced when the version is already installed.
The sort key prefers the installed candidate over candidates of the same
version from the index, so it is chosen on de-duplication.
Since candidates from index are already sorted by reverse version order,
`sorted()` here would keep the ordering mostly intact, only shuffling the
already-installed candidate into the correct position. We put the already-
installed candidate in front of those from the index, so it's put in front
after sorting due to Python sorting's stableness guarentee.
"""
candidates = sorted(
itertools.chain(others, [installed]),
key=functools.partial(_replaces_sort_key, installed),
itertools.chain([installed], others),
key=operator.attrgetter("version"),
reverse=True,
)
return iter(candidates)
Expand Down

0 comments on commit a279a9c

Please sign in to comment.