From 898180d41d2947dacf2409060a43f19895a4c95a Mon Sep 17 00:00:00 2001 From: yamamoto7 Date: Thu, 12 Nov 2026 23:12:48 +0900 Subject: [PATCH] fix: resolve partial PEP 440 specifiers with PEP 440 semantics (#1128) ## Summary `<=`, `==` and `>` with a partial version are valid PEP 440 specifiers, but the resolver tries them as semver ranges first, and semver reads them differently: | specifier | semver (before) | PEP 440 (what uv enforces) | |---|---|---| | `<=0.5` | `<0.6.0`, installs 0.5.31 | `<=0.5.0`, installs 0.5.0 | | `==0.5` | `==` is stripped and `0.5` is read as `0.5.x`, installs 0.5.31 | `==0.5.0`, installs 0.5.0 | | `>0.5` with `resolution-strategy: lowest` | `>=0.6.0`, installs 0.6.0 | `>0.5.0`, installs 0.5.1 | So `required-version = "<=0.5"` or `"==0.5"` in `uv.toml` / `pyproject.toml` installs a uv that does not satisfy the project's own `required-version`, and uv refuses to run there (`Required uv version ... does not match the running version ...`). This is the same problem #1111 fixed for `~=`. Instead of adding more operators to that check, specifiers that `@renovatebot/pep440` accepts as a valid range now skip the semver evaluation, and `==` is only stripped from a full version, so `==0.5` stays a PEP 440 pin. Notes: - Semver-only syntax is not valid PEP 440 and still goes through semver: `0.4.x`, `^0.5`, `~0.5`, `>=0.4.0 <0.5.0` and a bare `0.5`. - Inputs that are valid in both, like `>=0.4.0` from the docs, now go through PEP 440. They mean the same in both, and the PEP 440 path uses the same first-match lookup over the manifest. - Full pins like `==0.9.26` still resolve as explicit versions, and compound specifiers like `>=0.4,<=0.5` already went through PEP 440. ## Test plan - Added cases for `<=0.9` and `==0.9` with the default strategy and `>0.9` with `lowest` in `download-version.test.ts`. All three fail without the change. - `npm ci --ignore-scripts`, then `npm run build`, `npm run check` (no fixes), `npm run package` and `npm run test:unit` on macOS with Node 24.13.0: 179 tests across 16 suites pass. The rebuilt `dist/setup/index.cjs` only differs in the changed functions. Co-authored-by: Claude Opus 5.5 --- __tests__/download/download-version.test.ts | 27 +++++++++++++++++++++ dist/setup/index.cjs | 18 +++++++++----- src/version/resolve.ts | 17 +++++++------ src/version/specifier.ts | 7 +++++- 4 files changed, 54 insertions(+), 15 deletions(-) diff --git a/__tests__/download/download-version.test.ts b/__tests__/download/download-version.test.ts index 2173bc8..c158882 100644 --- a/__tests__/download/download-version.test.ts +++ b/__tests__/download/download-version.test.ts @@ -168,6 +168,33 @@ describe("download-version", () => { expect(version).toBe("0.10.2"); }); + it.each([ + ["<=0.9", "0.9.0"], + ["==0.9", "0.9.0"], + ])("resolves %s with PEP 440 semantics", async (specifier, expected) => { + mockGetFirstMatchingVersion.mockImplementation( + (predicate: (version: string) => boolean) => + ["0.10.2", "0.9.26", "0.9.0", "0.8.24"].find(predicate), + ); + + const version = await resolveVersion(specifier, undefined); + + expect(version).toBe(expected); + }); + + it("resolves >0.9 with PEP 440 semantics for the lowest strategy", async () => { + mockGetAllVersions.mockResolvedValue([ + "0.10.0", + "0.9.26", + "0.9.1", + "0.9.0", + ]); + + const version = await resolveVersion(">0.9", undefined, "lowest"); + + expect(version).toBe("0.9.1"); + }); + it("treats == exact pins as explicit versions", async () => { const version = await resolveVersion("==0.9.26", undefined); diff --git a/dist/setup/index.cjs b/dist/setup/index.cjs index 9ac2d96..0ab68a9 100644 --- a/dist/setup/index.cjs +++ b/dist/setup/index.cjs @@ -100924,6 +100924,9 @@ function contains(specifier, input) { const op = operator_exports[spec.operator]; return op(version3, spec.version || spec.legacy); } +function validRange(specifier) { + return Boolean(parse3(specifier)); +} // src/version/resolve.ts var semver5 = __toESM(require_semver5(), 1); @@ -100950,7 +100953,10 @@ function getLatestKnownVersion() { function normalizeVersionSpecifier(specifier) { const trimmedSpecifier = specifier.trim(); if (trimmedSpecifier.startsWith("==")) { - return trimmedSpecifier.slice(2); + const pinnedVersion = trimmedSpecifier.slice(2).trim(); + if (isExplicitVersion(pinnedVersion)) { + return pinnedVersion; + } } return trimmedSpecifier; } @@ -102035,7 +102041,7 @@ async function resolveVersion(versionInput, manifestUrl, resolutionStrategy = "h throw new Error(`No version found for ${versionInput}`); } async function findHighestSatisfyingVersion(versionSpecifier) { - const semverRange = isCompatibleReleaseSpecifier(versionSpecifier) ? null : semver5.validRange(versionSpecifier); + const semverRange = isPep440Specifier(versionSpecifier) ? null : semver5.validRange(versionSpecifier); if (semverRange !== null) { const semverMatch = await getFirstMatchingVersion( (version3) => semver5.satisfies(version3, semverRange) @@ -102057,11 +102063,11 @@ async function findHighestSatisfyingVersion(versionSpecifier) { } return pep440Match; } -function isCompatibleReleaseSpecifier(versionSpecifier) { - return versionSpecifier.includes("~="); +function isPep440Specifier(versionSpecifier) { + return validRange(versionSpecifier); } function maxSatisfying2(versions, version3) { - const maxSemver = isCompatibleReleaseSpecifier(version3) ? "" : evaluateVersions(versions, version3); + const maxSemver = isPep440Specifier(version3) ? "" : evaluateVersions(versions, version3); if (maxSemver !== "") { debug(`Found a version that satisfies the semver range: ${maxSemver}`); return maxSemver; @@ -102076,7 +102082,7 @@ function maxSatisfying2(versions, version3) { return void 0; } function minSatisfying3(versions, version3) { - const minSemver = isCompatibleReleaseSpecifier(version3) ? null : semver5.minSatisfying(versions, version3); + const minSemver = isPep440Specifier(version3) ? null : semver5.minSatisfying(versions, version3); if (minSemver !== null) { debug(`Found a version that satisfies the semver range: ${minSemver}`); return minSemver; diff --git a/src/version/resolve.ts b/src/version/resolve.ts index fb21f6a..1654be5 100644 --- a/src/version/resolve.ts +++ b/src/version/resolve.ts @@ -169,7 +169,7 @@ async function findHighestSatisfyingVersion( ): Promise { // This fast path assumes uv releases are semver-monotonic: newer manifest // entries always have higher versions, with no lower-version backports. - const semverRange = isCompatibleReleaseSpecifier(versionSpecifier) + const semverRange = isPep440Specifier(versionSpecifier) ? null : semver.validRange(versionSpecifier); if (semverRange !== null) { @@ -196,19 +196,20 @@ async function findHighestSatisfyingVersion( } /** - * `~=` is the PEP 440 compatible release operator. semver accepts it as a - * tilde range with a narrower upper bound (`~=0.5` as `<0.6.0` instead of - * `<1.0`), so it must only be evaluated as PEP 440. + * Specifiers that are valid PEP 440 must be evaluated as PEP 440. semver + * accepts several of them with different semantics, e.g. `~=0.5` as `<0.6.0` + * instead of `<1.0`, `<=0.5` as `<0.6.0` instead of `<=0.5.0` and `>0.5` as + * `>=0.6.0` instead of `>0.5.0`. */ -function isCompatibleReleaseSpecifier(versionSpecifier: string): boolean { - return versionSpecifier.includes("~="); +function isPep440Specifier(versionSpecifier: string): boolean { + return pep440.validRange(versionSpecifier); } function maxSatisfying( versions: string[], version: string, ): string | undefined { - const maxSemver = isCompatibleReleaseSpecifier(version) + const maxSemver = isPep440Specifier(version) ? "" : tc.evaluateVersions(versions, version); if (maxSemver !== "") { @@ -231,7 +232,7 @@ function minSatisfying( versions: string[], version: string, ): string | undefined { - const minSemver = isCompatibleReleaseSpecifier(version) + const minSemver = isPep440Specifier(version) ? null : semver.minSatisfying(versions, version); if (minSemver !== null) { diff --git a/src/version/specifier.ts b/src/version/specifier.ts index bb042d9..491b128 100644 --- a/src/version/specifier.ts +++ b/src/version/specifier.ts @@ -21,8 +21,13 @@ export type ParsedVersionSpecifier = export function normalizeVersionSpecifier(specifier: string): string { const trimmedSpecifier = specifier.trim(); + // Only strip `==` from full versions. A partial pin like `==0.5` means + // `==0.5.0` in PEP 440, but `0.5` alone is read as the semver range `0.5.x`. if (trimmedSpecifier.startsWith("==")) { - return trimmedSpecifier.slice(2); + const pinnedVersion = trimmedSpecifier.slice(2).trim(); + if (tc.isExplicitVersion(pinnedVersion)) { + return pinnedVersion; + } } return trimmedSpecifier;