From 10c68e674b689673126475734dbed5e43e0177ad Mon Sep 17 00:00:00 2001 From: Chris Jerdonek Date: Mon, 22 Apr 2019 19:23:36 -0700 Subject: [PATCH] Rename _link_package_versions() to evaluate_link(). --- src/pip/_internal/index.py | 32 ++++++++++++++++++++++---------- tests/unit/test_finder.py | 10 +++++----- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/src/pip/_internal/index.py b/src/pip/_internal/index.py index a4ab1b75f..746d5e7cd 100644 --- a/src/pip/_internal/index.py +++ b/src/pip/_internal/index.py @@ -258,7 +258,10 @@ def _get_html_page(link, session=None): class CandidateEvaluator(object): - _py_version_re = re.compile(r'-py([123]\.?[0-9]?)$') + """ + Responsible for filtering and sorting candidates for installation based + on what tags are valid. + """ def __init__( self, @@ -269,6 +272,11 @@ class CandidateEvaluator(object): self._prefer_binary = prefer_binary self._valid_tags = valid_tags + # We compile the regex here instead of as a class attribute so as + # not to not impact pip start-up time. This is also okay because + # CandidateEvaluator is generally instantiated only once per pip + # invocation (when PackageFinder is instantiated). + self._py_version_re = re.compile(r'-py([123]\.?[0-9]?)$') # These are boring links that have already been logged somehow. self._logged_links = set() # type: Set[Link] @@ -278,13 +286,17 @@ class CandidateEvaluator(object): logger.debug('Skipping link %s; %s', link, reason) self._logged_links.add(link) - def is_wheel_supported(self, wheel): + def _is_wheel_supported(self, wheel): # type: (Wheel) -> bool return wheel.supported(self._valid_tags) - def _link_package_versions(self, link, search): + def evaluate_link(self, link, search): # type: (Link, Search) -> Optional[InstallationCandidate] - """Return an InstallationCandidate or None""" + """ + Determine whether a link is a candidate for installation. + + Returns an InstallationCandidate if so, otherwise None. + """ version = None if link.egg_fragment: egg_info = link.egg_fragment @@ -318,7 +330,7 @@ class CandidateEvaluator(object): link, 'wrong project name (not %s)' % search.supplied) return None - if not self.is_wheel_supported(wheel): + if not self._is_wheel_supported(wheel): self._log_skipped_link( link, 'it is not compatible with this Python') return None @@ -384,7 +396,7 @@ class CandidateEvaluator(object): if candidate.location.is_wheel: # can raise InvalidWheelFilename wheel = Wheel(candidate.location.filename) - if not wheel.supported(self._valid_tags): + if not self._is_wheel_supported(wheel): raise UnsupportedWheel( "%s is not a supported wheel for this platform. It " "can't be sorted." % wheel.filename @@ -762,7 +774,7 @@ class PackageFinder(object): This checks index_urls and find_links. All versions found are returned as an InstallationCandidate list. - See _link_package_versions for details on which files are accepted + See evaluate_link() for details on which files are accepted """ index_locations = self._get_index_urls_locations(project_name) index_file_loc, index_url_loc = self._sort_locations(index_locations) @@ -962,9 +974,9 @@ class PackageFinder(object): # type: (...) -> List[InstallationCandidate] result = [] for link in self._sort_links(links): - v = self.candidate_evaluator._link_package_versions(link, search) - if v is not None: - result.append(v) + candidate = self.candidate_evaluator.evaluate_link(link, search) + if candidate is not None: + result.append(candidate) return result diff --git a/tests/unit/test_finder.py b/tests/unit/test_finder.py index 8e6038655..68e985fd7 100644 --- a/tests/unit/test_finder.py +++ b/tests/unit/test_finder.py @@ -460,7 +460,7 @@ def test_finder_installs_pre_releases_with_version_spec(): assert link.url == "https://foo/bar-2.0b1.tar.gz" -class TestLinkPackageVersions(object): +class TestCandidateEvaluator(object): # patch this for travis which has distribute in its base env for now @patch( @@ -481,7 +481,7 @@ class TestLinkPackageVersions(object): 'http:/yo/pytest-1.0-py2.py3-none-any.whl', ], ) - def test_link_package_versions_match(self, url): + def test_evaluate_link__match(self, url): """Test that 'pytest' archives match for 'pytest'""" link = Link(url) search = Search( @@ -489,7 +489,7 @@ class TestLinkPackageVersions(object): canonical=self.canonical_name, formats=['source', 'binary'], ) - result = self.evaluator._link_package_versions(link, search) + result = self.evaluator.evaluate_link(link, search) expected = InstallationCandidate(self.search_name, self.version, link) assert result == expected, result @@ -502,7 +502,7 @@ class TestLinkPackageVersions(object): 'http:/yo/pytest_xdist-1.0-py2.py3-none-any.whl', ], ) - def test_link_package_versions_substring_fails(self, url): + def test_evaluate_link__substring_fails(self, url): """Test that 'pytest archives won't match for 'pytest'.""" link = Link(url) search = Search( @@ -510,7 +510,7 @@ class TestLinkPackageVersions(object): canonical=self.canonical_name, formats=['source', 'binary'], ) - result = self.evaluator._link_package_versions(link, search) + result = self.evaluator.evaluate_link(link, search) assert result is None, result