Rename some PackageFinder "best candidate" classes and methods:

* Rename FoundCandidates to BestCandidateResult.

* Rename CandidateEvaluator's make_found_candidates() to
  compute_best_candidate().

* Rename CandidateEvaluator's get_best_candidate() to
  sort_best_candidate().

* Rename PackageFinder's find_candidates() to find_best_candidate().
This commit is contained in:
Chris Jerdonek 2019-07-19 22:07:49 -04:00
parent 58a66066cc
commit e8eda16ed8
5 changed files with 44 additions and 43 deletions

View File

@ -192,7 +192,7 @@ class ListCommand(IndexGroupCommand):
evaluator = finder.make_candidate_evaluator(
project_name=dist.project_name,
)
best_candidate = evaluator.get_best_candidate(all_candidates)
best_candidate = evaluator.sort_best_candidate(all_candidates)
if best_candidate is None:
continue

View File

@ -69,7 +69,7 @@ if MYPY_CHECK_RUNNING:
SecureOrigin = Tuple[str, str, Optional[str]]
__all__ = ['FormatControl', 'FoundCandidates', 'PackageFinder']
__all__ = ['FormatControl', 'BestCandidateResult', 'PackageFinder']
SECURE_ORIGINS = [
@ -568,6 +568,9 @@ class CandidateEvaluator(object):
:param target_python: The target Python interpreter to use when
checking compatibility. If None (the default), a TargetPython
object will be constructed from the running Python.
:param specifier: An optional object implementing `filter`
(e.g. `packaging.specifiers.SpecifierSet`) to filter applicable
versions.
:param hashes: An optional collection of allowed hashes.
"""
if target_python is None:
@ -643,21 +646,17 @@ class CandidateEvaluator(object):
project_name=self._project_name,
)
def make_found_candidates(
def compute_best_candidate(
self,
candidates, # type: List[InstallationCandidate]
):
# type: (...) -> FoundCandidates
# type: (...) -> BestCandidateResult
"""
Create and return a `FoundCandidates` instance.
:param specifier: An optional object implementing `filter`
(e.g. `packaging.specifiers.SpecifierSet`) to filter applicable
versions.
Compute and return a `BestCandidateResult` instance.
"""
applicable_candidates = self.get_applicable_candidates(candidates)
return FoundCandidates(
return BestCandidateResult(
candidates,
applicable_candidates=applicable_candidates,
evaluator=self,
@ -723,7 +722,7 @@ class CandidateEvaluator(object):
build_tag, pri,
)
def get_best_candidate(
def sort_best_candidate(
self,
candidates, # type: List[InstallationCandidate]
):
@ -754,11 +753,11 @@ class CandidateEvaluator(object):
return best_candidate
class FoundCandidates(object):
"""A collection of candidates, returned by `PackageFinder.find_candidates`.
class BestCandidateResult(object):
"""A collection of candidates, returned by `PackageFinder.find_best_candidate`.
This class is only intended to be instantiated by CandidateEvaluator's
`make_found_candidates()` method.
`compute_best_candidate()` method.
"""
def __init__(
@ -796,7 +795,7 @@ class FoundCandidates(object):
candidates are found.
"""
candidates = list(self.iter_applicable())
return self._evaluator.get_best_candidate(candidates)
return self._evaluator.sort_best_candidate(candidates)
class PackageFinder(object):
@ -1174,20 +1173,20 @@ class PackageFinder(object):
hashes=hashes,
)
def find_candidates(
def find_best_candidate(
self,
project_name, # type: str
specifier=None, # type: Optional[specifiers.BaseSpecifier]
hashes=None, # type: Optional[Hashes]
):
# type: (...) -> FoundCandidates
# type: (...) -> BestCandidateResult
"""Find matches for the given project and specifier.
:param specifier: An optional object implementing `filter`
(e.g. `packaging.specifiers.SpecifierSet`) to filter applicable
versions.
:return: A `FoundCandidates` instance.
:return: A `BestCandidateResult` instance.
"""
candidates = self.find_all_candidates(project_name)
candidate_evaluator = self.make_candidate_evaluator(
@ -1195,7 +1194,7 @@ class PackageFinder(object):
specifier=specifier,
hashes=hashes,
)
return candidate_evaluator.make_found_candidates(candidates)
return candidate_evaluator.compute_best_candidate(candidates)
def find_requirement(self, req, upgrade):
# type: (InstallRequirement, bool) -> Optional[Link]
@ -1206,10 +1205,10 @@ class PackageFinder(object):
Raises DistributionNotFound or BestVersionAlreadyInstalled otherwise
"""
hashes = req.hashes(trust_internet=False)
candidates = self.find_candidates(
best_candidate_result = self.find_best_candidate(
req.name, specifier=req.specifier, hashes=hashes,
)
best_candidate = candidates.get_best()
best_candidate = best_candidate_result.get_best()
installed_version = None # type: Optional[_BaseVersion]
if req.satisfied_by is not None:
@ -1230,7 +1229,7 @@ class PackageFinder(object):
'Could not find a version that satisfies the requirement %s '
'(from versions: %s)',
req,
_format_versions(candidates.iter_all()),
_format_versions(best_candidate_result.iter_all()),
)
raise DistributionNotFound(
@ -1265,14 +1264,14 @@ class PackageFinder(object):
'Installed version (%s) is most up-to-date (past versions: '
'%s)',
installed_version,
_format_versions(candidates.iter_applicable()),
_format_versions(best_candidate_result.iter_applicable()),
)
raise BestVersionAlreadyInstalled
logger.debug(
'Using version %s (newest of versions: %s)',
best_candidate.version,
_format_versions(candidates.iter_applicable()),
_format_versions(best_candidate_result.iter_applicable()),
)
return best_candidate.link

View File

@ -139,10 +139,10 @@ def pip_version_check(session, options):
trusted_hosts=options.trusted_hosts,
session=session,
)
candidate = finder.find_candidates("pip").get_best()
if candidate is None:
best_candidate = finder.find_best_candidate("pip").get_best()
if best_candidate is None:
return
pypi_version = str(candidate.version)
pypi_version = str(best_candidate.version)
# save that we've performed a check
state.save(pypi_version, current_time)

View File

@ -387,7 +387,7 @@ class TestCandidateEvaluator:
actual_versions = [str(c.version) for c in actual]
assert actual_versions == expected_versions
def test_make_found_candidates(self):
def test_compute_best_candidate(self):
specifier = SpecifierSet('<= 1.11')
versions = ['1.10', '1.11', '1.12']
candidates = [
@ -397,16 +397,16 @@ class TestCandidateEvaluator:
'my-project',
specifier=specifier,
)
found_candidates = evaluator.make_found_candidates(candidates)
result = evaluator.compute_best_candidate(candidates)
assert found_candidates._candidates == candidates
assert found_candidates._evaluator is evaluator
assert result._candidates == candidates
assert result._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
assert result._applicable_candidates == expected_applicable
@pytest.mark.parametrize('hex_digest, expected', [
# Test a link with no hash.
@ -448,15 +448,15 @@ class TestCandidateEvaluator:
actual = sort_value[1]
assert actual == expected
def test_get_best_candidate__no_candidates(self):
def test_sort_best_candidate__no_candidates(self):
"""
Test passing an empty list.
"""
evaluator = CandidateEvaluator.create('my-project')
actual = evaluator.get_best_candidate([])
actual = evaluator.sort_best_candidate([])
assert actual is None
def test_get_best_candidate__all_yanked(self, caplog):
def test_sort_best_candidate__all_yanked(self, caplog):
"""
Test all candidates yanked.
"""
@ -468,7 +468,7 @@ class TestCandidateEvaluator:
]
expected_best = candidates[1]
evaluator = CandidateEvaluator.create('my-project')
actual = evaluator.get_best_candidate(candidates)
actual = evaluator.sort_best_candidate(candidates)
assert actual is expected_best
assert str(actual.version) == '3.0'
@ -489,7 +489,7 @@ class TestCandidateEvaluator:
# Test a unicode string with a non-ascii character.
(u'curly quote: \u2018', u'curly quote: \u2018'),
])
def test_get_best_candidate__yanked_reason(
def test_sort_best_candidate__yanked_reason(
self, caplog, yanked_reason, expected_reason,
):
"""
@ -499,7 +499,7 @@ class TestCandidateEvaluator:
make_mock_candidate('1.0', yanked_reason=yanked_reason),
]
evaluator = CandidateEvaluator.create('my-project')
actual = evaluator.get_best_candidate(candidates)
actual = evaluator.sort_best_candidate(candidates)
assert str(actual.version) == '1.0'
assert len(caplog.records) == 1
@ -513,7 +513,9 @@ class TestCandidateEvaluator:
) + expected_reason
assert record.message == expected_message
def test_get_best_candidate__best_yanked_but_not_all(self, caplog):
def test_sort_best_candidate__best_yanked_but_not_all(
self, caplog,
):
"""
Test the best candidates being yanked, but not all.
"""
@ -526,7 +528,7 @@ class TestCandidateEvaluator:
]
expected_best = candidates[1]
evaluator = CandidateEvaluator.create('my-project')
actual = evaluator.get_best_candidate(candidates)
actual = evaluator.sort_best_candidate(candidates)
assert actual is expected_best
assert str(actual.version) == '2.0'

View File

@ -12,7 +12,7 @@ from pip._internal.index import InstallationCandidate
from pip._internal.utils import outdated
class MockFoundCandidates(object):
class MockBestCandidateResult(object):
def __init__(self, best):
self._best = best
@ -37,8 +37,8 @@ class MockPackageFinder(object):
def create(cls, *args, **kwargs):
return cls()
def find_candidates(self, project_name):
return MockFoundCandidates(self.INSTALLATION_CANDIDATES[0])
def find_best_candidate(self, project_name):
return MockBestCandidateResult(self.INSTALLATION_CANDIDATES[0])
class MockDistribution(object):