Skip to content

Commit

Permalink
Finder found candidates cleanup (#6415)
Browse files Browse the repository at this point in the history
  • Loading branch information
uranusjr authored and cjerdonek committed Apr 20, 2019
1 parent bb14ff4 commit 1ef8857
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 23 deletions.
Empty file.
63 changes: 40 additions & 23 deletions src/pip/_internal/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,9 @@ def _get_html_page(link, session=None):
class FoundCandidates(object):
"""A collection of candidates, returned by `PackageFinder.find_candidates`.
This class is only intended to be instantiated by PackageFinder through
the `from_specifier()` constructor.
Arguments:
* `candidates`: A sequence of all available candidates found.
Expand All @@ -270,15 +273,37 @@ class FoundCandidates(object):
def __init__(
self,
candidates, # type: List[InstallationCandidate]
specifier, # type: specifiers.BaseSpecifier
prereleases, # type: Optional[bool]
versions, # type: Set[str]
sort_key, # type: Callable[[InstallationCandidate], Any]
):
# type: (...) -> None
self._candidates = candidates
self._specifier = specifier
self._prereleases = prereleases
self._sort_key = sort_key
self._versions = versions

@classmethod
def from_specifier(
cls,
candidates, # type: List[InstallationCandidate]
specifier, # type: specifiers.BaseSpecifier
prereleases, # type: Optional[bool]
sort_key, # type: Callable[[InstallationCandidate], Any]
):
# type: (...) -> FoundCandidates
versions = {
str(v) for v in specifier.filter(
# We turn the version object into a str here because otherwise
# when we're debundled but setuptools isn't, Python will see
# packaging.version.Version and
# pkg_resources._vendor.packaging.version.Version as different
# types. This way we'll use a str as a common data interchange
# format. If we stop using the pkg_resources provided specifier
# and start using our own, we can drop the cast to str().
(str(c.version) for c in candidates),
prereleases=prereleases,
)
}
return cls(candidates, versions, sort_key)

def iter_all(self):
# type: () -> Iterable[InstallationCandidate]
Expand All @@ -288,22 +313,11 @@ def iter_all(self):

def iter_applicable(self):
# type: () -> Iterable[InstallationCandidate]
"""Iterate through candidates matching the given specifier.
"""Iterate through candidates matching the versions associated with
this instance.
"""
# Filter out anything which doesn't match our specifier.
versions = set(self._specifier.filter(
# We turn the version object into a str here because otherwise
# when we're debundled but setuptools isn't, Python will see
# packaging.version.Version and
# pkg_resources._vendor.packaging.version.Version as different
# types. This way we'll use a str as a common data interchange
# format. If we stop using the pkg_resources provided specifier
# and start using our own, we can drop the cast to str().
[str(c.version) for c in self._candidates],
prereleases=self._prereleases,
))
# Again, converting to str to deal with debundling.
return (c for c in self._candidates if str(c.version) in versions)
# Again, converting version to str to deal with debundling.
return (c for c in self.iter_all() if str(c.version) in self._versions)

def get_best(self):
# type: () -> Optional[InstallationCandidate]
Expand Down Expand Up @@ -692,8 +706,8 @@ def find_all_candidates(self, project_name):

def find_candidates(
self,
project_name, # type: str
specifier=specifiers.SpecifierSet(), # type: specifiers.BaseSpecifier
project_name, # type: str
specifier=None, # type: Optional[specifiers.BaseSpecifier]
):
"""Find matches for the given project and specifier.
Expand All @@ -702,7 +716,9 @@ def find_candidates(
Returns a `FoundCandidates` instance.
"""
return FoundCandidates(
if specifier is None:
specifier = specifiers.SpecifierSet()
return FoundCandidates.from_specifier(
self.find_all_candidates(project_name),
specifier=specifier,
prereleases=(self.allow_all_prereleases or None),
Expand All @@ -727,7 +743,8 @@ def find_requirement(self, req, upgrade):
def _format_versions(cand_iter):
# This repeated parse_version and str() conversion is needed to
# handle different vendoring sources from pip and pkg_resources.
# If we stop using the pkg_resources provided specifier.
# If we stop using the pkg_resources provided specifier and start
# using our own, we can drop the cast to str().
return ", ".join(sorted(
{str(c.version) for c in cand_iter},
key=parse_version,
Expand Down

0 comments on commit 1ef8857

Please sign in to comment.