Skip to content

Requires-Python error message should sort versions numerically, not alphabetically #13394

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

Merged
merged 6 commits into from
May 17, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions news/13367.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Requires-Python error message should not sort versions numerically, not alphabetically
18 changes: 16 additions & 2 deletions src/pip/_internal/index/package_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from pip._vendor.packaging import specifiers
from pip._vendor.packaging.tags import Tag
from pip._vendor.packaging.utils import canonicalize_name
from pip._vendor.packaging.version import InvalidVersion, _BaseVersion
from pip._vendor.packaging.version import InvalidVersion, Version, _BaseVersion
from pip._vendor.packaging.version import parse as parse_version

from pip._internal.exceptions import (
Expand Down Expand Up @@ -248,7 +248,21 @@ def evaluate_link(self, link: Link) -> tuple[LinkType, str]:
ignore_requires_python=self._ignore_requires_python,
)
if not supports_python:
reason = f"{version} Requires-Python {link.requires_python}"
requires_python = link.requires_python
if requires_python:
try:
rp_ver_spec_list = []
for rp_spec in specifiers.SpecifierSet(requires_python):
rp_version = rp_spec.version
if rp_spec.operator == "==" and rp_version.endswith(".*"):
rp_version = rp_version[:-2]
rp_ver_spec_list.append((Version(rp_version), rp_spec))
requires_python = ",".join(
[str(s) for _, s in sorted(rp_ver_spec_list)]
)
except InvalidVersion:
pass
reason = f"{version} Requires-Python {requires_python}"
return (LinkType.requires_python_mismatch, reason)

logger.debug("Found link %s, version: %s", link, version)
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class TestLinkEvaluator:
False,
(
LinkType.requires_python_mismatch,
"1.12 Requires-Python == 3.6.5",
"1.12 Requires-Python ==3.6.5,!=3.13.3",
),
id="requires-python-mismatch",
),
Expand Down Expand Up @@ -162,7 +162,7 @@ def test_evaluate_link(
)
link = Link(
"https://example.com/#egg=twine-1.12",
requires_python="== 3.6.5",
requires_python="!= 3.13.3, == 3.6.5",
)
actual = evaluator.evaluate_link(link)
assert actual == expected
Expand Down