Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finder found candidates cleanup #6415

Merged
merged 7 commits into from
Apr 20, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
42 changes: 22 additions & 20 deletions src/pip/_internal/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,9 +276,20 @@ def __init__(
):
# type: (...) -> None
self._candidates = candidates
self._specifier = specifier
self._prereleases = prereleases
self._sort_key = sort_key
self._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,
)
}

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

def iter_applicable(self):
# type: () -> Iterable[InstallationCandidate]
"""Iterate through candidates matching the given specifier.
"""Iterate through candidates matching the desired versions.
"""
# 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 +691,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,6 +701,8 @@ def find_candidates(

Returns a `FoundCandidates` instance.
"""
if specifier is None:
specifier = specifiers.SpecifierSet()
return FoundCandidates(
self.find_all_candidates(project_name),
specifier=specifier,
Expand All @@ -727,7 +728,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