Skip to content

Add ReturnStatement for __init__#49

Open
CyanM0un wants to merge 1 commit intoantgroup:mainfrom
CyanM0un:return_self
Open

Add ReturnStatement for __init__#49
CyanM0un wants to merge 1 commit intoantgroup:mainfrom
CyanM0un:return_self

Conversation

@CyanM0un
Copy link
Contributor

@CyanM0un CyanM0un commented Feb 15, 2026

Note

Low Risk
Small, localized change to UAST generation for __init__ only; main risk is subtle downstream assumptions about constructors now having an explicit return self.

Overview
Ensures Python __init__ methods translated into UAST end with an explicit ReturnStatement returning self when they don’t already end with a return.

This is applied only for constructor functions (visit_FunctionDef when node.name == '__init__') and avoids duplicating a return if the last emitted statement is already a UNode.ReturnStatement.

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

@gemini-code-assist
Copy link

Summary of Changes

Hello @CyanM0un, 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 UAST generation process for Python by introducing specific handling for __init__ methods. It implements logic to automatically append a ReturnStatement for self to the UAST representation of __init__ methods, thereby ensuring a more consistent and complete UAST structure, particularly when the method's body is not empty and lacks an explicit return statement.

Highlights

  • init method UAST representation: Ensured that Python's __init__ methods are correctly represented in the Universal Abstract Syntax Tree (UAST) by implicitly adding a ReturnStatement for self at the end of their body. This addition is conditional, occurring only if the method's body is not empty and does not already conclude with a ReturnStatement.

🧠 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.

Changelog
  • parser-Python/uast/visitor.py
    • Added logic within visit_FunctionDef to check if the current function is an __init__ method.
    • Implemented a conditional check to append a ReturnStatement for self to the __init__ method's body in the UAST, only if the body is not empty and the last statement is not already a ReturnStatement.
Activity
  • No human activity has been recorded on this pull request since its creation.
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

@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.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.

This is the final PR Bugbot will review for you during this billing cycle

Your free Bugbot reviews will reset on February 27

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 len(body) > 0:
last_stmt = body[-1]
if not isinstance(last_stmt, UNode.ReturnStatement):
function_def.body.body.append(UNode.ReturnStatement(UNode.SourceLocation(), UNode.Meta(), UNode.Identifier(UNode.SourceLocation(), UNode.Meta(), 'self')))
Copy link

Choose a reason for hiding this comment

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

Constructor return added outside classes

Low Severity

visit_FunctionDef appends return self for any function named __init__, even when it is not a class method. This changes semantics for module-level or nested functions named __init__ by introducing a synthetic return value in parser-Python/uast/visitor.py.

Fix in Cursor Fix in Web

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 adds logic to implicitly append a return self statement to __init__ methods that don't have an explicit return. This helps in normalizing the UAST representation of constructors. The implementation is correct, but I've provided a suggestion to make the code more concise and improve readability.

Comment on lines +339 to +342
if len(body) > 0:
last_stmt = body[-1]
if not isinstance(last_stmt, UNode.ReturnStatement):
function_def.body.body.append(UNode.ReturnStatement(UNode.SourceLocation(), UNode.Meta(), UNode.Identifier(UNode.SourceLocation(), UNode.Meta(), 'self')))

Choose a reason for hiding this comment

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

medium

The nested if statements can be combined into a single, more concise condition. This improves readability by reducing nesting depth.

Additionally, the line that creates the ReturnStatement is very long. You might consider breaking it into multiple lines or extracting parts into local variables to adhere to common line length limits and improve readability.

Suggested change
if len(body) > 0:
last_stmt = body[-1]
if not isinstance(last_stmt, UNode.ReturnStatement):
function_def.body.body.append(UNode.ReturnStatement(UNode.SourceLocation(), UNode.Meta(), UNode.Identifier(UNode.SourceLocation(), UNode.Meta(), 'self')))
if not body or not isinstance(body[-1], UNode.ReturnStatement):
function_def.body.body.append(UNode.ReturnStatement(UNode.SourceLocation(), UNode.Meta(), UNode.Identifier(UNode.SourceLocation(), UNode.Meta(), 'self')))

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.

1 participant