Skip to content

Commit 76cffc6

Browse files
committed
Allow a major-only -dev tag to select a prerelease.
Final releases publish the same -dev tags as prereleases, and install lookup only considered prereleases after no final match. `py install 3-dev` therefore resolved to the latest final instead of 3.16.0a1. Treat a requested prerelease tag as preferring prereleases on the first pass. Fixes #428
1 parent c692c46 commit 76cffc6

2 files changed

Lines changed: 96 additions & 1 deletion

File tree

‎src/manage/indexutils.py‎

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from .exceptions import InvalidFeedError
22
from .logging import LOGGER
3-
from .tagutils import tag_or_range, install_matches_any
3+
from .tagutils import CompanyTag, tag_or_range, install_matches_any
44
from .verutils import Version
55

66
SCHEMA = {
@@ -195,6 +195,37 @@ def _patch(source_url, v):
195195
return _SCHEMA_PATCHES.get(v.get("schema"), lambda _, v: v)(source_url, v)
196196

197197

198+
def _tag_prefers_prerelease(tag):
199+
"""True when *tag* asks for a prerelease, such as '3-dev' or '3.16.0a1'.
200+
201+
Tag sorting splits '-dev' and 'a1' off the numeric version, so the version
202+
component alone is not a prerelease. The suffix is what the request means.
203+
"""
204+
if not isinstance(tag, str):
205+
return False
206+
stripped = tag.strip()
207+
if not stripped or stripped.lstrip(" =")[:1] in "<>!":
208+
return False
209+
try:
210+
parsed = tag_or_range(stripped)
211+
except ValueError:
212+
return False
213+
if not isinstance(parsed, CompanyTag):
214+
return False
215+
for part in parsed._sortkey:
216+
text = str(part).casefold()
217+
if text == "dev":
218+
return True
219+
# Free-threaded tags keep the 't' ABI marker on the suffix ('a1t').
220+
if len(text) > 1 and text.endswith("t"):
221+
text = text[:-1]
222+
if len(text) >= 2 and text.startswith("rc") and text[2:].isdigit():
223+
return True
224+
if text[:1] in "abc" and text[1:].isdigit():
225+
return True
226+
return False
227+
228+
198229
class Index:
199230
def __init__(self, source_url, d):
200231
try:
@@ -232,6 +263,12 @@ def find_all(self, tags, *, seen_ids=None, loose_company=False, with_prerelease=
232263
yield i
233264

234265
def find_to_install(self, tag, *, loose_company=False, prefer_prerelease=False):
266+
# Final releases publish the same '-dev' tags as prereleases, so a
267+
# request such as '3-dev' matches an older final on the first pass and
268+
# the prerelease fallback never runs. A tag that is itself a
269+
# prerelease has to include prereleases while scanning newest-first.
270+
if not prefer_prerelease and _tag_prefers_prerelease(tag):
271+
prefer_prerelease = True
235272
tag_list = [tag] if tag else []
236273
LOGGER.debug("Finding %s to install", tag_list)
237274
for i in self.find_all(tag_list, loose_company=loose_company, with_prerelease=prefer_prerelease):

‎tests/test_indexutils.py‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,64 @@ def test_install_lookup():
170170
assert index.find_to_install("<3.12,!=3.10")["tag"] == "3.11.3"
171171

172172

173+
def _feed_package(sort, tag, install_for):
174+
data = fake_install_data(sort, sort_version=sort)
175+
data["tag"] = tag
176+
data["install-for"] = list(install_for)
177+
return data
178+
179+
180+
def test_find_to_install_dev_tag_prefers_prerelease():
181+
# gh-428: finals publish the same '-dev' tags as prereleases, so '3-dev'
182+
# used to stop at 3.15.0. '3.16-dev' already worked because no final
183+
# matched that tag and the prerelease fallback ran.
184+
def listed(full, minor):
185+
major = minor.partition(".")[0]
186+
return [
187+
full, minor, major,
188+
f"{minor}-dev", f"{major}-dev",
189+
f"{full}-64", f"{minor}-64", f"{major}-64",
190+
f"{minor}-dev-64", f"{major}-dev-64",
191+
]
192+
193+
only_major = iu.Index("https://localhost/", {
194+
"versions": [
195+
_feed_package(
196+
"3.16.0a1", "3.16-dev",
197+
listed("3.16.0a1", "3.16"),
198+
),
199+
_feed_package(
200+
"3.15.0", "3.15",
201+
listed("3.15.0", "3.15"),
202+
),
203+
],
204+
})
205+
assert str(only_major.find_to_install("3-dev")["sort-version"]) == "3.16.0a1"
206+
assert str(only_major.find_to_install(r"PythonCore\3-dev")["sort-version"]) == "3.16.0a1"
207+
assert str(only_major.find_to_install("3-dev-64")["sort-version"]) == "3.16.0a1"
208+
assert str(only_major.find_to_install("3.16-dev")["sort-version"]) == "3.16.0a1"
209+
assert str(only_major.find_to_install("3")["sort-version"]) == "3.15.0"
210+
assert str(only_major.find_to_install("3.15-dev")["sort-version"]) == "3.15.0"
211+
212+
# A newer prerelease of an already-final minor must win for '-dev' only.
213+
same_minor = iu.Index("https://localhost/", {
214+
"versions": [
215+
_feed_package(
216+
"3.16.1a1", "3.16-dev",
217+
listed("3.16.1a1", "3.16"),
218+
),
219+
_feed_package(
220+
"3.16.0", "3.16",
221+
listed("3.16.0", "3.16"),
222+
),
223+
],
224+
})
225+
assert str(same_minor.find_to_install("3-dev")["sort-version"]) == "3.16.1a1"
226+
assert str(same_minor.find_to_install("3.16-dev")["sort-version"]) == "3.16.1a1"
227+
assert str(same_minor.find_to_install("3")["sort-version"]) == "3.16.0"
228+
assert str(same_minor.find_to_install("3.16")["sort-version"]) == "3.16.0"
229+
230+
173231
def test_select_package():
174232
from manage.install_command import select_package
175233

0 commit comments

Comments
 (0)