Improves CustomComponents Errors (#748)
This commit is contained in:
commit
45d1b34197
3 changed files with 27 additions and 13 deletions
|
|
@ -49,7 +49,9 @@ class CustomComponent(Component, extra=Extra.allow):
|
||||||
reader = DirectoryReader("", False)
|
reader = DirectoryReader("", False)
|
||||||
|
|
||||||
for type_hint in TYPE_HINT_LIST:
|
for type_hint in TYPE_HINT_LIST:
|
||||||
if reader.is_type_hint_used_but_not_imported(type_hint, code):
|
if reader._is_type_hint_used_in_args(
|
||||||
|
"Optional", code
|
||||||
|
) and not reader._is_type_hint_imported("Optional", code):
|
||||||
error_detail = {
|
error_detail = {
|
||||||
"error": "Type hint Error",
|
"error": "Type hint Error",
|
||||||
"traceback": f"Type hint '{type_hint}' is used but not imported in the code.",
|
"traceback": f"Type hint '{type_hint}' is used but not imported in the code.",
|
||||||
|
|
|
||||||
|
|
@ -152,6 +152,7 @@ class DirectoryReader:
|
||||||
Check if a specific type hint is used in the
|
Check if a specific type hint is used in the
|
||||||
function definitions within the given code.
|
function definitions within the given code.
|
||||||
"""
|
"""
|
||||||
|
try:
|
||||||
module = ast.parse(code)
|
module = ast.parse(code)
|
||||||
|
|
||||||
for node in ast.walk(module):
|
for node in ast.walk(module):
|
||||||
|
|
@ -161,6 +162,9 @@ class DirectoryReader:
|
||||||
arg.annotation, type_hint_name
|
arg.annotation, type_hint_name
|
||||||
):
|
):
|
||||||
return True
|
return True
|
||||||
|
except SyntaxError:
|
||||||
|
# Returns False if the code is not valid Python
|
||||||
|
return False
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def _is_type_hint_in_arg_annotation(self, annotation, type_hint_name: str) -> bool:
|
def _is_type_hint_in_arg_annotation(self, annotation, type_hint_name: str) -> bool:
|
||||||
|
|
@ -204,8 +208,13 @@ class DirectoryReader:
|
||||||
return False, "Syntax error"
|
return False, "Syntax error"
|
||||||
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_in_args(
|
||||||
return False, "Type hint 'Optional' is used but not imported in the code."
|
"Optional", file_content
|
||||||
|
) and not self._is_type_hint_imported("Optional", file_content):
|
||||||
|
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())
|
||||||
|
|
|
||||||
|
|
@ -116,9 +116,12 @@ def instantiate_based_on_type(class_object, base_type, node_type, params):
|
||||||
|
|
||||||
|
|
||||||
def instantiate_custom_component(node_type, class_object, params):
|
def instantiate_custom_component(node_type, class_object, params):
|
||||||
class_object = get_function_custom(params.pop("code"))
|
# we need to make a copy of the params because we will be
|
||||||
|
# modifying it
|
||||||
|
params_copy = params.copy()
|
||||||
|
class_object = get_function_custom(params_copy.pop("code"))
|
||||||
custom_component = class_object()
|
custom_component = class_object()
|
||||||
built_object = custom_component.build(**params)
|
built_object = custom_component.build(**params_copy)
|
||||||
return built_object, {"repr": custom_component.custom_repr()}
|
return built_object, {"repr": custom_component.custom_repr()}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue