From 4a9e306678f35dec2c0eae428048e037baff1030 Mon Sep 17 00:00:00 2001 From: Chris Jerdonek Date: Sat, 6 Jul 2019 02:17:37 -0700 Subject: [PATCH] Add get_applicable_candidates(), and test. --- src/pip/_internal/index.py | 39 ++++++++++++++++++++++++++------------ tests/unit/test_index.py | 17 +++++++++++++++++ 2 files changed, 44 insertions(+), 12 deletions(-) diff --git a/src/pip/_internal/index.py b/src/pip/_internal/index.py index c7b2d0b0a..1352de1a9 100644 --- a/src/pip/_internal/index.py +++ b/src/pip/_internal/index.py @@ -507,22 +507,15 @@ class CandidateEvaluator(object): self._prefer_binary = prefer_binary self._supported_tags = supported_tags - def make_found_candidates( + def get_applicable_candidates( self, - candidates, # type: List[InstallationCandidate] - specifier=None, # type: Optional[specifiers.BaseSpecifier] + candidates, # type: List[InstallationCandidate] + specifier, # type: specifiers.BaseSpecifier ): - # type: (...) -> FoundCandidates + # type: (...) -> List[InstallationCandidate] """ - Create and return a `FoundCandidates` instance. - - :param specifier: An optional object implementing `filter` - (e.g. `packaging.specifiers.SpecifierSet`) to filter applicable - versions. + Return the applicable candidates from a list of candidates. """ - if specifier is None: - specifier = specifiers.SpecifierSet() - # Using None infers from the specifier instead. allow_prereleases = self._allow_all_prereleases or None versions = { @@ -543,6 +536,28 @@ class CandidateEvaluator(object): applicable_candidates = [ c for c in candidates if str(c.version) in versions ] + return applicable_candidates + + def make_found_candidates( + self, + candidates, # type: List[InstallationCandidate] + specifier=None, # type: Optional[specifiers.BaseSpecifier] + ): + # type: (...) -> FoundCandidates + """ + Create and return a `FoundCandidates` instance. + + :param specifier: An optional object implementing `filter` + (e.g. `packaging.specifiers.SpecifierSet`) to filter applicable + versions. + """ + if specifier is None: + specifier = specifiers.SpecifierSet() + + applicable_candidates = self.get_applicable_candidates( + candidates=candidates, + specifier=specifier, + ) return FoundCandidates( candidates, diff --git a/tests/unit/test_index.py b/tests/unit/test_index.py index f9018324c..d6ebf939d 100644 --- a/tests/unit/test_index.py +++ b/tests/unit/test_index.py @@ -205,6 +205,23 @@ class TestCandidateEvaluator: return candidate + def test_get_applicable_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.create() + actual = evaluator.get_applicable_candidates( + candidates, specifier=specifier, + ) + expected_applicable = candidates[:2] + assert [str(c.version) for c in expected_applicable] == [ + '1.10', + '1.11', + ] + assert actual == expected_applicable + def test_make_found_candidates(self): specifier = SpecifierSet('<= 1.11') versions = ['1.10', '1.11', '1.12']