From ac85517361b0ea297386d7f41ec86634c3bc7e46 Mon Sep 17 00:00:00 2001 From: ogabrielluiz Date: Tue, 11 Jun 2024 22:56:26 -0300 Subject: [PATCH] refactor: Update Input field type to allow None as default value Update the Input field type in the base.py file to allow None as the default value for the 'value' attribute. This change ensures consistency and improves code readability. --- .../base/langflow/template/field/base.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/backend/base/langflow/template/field/base.py b/src/backend/base/langflow/template/field/base.py index d2a5d870c..fe260c890 100644 --- a/src/backend/base/langflow/template/field/base.py +++ b/src/backend/base/langflow/template/field/base.py @@ -1,6 +1,6 @@ from enum import Enum from types import GenericAlias -from typing import Any, Callable, Optional, Union +from typing import Any, Callable, Optional, Union, _GenericAlias, _UnionGenericAlias, get_args, get_origin from pydantic import BaseModel, ConfigDict, Field, field_serializer, field_validator, model_serializer, model_validator @@ -36,7 +36,7 @@ class Input(BaseModel): multiline: bool = False """Defines if the field will allow the user to open a text editor. Default is False.""" - value: Any = "" + value: Any = None """The value of the field. Default is None.""" file_types: list[str] = Field(default=[], serialization_alias="fileTypes") @@ -117,11 +117,22 @@ class Input(BaseModel): # If the user passes CustomComponent as a type insteado of "CustomComponent" we need to convert it to a string # this should be done for all types # How to check if v is a type? - if isinstance(v, (type, GenericAlias)): + if isinstance(v, (type, _GenericAlias, GenericAlias, _UnionGenericAlias)): if isinstance(v, type): v = v.__name__ else: - v = str(v) + origin = get_origin(v) + args = get_args(v) + if origin and args: + v = f"{origin.__name__}[{', '.join(arg.__name__ if isinstance(arg, type) else str(arg) for arg in args)}]" + # if v is union with None (e.g Union[someType, NoneType]) we need to remove NoneType + # we can return Optional[someType] instead of Union[someType, NoneType] + if "NoneType" in v: + v = v.replace(", NoneType", "") + v = v.replace("Union[", "Optional[") + + else: + v = str(v) elif not isinstance(v, str): raise ValueError(f"type must be a string or a type, not {type(v)}") return v