Skip to content

Conversation

@aignas
Copy link
Collaborator

@aignas aignas commented Dec 27, 2025

This is a quick draft POC to outline how I imagine any versions in pip.parse being handled.

TODO:

  • More tests
  • Discuss how to handle the detection of available python interpreters -
    users can specify anything, so we should normalize, but where do we stop?

Fixes #3479.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @aignas, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces experimental changes to the pypi module, specifically enhancing how Python version strings are handled. The core objective is to allow users to specify more detailed Python versions (e.g., including patch, alpha, or release candidate identifiers) in pip.parse configurations. This is achieved by integrating a version parsing and normalization utility, ensuring consistency and correctness when these versions are used for interpreter detection, repository naming, and configuration settings. The changes aim to provide greater flexibility and accuracy in managing Python environments within the system.

Highlights

  • Version Normalization: Introduced a mechanism to parse and normalize Python version strings (e.g., '3.15.0alpha2' becomes '3.15.0a2') across various internal functions, ensuring consistent handling.
  • Flexible Python Version Handling: Updated pip.parse related logic to accept and correctly process more granular Python version specifications, including patch, alpha, and release candidate identifiers, beyond just major.minor.
  • Refactored Version Labeling: The version_label utility now uses the full normalized version string for generating labels, allowing for more precise identification and replacing all dots with a specified separator.
  • Expanded Test Coverage: Added new test cases to validate the handling of different Python version formats, including alpha and release candidate versions, ensuring the normalization logic works as expected.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces changes to allow any valid PEP440 version string for Python, normalizing them across the system. The core change is to consistently use version.parse() for normalization. My review identified a recurring high-severity issue where the code does not check if version.parse() returns None before accessing its attributes, which can lead to crashes with invalid version strings. I have provided comments and suggestions in multiple files to add the necessary checks by enabling strict parsing, which will make the implementation more robust.

# the `FeatureFlagInfo`?
#
# ensure that the values here are normalized
value = version.parse(value).string
Copy link
Contributor

Choose a reason for hiding this comment

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

high

version.parse() with the default strict=False can return None for invalid versions, which would cause a crash when accessing .string. Using strict=True will ensure it fails with a descriptive error message for invalid versions, which is safer and provides better user feedback.

Suggested change
value = version.parse(value).string
value = version.parse(value, strict = True).string

ver = version.parse(input)
value = _flag_value(ctx.attr._python_version_flag)
if value:
ver = version.parse(value)
Copy link
Contributor

Choose a reason for hiding this comment

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

high

For robustness, it's better to parse with strict=True. Although the version string should already be normalized at this point, this provides defense-in-depth and ensures a clear failure instead of a potential crash if an invalid version string is somehow passed here.

Suggested change
ver = version.parse(value)
ver = version.parse(value, strict = True)


def _major_minor_version(version_str):
def _normalize_version(version_str):
ver = version.parse(version_str)
Copy link
Contributor

Choose a reason for hiding this comment

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

high

version.parse() with the default strict=False can return None for invalid versions, causing a crash on .string access on the next line. Using strict=True will cause it to fail with a descriptive error message for invalid versions, which is safer.

    ver = version.parse(version_str, strict = True)

python_name = "python_{}_host".format(
pip_attr.python_version.replace(".", "_"),
# normalize the version when getting the available interpreter
version.parse(pip_attr.python_version).string.replace(".", "_"),
Copy link
Contributor

Choose a reason for hiding this comment

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

high

version.parse() with the default strict=False can return None for invalid versions, causing a crash on .string access. Using strict=True will cause it to fail with a descriptive error message for invalid versions, which is safer.

            version.parse(pip_attr.python_version, strict = True).string.replace(".", "_"),

minor, _, _ = version.partition(".")

return major + sep + minor
parsed = v.parse(version)
Copy link
Contributor

Choose a reason for hiding this comment

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

high

v.parse() with the default strict=False can return None for invalid versions, causing a crash on .string access on the next line. Using strict=True will cause it to fail with a descriptive error message for invalid versions, which is safer.

Suggested change
parsed = v.parse(version)
parsed = v.parse(version, strict = True)

"3.15.2": "3152",
}.items():
# All of the versions should be normalized
normalized_python_version = _version.parse(python_version).string
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

This call is unsafe as _version.parse() can return None. Using strict=True will make the test fail clearly if an invalid version string is ever added to the test cases, which is a good practice for test robustness.

Suggested change
normalized_python_version = _version.parse(python_version).string
normalized_python_version = _version.parse(python_version, strict = True).string

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

"attempting to create a duplicate library" when multiple python versions with same major+minor are used (different patch version)

1 participant