DO NOT sort the returned matches by version

A higher version is not always preferred over the lower; the user may
be explicitly preferring lower versions by specifying --prefer-binary
or similar flags.

PackageFinder already takes these into account for these and orders
the matches. Don't break it.
This commit is contained in:
Tzu-ping Chung 2020-05-25 14:09:26 +08:00
parent 7d875faff9
commit 5f2bc2b47c
1 changed files with 9 additions and 6 deletions

View File

@ -6,8 +6,6 @@ from pip._internal.utils.typing import MYPY_CHECK_RUNNING
if MYPY_CHECK_RUNNING:
from typing import Any, Dict, Optional, Sequence, Set, Tuple, Union
from pip._vendor.packaging.version import _BaseVersion
from .base import Requirement, Candidate
from .factory import Factory
@ -90,17 +88,22 @@ class PipProvider(AbstractProvider):
return False
def sort_key(c):
# type: (Candidate) -> Tuple[int, _BaseVersion]
# type: (Candidate) -> int
"""Return a sort key for the matches.
The highest priority should be given to installed candidates that
are not eligible for upgrade. We use the integer value in the first
part of the key to sort these before other candidates.
We only pull the installed candidate to the bottom (i.e. most
preferred), but otherwise keep the ordering returned by the
requirement. The requirement is responsible for returning a list
otherwise sorted for the resolver, taking account for versions
and binary preferences as specified by the user.
"""
if c.is_installed and not _eligible_for_upgrade(c.name):
return (1, c.version)
return (0, c.version)
return 1
return 0
return sorted(matches, key=sort_key)