fix(python): support withitem __enter__ call#47
fix(python): support withitem __enter__ call#47CrackTC wants to merge 1 commit intoantgroup:mainfrom
Conversation
Summary of ChangesHello @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 addresses a parsing inaccuracy in the Python UAST visitor concerning Highlights
🧠 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 AssistThe 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
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 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
|
There was a problem hiding this comment.
Code Review
This pull request correctly adds support for the __enter__ call in with statements that use an as clause. However, it misses the case where a with statement is used without an as clause, where the __enter__ method should still be called for its side effects. I've provided a suggestion that handles both cases correctly and also improves the readability of the code.
| def visit_withitem(self, node): | ||
| if node.optional_vars is not None: | ||
| enter = UNode.MemberAccess(UNode.SourceLocation(), UNode.Meta(), | ||
| self.packPos(node.context_expr, self.visit(node.context_expr)), | ||
| UNode.Identifier(UNode.SourceLocation(), UNode.Meta(), "__enter__")) | ||
|
|
||
| call = UNode.CallExpression(UNode.SourceLocation(), UNode.Meta(), | ||
| self.packPos(node.context_expr, enter), []) | ||
|
|
||
| return UNode.VariableDeclaration(UNode.SourceLocation(), UNode.Meta(), | ||
| self.packPos(node.optional_vars, self.visit(node.optional_vars)), | ||
| self.packPos(node.context_expr, self.visit(node.context_expr)), False, | ||
| self.packPos(node.context_expr, call), False, | ||
| UNode.DynamicType(UNode.SourceLocation(), UNode.Meta())) | ||
| else: | ||
| return UNode.Noop(UNode.SourceLocation(), UNode.Meta()) |
There was a problem hiding this comment.
The current implementation correctly handles with <expr> as <var>: by generating a call to __enter__. However, it doesn't handle with <expr>: (without an as clause). In this case, __enter__ should still be called, but its return value is discarded. The current code returns a Noop, which is incorrect.
I've provided a suggestion that fixes this by moving the __enter__ call logic outside the if condition. The else branch now wraps this call in an ExpressionStatement to indicate it's called for its side effects. This change also improves readability by breaking down the complex node creation and location packing.
| def visit_withitem(self, node): | |
| if node.optional_vars is not None: | |
| enter = UNode.MemberAccess(UNode.SourceLocation(), UNode.Meta(), | |
| self.packPos(node.context_expr, self.visit(node.context_expr)), | |
| UNode.Identifier(UNode.SourceLocation(), UNode.Meta(), "__enter__")) | |
| call = UNode.CallExpression(UNode.SourceLocation(), UNode.Meta(), | |
| self.packPos(node.context_expr, enter), []) | |
| return UNode.VariableDeclaration(UNode.SourceLocation(), UNode.Meta(), | |
| self.packPos(node.optional_vars, self.visit(node.optional_vars)), | |
| self.packPos(node.context_expr, self.visit(node.context_expr)), False, | |
| self.packPos(node.context_expr, call), False, | |
| UNode.DynamicType(UNode.SourceLocation(), UNode.Meta())) | |
| else: | |
| return UNode.Noop(UNode.SourceLocation(), UNode.Meta()) | |
| def visit_withitem(self, node): | |
| context_uast = self.packPos(node.context_expr, self.visit(node.context_expr)) | |
| enter_access = UNode.MemberAccess(UNode.SourceLocation(), UNode.Meta(), | |
| context_uast, | |
| UNode.Identifier(UNode.SourceLocation(), UNode.Meta(), "__enter__")) | |
| self.packPos(node.context_expr, enter_access) | |
| enter_call = UNode.CallExpression(UNode.SourceLocation(), UNode.Meta(), enter_access, []) | |
| self.packPos(node.context_expr, enter_call) | |
| if node.optional_vars is not None: | |
| return UNode.VariableDeclaration(UNode.SourceLocation(), UNode.Meta(), | |
| self.packPos(node.optional_vars, self.visit(node.optional_vars)), | |
| enter_call, False, | |
| UNode.DynamicType(UNode.SourceLocation(), UNode.Meta())) | |
| else: | |
| return self.packPos(node, UNode.ExpressionStatement(UNode.SourceLocation(), UNode.Meta(), enter_call)) |
Note
Adds proper handling for Python
withitems.visit_withitemto call__enter__()oncontext_exprand initializeoptional_varsvia aVariableDeclarationcontext_exprwith aCallExpressionto__enter__(); returnsNoopwhen nooptional_varsWritten by Cursor Bugbot for commit 1f5ca17. This will update automatically on new commits. Configure here.