Skip to content

Commit ab6ddfb

Browse files
edvilmeCopilot
andcommitted
Route post-merge version handling through PythonVersion
Follow-up to the rebase onto main. Removes the remaining hand-rolled version handling around the centralized classes: - Parse each uv catalog candidate once and sort on the parsed value, instead of re-parsing with the throwing constructor inside the sort comparator. - Apply the same decorate-sort-undecorate shape to pickCompatibleInterpreter, so sorting no longer depends on a distant filter having already proven every version parses. - Build the short display string with toReleaseString instead of reaching into major/minor/patch. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 6444890d-5c9c-4e8e-82b3-37a157ead632
1 parent a2d44d4 commit ab6ddfb

3 files changed

Lines changed: 14 additions & 14 deletions

File tree

src/common/inlineScript/interpreter.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@ export function pickCompatibleInterpreter(
2727
): PythonEnvironment | undefined {
2828
const trimmedConstraint = requiresPython?.trim();
2929
const constraint = trimmedConstraint ? trimmedConstraint : undefined;
30-
const candidates = installed.filter((env) => isUsableBaseInterpreter(env, constraint));
30+
const candidates = installed.flatMap((env) => {
31+
const version = isUsableBaseInterpreter(env, constraint) ? PythonVersion.tryParse(env.version) : undefined;
32+
return version ? [{ env, version }] : [];
33+
});
3134
if (candidates.length === 0) {
3235
return undefined;
3336
}
34-
const sorted = [...candidates].sort((a, b) => new PythonVersion(b.version).compareTo(new PythonVersion(a.version)));
35-
return sorted[0];
37+
return candidates.sort((a, b) => b.version.compareTo(a.version))[0].env;
3638
}
3739

3840
/**

src/managers/builtin/inlineScript/envManager.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2475,17 +2475,17 @@ export class InlineScriptEnvManager implements EnvironmentManager, Disposable {
24752475
return { errorCategory: 'install-failure' };
24762476
}
24772477
const version = available
2478-
.filter(
2479-
(candidate) =>
2478+
.flatMap((candidate) => {
2479+
const parsed = PythonVersion.tryParse(candidate.version);
2480+
return parsed &&
24802481
candidate.implementation === 'cpython' &&
24812482
candidate.variant === 'default' &&
24822483
candidate.version_parts.major === 3 &&
2483-
PythonVersion.tryParse(candidate.version) !== undefined &&
2484-
this.matchesInstallConstraint(requiresPython, candidate.version),
2485-
)
2486-
.sort((left, right) =>
2487-
new PythonVersion(right.version).compareTo(new PythonVersion(left.version)),
2488-
)[0]?.version;
2484+
this.matchesInstallConstraint(requiresPython, candidate.version)
2485+
? [{ parsed, raw: candidate.version }]
2486+
: [];
2487+
})
2488+
.sort((left, right) => right.parsed.compareTo(left.parsed))[0]?.raw;
24892489
return version ? { version } : { errorCategory: 'no-compatible-python' };
24902490
}
24912491

src/managers/common/utils.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ export function shortenVersionString(input: string): string {
3131
if (!version) {
3232
return input;
3333
}
34-
return version.precision >= 3
35-
? `${version.major}.${version.minor}.${version.patch}`
36-
: `${version.major}.${version.minor}.x`;
34+
return version.precision >= 3 ? version.toReleaseString(3) : `${version.toReleaseString(2)}.x`;
3735
}
3836

3937
export function sortEnvironments(collection: PythonEnvironment[]): PythonEnvironment[] {

0 commit comments

Comments
 (0)