Skip to content

Commit acbae35

Browse files
cliffhallclaude
andcommitted
Order PEP 440 local version labels, and ignore them in specifier matching as packaging does
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: cliffhall <cliff@futurescale.com>
1 parent 53d6971 commit acbae35

2 files changed

Lines changed: 76 additions & 5 deletions

File tree

‎scripts/lib/pep440.mjs‎

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// "could not determine", which leaves the issue alone.
1515

1616
const VERSION_RE =
17-
/^v?(?:(\d+)!)?(\d+(?:\.\d+)*)(?:[-_.]?(a|alpha|b|beta|c|rc|pre|preview)[-_.]?(\d*))?(?:-(\d+)|[-_.]?(?:post|rev|r)[-_.]?(\d*))?(?:[-_.]?dev[-_.]?(\d*))?(?:\+[a-z0-9]+(?:[-_.][a-z0-9]+)*)?$/i;
17+
/^v?(?:(\d+)!)?(\d+(?:\.\d+)*)(?:[-_.]?(a|alpha|b|beta|c|rc|pre|preview)[-_.]?(\d*))?(?:-(\d+)|[-_.]?(?:post|rev|r)[-_.]?(\d*))?(?:[-_.]?dev[-_.]?(\d*))?(?:\+([a-z0-9]+(?:[-_.][a-z0-9]+)*))?$/i;
1818

