Skip to content

fix(python): only create subscope for FunctionDefinition#103

Merged
Arielwyy merged 1 commit intoantgroup:mainfrom
CrackTC:fix/python-scope
Jan 16, 2026
Merged

fix(python): only create subscope for FunctionDefinition#103
Arielwyy merged 1 commit intoantgroup:mainfrom
CrackTC:fix/python-scope

Conversation

@CrackTC
Copy link
Collaborator

@CrackTC CrackTC commented Jan 13, 2026

After investigating YASA-UAST python parser, I found that make subscope only when parent is FunctionDefinition is enough for fixing this bug.

There are 9 situations that generates ScopedStatement

  1. FunctionDef
  2. ClassDef
  3. If
  4. While
  5. For
  6. Lambda
  7. List Comprehension
  8. Try/Catch
  9. Match/Case

We check FunctionDefinition so 1 is OK

For 2 and 6, the parser generates FunctionDefinition as parent of ScopedStatement, so we covered these situations:
https://github.com/antgroup/YASA-UAST/blob/3bc130288bbf8135fe3ba8f945e225ea861d386f/parser-Python/uast/visitor.py#L352-L359
https://github.com/antgroup/YASA-UAST/blob/3bc130288bbf8135fe3ba8f945e225ea861d386f/parser-Python/uast/visitor.py#L813-L816

For 3, 4, 5, 7, 8 and 9, they aren't defining variable scopes in python syntax, so this is the bug per se and we shouldn't make subscope for them.


Note

Adjusts Python block scoping to align with Python semantics.

  • In python-analyzer.ts processScopedStatement, only create a new scope for block statements whose parent is FunctionDefinition; otherwise reuse the current scope
  • Keeps existing processing order (needCompileFirst vs non-first) and TryStatement handling unchanged

Written by Cursor Bugbot for commit 90ff6bb. This will update automatically on new commits. Configure here.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @CrackTC, 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 refines the Python analyzer's scope creation mechanism to accurately reflect Python's variable scoping rules. Previously, subscopes were indiscriminately created for various block statements, leading to incorrect scope management. The updated logic ensures that subscopes are only generated for blocks that are children of FunctionDefinition nodes, thereby resolving a bug related to improper scope creation for constructs like conditional statements and loops, which do not introduce new variable scopes in Python.

Highlights

  • Scope Creation Logic Refinement: The logic for creating subscopes within the Python analyzer has been updated to be more precise and align with Python's variable scoping rules.
  • Bug Fix for Incorrect Scoping: This change addresses a bug where subscopes were being incorrectly created for Python constructs that do not define new variable scopes, such as if, while, for statements, and comprehensions.
  • Targeted Subscope Generation: Subscopes are now exclusively created for block statements whose parent is a FunctionDefinition, ensuring correct scope management.

🧠 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 correctly addresses a bug in the Python analyzer where sub-scopes were being created for block statements that do not introduce new variable scopes in Python, such as if, for, and while. The fix correctly limits the creation of new scopes to ScopedStatement nodes within a FunctionDefinition, which aligns with Python's scoping rules. The implementation is correct. I've added one minor suggestion to improve code conciseness.

Comment on lines +747 to +751
let block_scope = scope
if (node.parent?.type === 'FunctionDefinition') {
// 只对函数体内的块语句创建子作用域,python的其他块语句不创建子作用域
block_scope = Scope.createSubScope(scopeName, scope, 'scope')
}
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 logic can be made more concise by using a ternary operator. This also allows block_scope to be declared with const, which is generally preferred for variables that are not reassigned.

Suggested change
let block_scope = scope
if (node.parent?.type === 'FunctionDefinition') {
// 只对函数体内的块语句创建子作用域,python的其他块语句不创建子作用域
block_scope = Scope.createSubScope(scopeName, scope, 'scope')
}
// 只对函数体内的块语句创建子作用域,python的其他块语句不创建子作用域
const block_scope =
node.parent?.type === 'FunctionDefinition'
? Scope.createSubScope(scopeName, scope, 'scope')
: scope

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

This PR is being reviewed by Cursor Bugbot

Details

Your team is on the Bugbot Free tier. On this plan, Bugbot will review limited PRs each billing cycle for each member of your team.

To receive Bugbot reviews on all of your PRs, visit the Cursor dashboard to activate Pro and start your 14-day free trial.

if (node.parent?.type === 'FunctionDefinition') {
// 只对函数体内的块语句创建子作用域,python的其他块语句不创建子作用域
block_scope = Scope.createSubScope(scopeName, scope, 'scope')
}
Copy link

Choose a reason for hiding this comment

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

List comprehensions may lose scope isolation in Python 3

Medium Severity

The change only creates subscopes for ScopedStatement nodes whose parent is FunctionDefinition. The PR description claims list comprehensions don't define scopes in Python, but this is incorrect for Python 3 where list comprehensions DO have their own scope (the loop variable doesn't leak to the enclosing scope). If the YASA-UAST parser doesn't wrap list comprehensions in FunctionDefinition nodes (as it does for lambdas and class bodies), comprehension variables would incorrectly be analyzed as belonging to the outer scope, potentially causing incorrect analysis results.

Fix in Cursor Fix in Web

@Arielwyy Arielwyy merged commit 4d28316 into antgroup:main Jan 16, 2026
2 checks passed
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.

2 participants