Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor parse_return_statement method to handle nested returns #1549

Merged
merged 1 commit into from Mar 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 24 additions & 4 deletions src/backend/langflow/interface/custom/code_parser/code_parser.py
Expand Up @@ -6,6 +6,8 @@

from cachetools import TTLCache, cachedmethod, keys
from fastapi import HTTPException


from langflow.interface.custom.schema import CallableCodeDetails, ClassCodeDetails


Expand Down Expand Up @@ -234,10 +236,28 @@ def parse_function_body(self, node: ast.FunctionDef) -> List[str]:

def parse_return_statement(self, node: ast.FunctionDef) -> bool:
"""
Parses the return statement of a function or method node.
"""

return any(isinstance(n, ast.Return) for n in node.body)
Parses the return statement of a function or method node, including nested returns.
"""

def has_return(node):
if isinstance(node, ast.Return):
return True
elif isinstance(node, ast.If):
return any(has_return(child) for child in node.body) or any(has_return(child) for child in node.orelse)
elif isinstance(node, ast.Try):
return (
any(has_return(child) for child in node.body)
or any(has_return(child) for child in node.handlers)
or any(has_return(child) for child in node.finalbody)
)
elif isinstance(node, (ast.For, ast.While)):
return any(has_return(child) for child in node.body) or any(has_return(child) for child in node.orelse)
elif isinstance(node, ast.With):
return any(has_return(child) for child in node.body)
else:
return False

return any(has_return(child) for child in node.body)

def parse_assign(self, stmt):
"""
Expand Down