Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions parser-Python/uast/visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,10 @@ def visit_FunctionDef(self, node):
if node.name == '__init__':
function_def._meta.isConstructor = True
# function_def.body.body.append(UNode.ReturnStatement(UNode.SourceLocation(), UNode.Meta(), UNode.Identifier(UNode.SourceLocation(), UNode.Meta(), 'self')))
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

Comment on lines +339 to +342

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')))

decorator_list = []
for decorator in node.decorator_list:
decorator_list.append(self.packPos(decorator, self.visit(decorator)))
Expand Down