From 015b2c19da61cbde46e53cb3020895f3c9843fa7 Mon Sep 17 00:00:00 2001 From: Andrey Bienkowski Date: Tue, 9 Feb 2021 19:04:45 +0300 Subject: [PATCH 1/7] Add test: build tag is less important than other tags --- tests/unit/test_finder.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tests/unit/test_finder.py b/tests/unit/test_finder.py index 162a9b356f3..89cc655bd9f 100644 --- a/tests/unit/test_finder.py +++ b/tests/unit/test_finder.py @@ -209,6 +209,8 @@ def test_existing_over_wheel_priority(self, data): with pytest.raises(BestVersionAlreadyInstalled): finder.find_requirement(req, True) + +class TestCandidateEvaluator: def test_link_sorting(self): """ Test link sorting @@ -276,6 +278,37 @@ def test_link_sorting_wheels_with_build_tags(self): results2 = sorted(reversed(links), key=sort_key, reverse=True) assert links == results == results2, results2 + def test_build_tag_is_less_important_than_other_tags(self): + links = [ + InstallationCandidate( + "simple", + "1.0", + Link('simple-1.0-1-py3-abi3-linux_x86_64.whl'), + ), + InstallationCandidate( + "simple", + '1.0', + Link('simple-1.0-2-py3-abi3-linux_i386.whl'), + ), + InstallationCandidate( + "simple", + '1.0', + Link('simple-1.0.tar.gz'), + ), + ] + valid_tags = [ + Tag('py3', 'abi3', 'linux_x86_64'), + Tag('py3', 'abi3', 'linux_i386'), + ] + evaluator = CandidateEvaluator( + 'my-project', supported_tags=valid_tags, specifier = SpecifierSet(), + ) + sort_key = evaluator._sort_key + results = sorted(links, key=sort_key, reverse=True) + results2 = sorted(reversed(links), key=sort_key, reverse=True) + + assert links == results == results2, results2 + def test_finder_priority_file_over_page(data): """Test PackageFinder prefers file links over equivalent page links""" From 426279c39a2e0790a943b9f9753c7371f334be9b Mon Sep 17 00:00:00 2001 From: Andrey Bienkowski Date: Tue, 9 Feb 2021 19:09:10 +0300 Subject: [PATCH 2/7] Sort order: compatibility tags are more important than build tag --- src/pip/_internal/index/package_finder.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pip/_internal/index/package_finder.py b/src/pip/_internal/index/package_finder.py index b826690fa5f..c50885430ef 100644 --- a/src/pip/_internal/index/package_finder.py +++ b/src/pip/_internal/index/package_finder.py @@ -530,7 +530,7 @@ def _sort_key(self, candidate): yank_value = -1 * int(link.is_yanked) # -1 for yanked. return ( has_allowed_hash, yank_value, binary_preference, candidate.version, - build_tag, pri, + pri, build_tag, ) def sort_best_candidate( From 0faf750a5192f700901ec45e0a67651a8f39c0d8 Mon Sep 17 00:00:00 2001 From: Andrey Bienkowski Date: Tue, 9 Feb 2021 19:33:55 +0300 Subject: [PATCH 3/7] Add a news entry --- news/9565.bugfix.rst | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 news/9565.bugfix.rst diff --git a/news/9565.bugfix.rst b/news/9565.bugfix.rst new file mode 100644 index 00000000000..4acdcd06a56 --- /dev/null +++ b/news/9565.bugfix.rst @@ -0,0 +1,9 @@ +Make wheel compatibility tag preferences more important than the build tag + +For example: if both linux_x86_64 and linux_i386 are supported and linux_x86_64 +is preferred over linux_i386 the old behavior was to prefer +simple-1.0-2-py3-abi3-linux_i386.whl over +simple-1.0-1-py3-abi3-linux_x86_64.whl +but now pip prefers +simple-1.0-1-py3-abi3-linux_x86_64.whl over +simple-1.0-2-py3-abi3-linux_i386.whl From d8c8c00d7c285be03c734b9c45de300dde2445e5 Mon Sep 17 00:00:00 2001 From: Andrey Bienkowski Date: Tue, 9 Feb 2021 20:16:41 +0300 Subject: [PATCH 4/7] Shorten the news entry --- news/9565.bugfix.rst | 8 -------- 1 file changed, 8 deletions(-) diff --git a/news/9565.bugfix.rst b/news/9565.bugfix.rst index 4acdcd06a56..b8f95fd3592 100644 --- a/news/9565.bugfix.rst +++ b/news/9565.bugfix.rst @@ -1,9 +1 @@ Make wheel compatibility tag preferences more important than the build tag - -For example: if both linux_x86_64 and linux_i386 are supported and linux_x86_64 -is preferred over linux_i386 the old behavior was to prefer -simple-1.0-2-py3-abi3-linux_i386.whl over -simple-1.0-1-py3-abi3-linux_x86_64.whl -but now pip prefers -simple-1.0-1-py3-abi3-linux_x86_64.whl over -simple-1.0-2-py3-abi3-linux_i386.whl From 5297d6927ffc0d06dd200a5c7268b8170d20b8ae Mon Sep 17 00:00:00 2001 From: Andrey Bienkowski Date: Tue, 9 Feb 2021 21:11:50 +0300 Subject: [PATCH 5/7] Add one more test --- tests/unit/test_finder.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/unit/test_finder.py b/tests/unit/test_finder.py index 89cc655bd9f..bde91c897b9 100644 --- a/tests/unit/test_finder.py +++ b/tests/unit/test_finder.py @@ -290,6 +290,11 @@ def test_build_tag_is_less_important_than_other_tags(self): '1.0', Link('simple-1.0-2-py3-abi3-linux_i386.whl'), ), + InstallationCandidate( + "simple", + '1.0', + Link('simple-1.0-2-py3-any-none.whl'), + ), InstallationCandidate( "simple", '1.0', @@ -299,6 +304,7 @@ def test_build_tag_is_less_important_than_other_tags(self): valid_tags = [ Tag('py3', 'abi3', 'linux_x86_64'), Tag('py3', 'abi3', 'linux_i386'), + Tag('py3', 'any', 'none'), ] evaluator = CandidateEvaluator( 'my-project', supported_tags=valid_tags, specifier = SpecifierSet(), From 6db6964a985c88140b8c8dfd5161c5561cbf19d9 Mon Sep 17 00:00:00 2001 From: Andrey Bienkowski Date: Fri, 2 Apr 2021 14:58:17 +0300 Subject: [PATCH 6/7] Rewrite the assertions to be less confusing --- tests/unit/test_finder.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tests/unit/test_finder.py b/tests/unit/test_finder.py index bde91c897b9..09b4ee13388 100644 --- a/tests/unit/test_finder.py +++ b/tests/unit/test_finder.py @@ -251,7 +251,8 @@ def test_link_sorting(self): results = sorted(links, key=sort_key, reverse=True) results2 = sorted(reversed(links), key=sort_key, reverse=True) - assert links == results == results2, results2 + assert links == results, results + assert links == results2, results2 def test_link_sorting_wheels_with_build_tags(self): """Verify build tags affect sorting.""" @@ -276,7 +277,9 @@ def test_link_sorting_wheels_with_build_tags(self): sort_key = candidate_evaluator._sort_key results = sorted(links, key=sort_key, reverse=True) results2 = sorted(reversed(links), key=sort_key, reverse=True) - assert links == results == results2, results2 + + assert links == results, results + assert links == results2, results2 def test_build_tag_is_less_important_than_other_tags(self): links = [ @@ -313,7 +316,8 @@ def test_build_tag_is_less_important_than_other_tags(self): results = sorted(links, key=sort_key, reverse=True) results2 = sorted(reversed(links), key=sort_key, reverse=True) - assert links == results == results2, results2 + assert links == results, results + assert links == results2, results2 def test_finder_priority_file_over_page(data): From e96791f106e8b721cec3dbbbab1423a173889682 Mon Sep 17 00:00:00 2001 From: Andrey Bienkowski Date: Fri, 2 Apr 2021 15:17:59 +0300 Subject: [PATCH 7/7] Lint --- src/pip/_internal/index/package_finder.py | 4 ++-- tests/unit/test_finder.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/pip/_internal/index/package_finder.py b/src/pip/_internal/index/package_finder.py index c50885430ef..4cc9ffd4dfa 100644 --- a/src/pip/_internal/index/package_finder.py +++ b/src/pip/_internal/index/package_finder.py @@ -44,7 +44,7 @@ BuildTag = Union[Tuple[()], Tuple[int, str]] CandidateSortingKey = ( - Tuple[int, int, int, _BaseVersion, BuildTag, Optional[int]] + Tuple[int, int, int, _BaseVersion, Optional[int], BuildTag] ) @@ -530,7 +530,7 @@ def _sort_key(self, candidate): yank_value = -1 * int(link.is_yanked) # -1 for yanked. return ( has_allowed_hash, yank_value, binary_preference, candidate.version, - pri, build_tag, + pri, build_tag, ) def sort_best_candidate( diff --git a/tests/unit/test_finder.py b/tests/unit/test_finder.py index 09b4ee13388..9638199fbf1 100644 --- a/tests/unit/test_finder.py +++ b/tests/unit/test_finder.py @@ -310,7 +310,7 @@ def test_build_tag_is_less_important_than_other_tags(self): Tag('py3', 'any', 'none'), ] evaluator = CandidateEvaluator( - 'my-project', supported_tags=valid_tags, specifier = SpecifierSet(), + 'my-project', supported_tags=valid_tags, specifier=SpecifierSet(), ) sort_key = evaluator._sort_key results = sorted(links, key=sort_key, reverse=True)