Skip to content

Conversation

@toaction
Copy link
Contributor

@toaction toaction commented Sep 29, 2025

fix #22

@gemini-code-assist
Copy link

Summary of Changes

Hello @toaction, 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 significantly enhances the system's capability by introducing comprehensive support for Python global variables. It extends the pydump tool to accurately identify and extract various global variable types from Python modules and integrates new logic into the pygen tool to generate corresponding Go variable declarations. These Go variables are linked to their Python counterparts using //go:linkname directives, enabling seamless interoperability with Python modules that define global state. This feature expands the scope of Python-Go integration beyond just functions.

Highlights

  • Global Variable Type Recognition: A new map, pyVarTypes, has been introduced in _xtool/pydump/pydump.go to recognize common Python global variable types such as int, float, complex, bool, str, list, tuple, set, dict, and NoneType.
  • Global Variable Extraction: The pydump function in _xtool/pydump/pydump.go has been updated to identify and extract global variables based on their types, appending them to the modInstance.Variables field.
  • Module Structure Enhancement: The Module struct in symbol/symbol.go now includes a Variables []*Symbol field, allowing it to store information about the global variables found within a Python module.
  • Go Variable Generation Logic: A new file, tool/pygen/genvar.go, has been added. It contains genVars and genVar functions responsible for generating Go variable declarations and applying //go:linkname directives to link them to their corresponding Python global variables.
  • Integration into Code Generation: The genMod function in tool/pygen/pygen.go now calls ctx.genVars to ensure that global variables are processed and their Go bindings are generated as part of the overall code generation flow.
  • New Test Case for Variables: A new test function, TestGenVar, has been added to tool/pygen/pygen_test.go to validate the correct generation of Go code for various Python global variable types, including new test data files demo.py and expect.go.
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

@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 support for generating Go bindings for global variables from Python modules. The changes are well-structured, adding logic to pydump to identify variable types, updating the symbol.Module struct, and implementing the code generation in a new genvar.go file. The new functionality is also covered by a test case, which is great. I have one suggestion regarding the performance of the name conflict resolution logic, which could be improved for larger modules.

@niupilot
Copy link
Contributor

niupilot bot commented Sep 29, 2025

Code Review Summary

Excellent implementation of global variable support! 🎯 The feature successfully extends existing patterns and maintains codebase consistency.

Critical Issues to Address:

  • Missing error handling in genVars/genVar functions
  • O(n²) performance bottleneck in name conflict resolution
  • Input validation gaps for security

Strengths:

  • Proper Go linkname directive usage
  • Consistent with existing function generation patterns
  • Good test coverage included

Please address the inline comments before merging. The security and performance issues are particularly important for production use.

@toaction
Copy link
Contributor Author

need review @luoliwoshang

Comment on lines 23 to 34
var pyVarTypes = map[string]struct{}{
"int": {},
"float": {},
"complex": {},
"bool": {},
"str": {},
"list": {},
"tuple": {},
"set": {},
"dict": {},
"NoneType": {},
}
Copy link
Member

Choose a reason for hiding this comment

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

the name pyVarTypes is misleading - it suggests "variable types" but actually stores Python data type names. Variables don't have types, their values do. Are all Python basic types included here? It seems to be missing
bytes, frozenset, and range types.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The variable types have been modified to data types: fc467d8

Only a portion of the data types (those that can be directly declared) have been converted here. For a complete reference of all Python data types, please visit: #22

Copy link
Member

Choose a reason for hiding this comment

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

When you mention "those that can be directly declared", are you referring to global variables that can be created using literal syntax?
For constructor-declared global variables, they are still global variables. Is this not intended to be supported, or is there currently no way to support it? If it's temporarily unsupported but we plan to support it in the future, this should be clearly documented in the comments.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Due to hardcoding issues, currently we determine whether to generate corresponding Go binding code by checking if the variable's corresponding data type is a basic data type:

// variables
if _, ok := pyBasicDataTypes[sym.Type]; ok {
    modInstance.Variables = append(modInstance.Variables, sym)
    continue
}

After resolving the hardcoding issue, we will support more types of global variables, such as class variables:

class Animal:
    pass

DOG = Animal()

Copy link
Member

Choose a reason for hiding this comment

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

The current whitelisting approach only supports a small subset of global variables. Perhaps we could find alternative ways to support this feature! The current architecture seems a bit too ad-hoc.

@codecov
Copy link

codecov bot commented Oct 10, 2025

Codecov Report

❌ Patch coverage is 95.83333% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 47.05%. Comparing base (f84dd79) to head (fc467d8).

Files with missing lines Patch % Lines
tool/pygen/pygen.go 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #39      +/-   ##
==========================================
+ Coverage   44.46%   47.05%   +2.58%     
==========================================
  Files           6        7       +1     
  Lines         452      476      +24     
==========================================
+ Hits          201      224      +23     
- Misses        232      233       +1     
  Partials       19       19              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

feat: add package variable

2 participants