1919
const PRE_RANK = {
2020
a: 0,
@@ -29,14 +29,23 @@ const PRE_RANK = {
2929

3030
/**
3131
* @param {string} version
32-
* @returns {{epoch: number, release: number[], pre: [number, number] | null, post: number | null, dev: number | null}}
32+
* @returns {{epoch: number, release: number[], pre: [number, number] | null, post: number | null, dev: number | null, local: Array<number | string> | null}}
3333
* @throws on a version PEP 440 does not admit
3434
*/
3535
export function parseVersion(version) {
3636
const match = VERSION_RE.exec(String(version).trim());
3737
if (!match) throw new Error(`not a PEP 440 version: "${version}"`);
38-
const [, epoch, release, preKind, preNum, postImplicit, postNum, devNum] =
39-
match;
38+
const [
39+
,
40+
epoch,
41+
release,
42+
preKind,
43+
preNum,
44+
postImplicit,
45+
postNum,
46+
devNum,
47+
local,
48+
] = match;
4049
const post =
4150
postImplicit !== undefined
4251
? Number(postImplicit)
@@ -51,9 +60,39 @@ export function parseVersion(version) {
5160
: null,
5261
post,
5362
dev: devNum !== undefined ? Number(devNum || 0) : null,
63+
// Segments split on `.`, `-` or `_`: all digits compare as numbers, the
64+
// rest case-insensitively as text.
65+
local:
66+
local === undefined
67+
? null
68+
: local
69+
.toLowerCase()
70+
.split(/[-_.]/)
71+
.map((seg) => (/^\d+$/.test(seg) ? Number(seg) : seg)),
5472
};
5573
}
5674

75+
/**
76+
* PEP 440 local-label ordering: a version with a label sorts after the same
77+
* version without one; segment by segment a number beats text; and with a
78+
* matching prefix, more segments sort later.
79+
*/
80+
function compareLocal(a, b) {
81+
if (a === null || b === null) return a === b ? 0 : a === null ? -1 : 1;
82+
for (let i = 0; i < Math.min(a.length, b.length); i++) {
83+
const [x, y] = [a[i], b[i]];
84+
if (x === y) continue;
85+
if (typeof x !== typeof y) return typeof x === "number" ? 1 : -1;
86+
return x < y ? -1 : 1;
87+
}
88+
return a.length - b.length;
89+
}
90+
91+
/** The public part of a version: everything before a `+local` label. */
92+
function publicVersion(version) {
93+
return String(version).trim().replace(/\+.*$/, "");
94+
}
95+
5796
/** Is this a pre-release or development release? */
5897
export function isPrerelease(version) {
5998
const v = parseVersion(version);
@@ -76,6 +115,7 @@ function sortKey(v) {
76115
: [Infinity, 0];
77116
return {
78117
epoch: v.epoch,
118+
local: v.local,
79119
release,
80120
rest: [...pre, v.post ?? -Infinity, v.dev ?? Infinity],
81121
};
@@ -97,7 +137,7 @@ export function compareVersions(a, b) {
97137
for (let i = 0; i < x.rest.length; i++) {
98138
if (x.rest[i] !== y.rest[i]) return x.rest[i] < y.rest[i] ? -1 : 1;
99139
}
100-
return 0;
140+
return compareLocal(x.local, y.local);
101141
}
102142

103143
const CLAUSE_RE = /^(~=|===|==|!=|<=|>=|<|>|=)\s*(\S+)$/;
@@ -124,6 +164,14 @@ function clauseMatches(version, clause) {
124164
if (!match) throw new Error(`not a PEP 440 specifier clause: "${clause}"`);
125165
const [, op, operand] = match;
126166
if (op === "===") return String(version).trim() === operand;
167+
// Specifiers match on the public version: a candidate's local label is
168+
// ignored unless an equality operand names one itself (PEP 440, and what
169+
// `packaging` does), so `<=1.0` admits `1.0+vendor.1`.
170+
const keepLocal =
171+
(op === "==" || op === "=" || op === "!=") &&
172+
!operand.endsWith(".*") &&
173+
parseVersion(operand).local !== null;
174+
version = keepLocal ? String(version).trim() : publicVersion(version);
127175
if (op === "==" || op === "=" || op === "!=") {
128176
const equal = operand.endsWith(".*")
129177
? prefixMatches(version, operand.slice(0, -2))

‎scripts/lib/pep440.test.mjs‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ test("parseVersion reads release, pre, post and dev segments", () => {
2020
pre: [0, 1],
2121
post: null,
2222
dev: null,
23+
local: null,
2324
});
2425
assert.deepEqual(parseVersion("4.6.2.post1").post, 1);
2526
assert.deepEqual(parseVersion("1.0-1").post, 1);
@@ -101,6 +102,28 @@ test("prefix matching compares epochs too", () => {
101102
assert.equal(satisfies("1!1.4.5", "==1.4.*"), false);
102103
});
103104

105+
test("local version labels order after the public version, and specifiers ignore them", () => {
106+
assert.deepEqual(parseVersion("1.0+Vendor.1").local, ["vendor", 1]);
107+
assert.ok(compareVersions("1.0+vendor.1", "1.0") > 0);
108+
assert.ok(compareVersions("1.0+vendor.1", "1.0.post1") < 0);
109+
assert.ok(compareVersions("1.0+2", "1.0+abc") > 0, "a number beats text");
110+
assert.ok(
111+
compareVersions("1.0+abc.2", "1.0+abc.10") < 0,
112+
"numbers compare as numbers",
113+
);
114+
assert.ok(
115+
compareVersions("1.0+abc.1.x", "1.0+abc.1") > 0,
116+
"more segments sort later",
117+
);
118+
assert.equal(compareVersions("1.0+ABC", "1.0+abc"), 0);
119+
assert.equal(satisfies("1.0+vendor.1", "==1.0"), true);
120+
assert.equal(satisfies("1.0+vendor.1", "<=1.0"), true);
121+
assert.equal(satisfies("1.0+vendor.1", ">1.0"), false);
122+
assert.equal(satisfies("1.0+vendor.1", "==1.0+vendor.1"), true);
123+
assert.equal(satisfies("1.0+vendor.2", "==1.0+vendor.1"), false);
124+
assert.equal(satisfies("1.0+vendor.1", "===1.0+vendor.1"), true);
125+
});
126+
104127
test("satisfies throws instead of guessing", () => {
105128
assert.throws(() => satisfies("1.0", "about 1.0"), /specifier clause/);
106129
assert.throws(() => satisfies("1.0", "~=1"), /two release segments/);

0 commit comments

Comments
 (0)