Skip to content

Commit cf5824a

Browse files
committed
Lazy-evaluate candidates with installed inserted
I don't really know why we didn't do this before, but the implementation seems to match the description in the docstring.
1 parent c1dc7a8 commit cf5824a

File tree

1 file changed

+4
-13
lines changed

1 file changed

+4
-13
lines changed

src/pip/_internal/resolution/resolvelib/found_candidates.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import itertools
2-
import operator
32

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

@@ -31,19 +30,11 @@ def _insert_installed(installed, others):
3130
This iterator is used when the resolver prefers to upgrade an
3231
already-installed package. Candidates from index are returned in their
3332
normal ordering, except replaced when the version is already installed.
34-
35-
Since candidates from index are already sorted by reverse version order,
36-
`sorted()` here would keep the ordering mostly intact, only shuffling the
37-
already-installed candidate into the correct position. We put the already-
38-
installed candidate in front of those from the index, so it's put in front
39-
after sorting due to Python sorting's stableness guarentee.
4033
"""
41-
candidates = sorted(
42-
itertools.chain([installed], others),
43-
key=operator.attrgetter("version"),
44-
reverse=True,
45-
)
46-
return iter(candidates)
34+
for candidate in others:
35+
if candidate.version == installed.version:
36+
candidate = installed
37+
yield candidate
4738

4839

4940
class FoundCandidates(collections_abc.Sequence):

0 commit comments

Comments
 (0)