🐛 fix(custom_component.py): update error message to include the specific type hint that is used but not imported in the code

🐛 fix(directory_reader.py): update error message to include the specific type hint that is used but not imported in the code
This commit is contained in:
gustavoschaedler 2023-07-31 23:22:12 +01:00
commit 267d543420
2 changed files with 10 additions and 8 deletions

View file

@ -30,6 +30,8 @@ class CustomComponent(Component, extra=Extra.allow):
return self.field_config return self.field_config
def _class_template_validation(self, code: str) -> bool: def _class_template_validation(self, code: str) -> bool:
TYPE_HINT_LIST = ["Optional", "Prompt", "PromptTemplate", "LLMChain"]
if not code: if not code:
raise HTTPException( raise HTTPException(
status_code=400, status_code=400,
@ -40,14 +42,14 @@ class CustomComponent(Component, extra=Extra.allow):
) )
reader = DirectoryReader("", False) reader = DirectoryReader("", False)
if reader.is_type_hint_used_but_not_imported("Optional", code):
raise HTTPException( for type_hint in TYPE_HINT_LIST:
status_code=400, if reader.is_type_hint_used_but_not_imported(type_hint, code):
detail={ error_detail = {
"error": "Type hint Error", "error": "Type hint Error",
"traceback": "Name type hint 'Optional' is not defined", "traceback": f"Type hint '{type_hint}' is used but not imported in the code.",
}, }
) raise HTTPException(status_code=400, detail=error_detail)
def is_check_valid(self) -> bool: def is_check_valid(self) -> bool:
return self._class_template_validation(self.code) return self._class_template_validation(self.code)

View file

@ -196,7 +196,7 @@ class DirectoryReader:
elif not self.validate_build(file_content): elif not self.validate_build(file_content):
return False, "Missing build function" return False, "Missing build function"
elif self.is_type_hint_used_but_not_imported("Optional", file_content): elif self.is_type_hint_used_but_not_imported("Optional", file_content):
return False, "Name type hint 'Optional' is not defined" return False, "Type hint 'Optional' is used but not imported in the code."
else: else:
if self.compress_code_field: if self.compress_code_field:
file_content = str(StringCompressor(file_content).compress_string()) file_content = str(StringCompressor(file_content).compress_string())