Fix Python prerelease sorting (#7884)

## Summary

Observed from #7880, `rc3` should be sorted before `rc2`.
This commit is contained in:
Jo 2024-10-03 20:03:19 +08:00 committed by GitHub
parent c708cc82fa
commit 32ab2a5a95
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 272 additions and 260 deletions

View file

@ -448,14 +448,26 @@ class PyPyFinder(Finder):
def render(downloads: list[PythonDownload]) -> None:
"""Render `download-metadata.json`."""
def prerelease_sort_key(prerelease: str) -> tuple[int, int]:
if prerelease.startswith("a"):
return 0, int(prerelease[1:])
if prerelease.startswith("b"):
return 1, int(prerelease[1:])
if prerelease.startswith("rc"):
return 2, int(prerelease[2:])
return 3, 0
def sort_key(download: PythonDownload) -> tuple:
# Sort by implementation, version (latest first), and then by triple.
impl_order = [ImplementationName.CPYTHON, ImplementationName.PYPY]
prerelease = prerelease_sort_key(download.version.prerelease)
return (
impl_order.index(download.implementation),
-download.version.major,
-download.version.minor,
-download.version.patch,
-prerelease[0],
-prerelease[1],
download.triple,
)