Skip to content

Use PythonVersion class to parse and compare Python versions - #1748

Merged
Eduardo Villalpando Mello (edvilme) merged 10 commits into
mainfrom
python-versions
Sep 1, 2026
Merged

Use PythonVersion class to parse and compare Python versions#1748
Eduardo Villalpando Mello (edvilme) merged 10 commits into
mainfrom
python-versions

Conversation

@edvilme

Copy link
Copy Markdown
Contributor

This pull request introduces a new PythonVersion class to provide robust, normalized parsing and comparison of Python version strings. The codebase is updated to use this new class in the getLatest utility, replacing the previous PEP 440-based implementation. Comprehensive unit tests are also added for both the version normalization logic and the updated selection of the latest Python environment.

Python version normalization and comparison:

  • Added the PythonVersion class in src/common/pythonVersion.ts, which normalizes Python version strings, supports comparison, and handles various version formats and prerelease suffixes.

Integration with environment selection:

  • Updated getLatest in src/managers/common/utils.ts to use PythonVersion for selecting the latest Python environment, improving reliability and correctness in version comparisons.

Testing improvements:

  • Added unit tests for PythonVersion in src/test/common/pythonVersion.unit.test.ts, covering normalization, comparison, error handling, and parsing of various version formats.
  • Added unit tests for getLatest in src/test/managers/common/utils.getLatest.unit.test.ts to verify correct selection among multiple Python environments, including handling of invalid and errored entries.

Dependency updates:

  • Updated imports in src/managers/common/utils.ts to include the new PythonVersion class.

Fixes #1743

@edvilme Eduardo Villalpando Mello (edvilme) changed the title Python versions Use PythonVersion class to parse and compare Python versions Aug 31, 2026
@edvilme Eduardo Villalpando Mello (edvilme) added the bug Issue identified by VS Code Team member as probable bug label Aug 31, 2026
Comment thread src/common/pythonVersion.ts

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces a new PythonVersion utility to normalize and compare Python interpreter version strings (including PET-style *.final.0 formats), and adopts it in environment sorting / “latest” selection and inline-script requires-python matching to address incorrect default interpreter selection (Fixes #1743).

Changes:

  • Added PythonVersion class for parsing, normalization, comparison, and requires-python specifier evaluation.
  • Updated environment selection utilities (sortEnvironments, getLatest) to use PythonVersion comparisons instead of direct PEP 440 comparisons.
  • Updated inline-script metadata version matching to use PythonVersion, with new/expanded unit tests.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/common/pythonVersion.ts Adds normalized Python version parsing/comparison and specifier matching.
src/managers/common/utils.ts Switches sorting and latest-environment selection to use PythonVersion.
src/common/inlineScript/metadata.ts Replaces release-segment logic with PythonVersion for requires-python matching.
src/test/common/pythonVersion.unit.test.ts Adds unit coverage for version normalization/comparison/specifiers.
src/test/managers/common/utils.getLatest.unit.test.ts Adds unit tests for updated latest-environment selection.
src/test/managers/common/utils.sortEnvironments.unit.test.ts Adds unit tests for updated environment sorting behavior.
src/test/common/inlineScript/metadata.unit.test.ts Extends tests to cover normalized interpreter version formats.
Suppressed comments (2)

src/common/pythonVersion.ts:41

  • PythonVersion currently rejects PEP 440-style versions with a leading v prefix (e.g. v3.12.4). Elsewhere in the repo, version parsing explicitly tolerates a leading v, so this can cause version comparisons / specifier matching to fail unexpectedly.
    constructor(version: string) {
        const normalizedVersion = version.trim();
        const match = PythonVersion.VERSION_PATTERN.exec(normalizedVersion);
        if (!match) {

src/test/common/pythonVersion.unit.test.ts:41

  • Add a test that wildcard specifiers with a leading v (e.g. ==v3.14.*) are accepted, since WILDCARD_PATTERN is being updated to allow it.
        assert.strictEqual(version.satisfies('==3.*'), true);
        assert.strictEqual(version.satisfies('==3.14.*'), true);
        assert.strictEqual(version.satisfies('==3.14.0.*'), true);

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/common/pythonVersion.ts
Comment thread src/common/inlineScript/metadata.ts
Comment thread src/test/common/pythonVersion.unit.test.ts

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.

Comment thread src/test/managers/common/utils.sortEnvironments.unit.test.ts Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@edvilme
Eduardo Villalpando Mello (edvilme) merged commit 7d70c65 into main Sep 1, 2026
123 of 124 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Issue identified by VS Code Team member as probable bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Latest Python version is not being used by default

4 participants