From 4583d30f4c29c8de83556a197e0221da79677013 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Fri, 1 Dec 2023 13:12:19 -0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(code=5Fparser.py):=20add=20sup?= =?UTF-8?q?port=20for=20parsing=20return=20statement=20in=20function=20or?= =?UTF-8?q?=20method=20nodes=20=F0=9F=90=9B=20fix(code=5Fparser.py):=20fix?= =?UTF-8?q?=20import=20formatting=20to=20improve=20readability=20=E2=9C=A8?= =?UTF-8?q?=20feat(code=5Fparser.py):=20add=20support=20for=20parsing=20fu?= =?UTF-8?q?nction=20arguments=20and=20body=20in=20function=20or=20method?= =?UTF-8?q?=20nodes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../langflow/interface/custom/code_parser.py | 25 ++++++++++++++++--- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/backend/langflow/interface/custom/code_parser.py b/src/backend/langflow/interface/custom/code_parser.py index cb42b8142..c24653482 100644 --- a/src/backend/langflow/interface/custom/code_parser.py +++ b/src/backend/langflow/interface/custom/code_parser.py @@ -6,7 +6,8 @@ from typing import Any, Dict, List, Type, Union from cachetools import TTLCache, cachedmethod, keys from fastapi import HTTPException -from langflow.interface.custom.schema import CallableCodeDetails, ClassCodeDetails +from langflow.interface.custom.schema import (CallableCodeDetails, + ClassCodeDetails) class CodeSyntaxError(HTTPException): @@ -56,6 +57,9 @@ class CodeParser: ast.Assign: self.parse_global_vars, } + + + def __get_tree(self): """ Parses the provided code to validate its syntax. @@ -79,6 +83,7 @@ class CodeParser: if handler := self.handlers.get(type(node)): # type: ignore handler(node) # type: ignore + def parse_imports(self, node: Union[ast.Import, ast.ImportFrom]) -> None: """ Extracts "imports" from the code, including aliases. @@ -149,12 +154,16 @@ class CodeParser: # Handle cases where the type is not found in the constructed environment pass + func = CallableCodeDetails( - name=node.name, doc=ast.get_docstring(node), args=[], body=[], return_type=return_type or get_data_type() + name=node.name, + doc=ast.get_docstring(node), + args= self.parse_function_args(node), + body= self.parse_function_body(node), + return_type=return_type or get_data_type(), + has_return=self.parse_return_statement(node), ) - func.args = self.parse_function_args(node) - func.body = self.parse_function_body(node) return func.model_dump() @@ -230,6 +239,14 @@ class CodeParser: """ return [ast.unparse(line) for line in node.body] + 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) + + def parse_assign(self, stmt): """ Parses an Assign statement and returns a dictionary