From 7f23334f9739feac8ca0541248578cd79fc7e329 Mon Sep 17 00:00:00 2001 From: gustavoschaedler Date: Fri, 28 Jul 2023 23:24:17 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(directory=5Freader.py):=20ad?= =?UTF-8?q?d=20check=20for=20usage=20of=20'Optional'=20type=20hint=20witho?= =?UTF-8?q?ut=20importing=20it=20=E2=9C=A8=20feat(directory=5Freader.py):?= =?UTF-8?q?=20add=20methods=20to=20check=20if=20a=20specific=20type=20hint?= =?UTF-8?q?=20is=20imported=20and=20used=20in=20function=20arguments=20?= =?UTF-8?q?=E2=9C=A8=20feat(directory=5Freader.py):=20add=20method=20to=20?= =?UTF-8?q?check=20if=20a=20type=20hint=20is=20used=20but=20not=20imported?= =?UTF-8?q?=20in=20the=20code=20=E2=9C=A8=20feat(directory=5Freader.py):?= =?UTF-8?q?=20add=20check=20for=20usage=20of=20'Optional'=20type=20hint=20?= =?UTF-8?q?without=20importing=20it=20in=20the=20process=5Ffile=20method?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../interface/custom/directory_reader.py | 57 ++++++++++++++++++- 1 file changed, 55 insertions(+), 2 deletions(-) diff --git a/src/backend/langflow/interface/custom/directory_reader.py b/src/backend/langflow/interface/custom/directory_reader.py index 6842dbee4..d24dbd1ab 100644 --- a/src/backend/langflow/interface/custom/directory_reader.py +++ b/src/backend/langflow/interface/custom/directory_reader.py @@ -129,21 +129,74 @@ class DirectoryReader: None, ) + def _is_type_hint_imported(self, type_hint_name: str, code: str) -> bool: + """ + Check if a specific type hint is imported + from the typing module in the given code. + """ + module = ast.parse(code) + + return any( + isinstance(node, ast.ImportFrom) + and node.module == "typing" + and any(alias.name == type_hint_name for alias in node.names) + for node in ast.walk(module) + ) + + def _is_type_hint_used_in_args(self, type_hint_name: str, code: str) -> bool: + """ + Check if a specific type hint is used in the + function definitions within the given code. + """ + module = ast.parse(code) + + for node in ast.walk(module): + if isinstance(node, ast.FunctionDef): + for arg in node.args.args: + if self._is_type_hint_in_arg_annotation( + arg.annotation, type_hint_name + ): + return True + return False + + def _is_type_hint_in_arg_annotation(self, annotation, type_hint_name: str) -> bool: + """ + Helper function to check if a type hint exists in an annotation. + """ + return ( + annotation is not None + and isinstance(annotation, ast.Subscript) + and isinstance(annotation.value, ast.Name) + and annotation.value.id == type_hint_name + ) + + def is_type_hint_used_but_not_imported( + self, type_hint_name: str, code: str + ) -> bool: + """ + Check if a type hint is used but not imported in the given code. + """ + return self._is_type_hint_used_in_args( + type_hint_name, code + ) and not self._is_type_hint_imported(type_hint_name, code) + def process_file(self, file_path): """ Process a file by validating its content and returning the result and content/error message. """ file_content = self.read_file_content(file_path) + if file_content is None: return False, f"Could not read {file_path}" - - if self.is_empty_file(file_content): + elif self.is_empty_file(file_content): return False, "Empty file" elif not self.validate_code(file_content): return False, "Syntax error" elif not self.validate_build(file_content): return False, "Missing build function" + elif self.is_type_hint_used_but_not_imported("Optional", file_content): + return False, "Name hint type 'Optional' is not defined" else: if self.compress_code_field: file_content = str(StringCompressor(file_content).compress_string())