Merge pull request #6684 from cjerdonek/simplify-found-candidates

Simplify FoundCandidates
This commit is contained in:
Chris Jerdonek 2019-07-06 02:04:39 -07:00 committed by GitHub
commit 9311049de2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 18 deletions

View File

@ -500,7 +500,17 @@ class CandidateEvaluator(object):
prereleases=allow_prereleases,
)
}
return FoundCandidates(candidates, versions=versions, evaluator=self)
# Again, converting version to str to deal with debundling.
applicable_candidates = [
c for c in candidates if str(c.version) in versions
]
return FoundCandidates(
candidates,
applicable_candidates=applicable_candidates,
evaluator=self,
)
def _sort_key(self, candidate):
# type: (InstallationCandidate) -> CandidateSortingKey
@ -594,21 +604,20 @@ class FoundCandidates(object):
def __init__(
self,
candidates, # type: List[InstallationCandidate]
versions, # type: Set[str]
evaluator, # type: CandidateEvaluator
candidates, # type: List[InstallationCandidate]
applicable_candidates, # type: List[InstallationCandidate]
evaluator, # type: CandidateEvaluator
):
# type: (...) -> None
"""
:param candidates: A sequence of all available candidates found.
:param versions: The applicable versions to filter applicable
candidates.
:param applicable_candidates: The applicable candidates.
:param evaluator: A CandidateEvaluator object to sort applicable
candidates by order of preference.
"""
self._applicable_candidates = applicable_candidates
self._candidates = candidates
self._evaluator = evaluator
self._versions = versions
def iter_all(self):
# type: () -> Iterable[InstallationCandidate]
@ -618,11 +627,9 @@ class FoundCandidates(object):
def iter_applicable(self):
# type: () -> Iterable[InstallationCandidate]
"""Iterate through candidates matching the versions associated with
this instance.
"""Iterate through the applicable candidates.
"""
# Again, converting version to str to deal with debundling.
return (c for c in self.iter_all() if str(c.version) in self._versions)
return iter(self._applicable_candidates)
def get_best(self):
# type: () -> Optional[InstallationCandidate]

View File

@ -4,6 +4,7 @@ import os.path
import pytest
from mock import Mock
from pip._vendor import html5lib, requests
from pip._vendor.packaging.specifiers import SpecifierSet
from pip._internal.download import PipSession
from pip._internal.index import (
@ -170,6 +171,33 @@ class TestLinkEvaluator:
class TestCandidateEvaluator:
def make_mock_candidate(self, version, yanked_reason=None):
url = 'https://example.com/pkg-{}.tar.gz'.format(version)
link = Link(url, yanked_reason=yanked_reason)
candidate = InstallationCandidate('mypackage', version, link)
return candidate
def test_make_found_candidates(self):
specifier = SpecifierSet('<= 1.11')
versions = ['1.10', '1.11', '1.12']
candidates = [
self.make_mock_candidate(version) for version in versions
]
evaluator = CandidateEvaluator()
found_candidates = evaluator.make_found_candidates(
candidates, specifier=specifier,
)
assert found_candidates._candidates == candidates
assert found_candidates._evaluator is evaluator
expected_applicable = candidates[:2]
assert [str(c.version) for c in expected_applicable] == [
'1.10',
'1.11',
]
assert found_candidates._applicable_candidates == expected_applicable
@pytest.mark.parametrize('yanked_reason, expected', [
# Test a non-yanked file.
(None, 0),
@ -190,13 +218,6 @@ class TestCandidateEvaluator:
actual = sort_value[0]
assert actual == expected
def make_mock_candidate(self, version, yanked_reason=None):
url = 'https://example.com/pkg-{}.tar.gz'.format(version)
link = Link(url, yanked_reason=yanked_reason)
candidate = InstallationCandidate('mypackage', version, link)
return candidate
def test_get_best_candidate__no_candidates(self):
"""
Test passing an empty list.