From e7be07ebecafa95e0b05553891e371dc6fc9e1c1 Mon Sep 17 00:00:00 2001 From: ogabrielluiz Date: Wed, 12 Jun 2024 12:34:38 -0300 Subject: [PATCH] refactor: Update field input classes for different data types This commit adds field input classes for different data types in the `inputs.py` file. The purpose of this change is to improve the organization and separation of concerns in the codebase. Each input class specifies the field type and provides default values or options where applicable. This update enhances the maintainability and extensibility of the codebase. --- .../langflow/template/field/input_mixin.py | 9 ++- .../base/langflow/template/field/inputs.py | 63 +++++++++++-------- 2 files changed, 44 insertions(+), 28 deletions(-) diff --git a/src/backend/base/langflow/template/field/input_mixin.py b/src/backend/base/langflow/template/field/input_mixin.py index e58749c92..a053c62b4 100644 --- a/src/backend/base/langflow/template/field/input_mixin.py +++ b/src/backend/base/langflow/template/field/input_mixin.py @@ -1,7 +1,7 @@ from enum import Enum -from typing import Any, Optional +from typing import Annotated, Any, Optional -from pydantic import BaseModel, ConfigDict, Field, field_validator +from pydantic import BaseModel, ConfigDict, Field, PlainSerializer, field_validator from langflow.field_typing.range_spec import RangeSpec @@ -18,11 +18,14 @@ class FieldTypes(str, Enum): PROMPT = "Prompt" +SerializableFieldTypes = Annotated[FieldTypes, PlainSerializer(lambda v: v.value, return_type=str)] + + # Base mixin for common input field attributes and methods class BaseInputMixin(BaseModel): model_config = ConfigDict(arbitrary_types_allowed=True) - field_type: Optional[FieldTypes] = Field(default=FieldTypes.TEXT) + field_type: Optional[SerializableFieldTypes] = Field(default=FieldTypes.TEXT) required: bool = False """Specifies if the field is required. Defaults to False.""" diff --git a/src/backend/base/langflow/template/field/inputs.py b/src/backend/base/langflow/template/field/inputs.py index c71061f6e..859ee8de3 100644 --- a/src/backend/base/langflow/template/field/inputs.py +++ b/src/backend/base/langflow/template/field/inputs.py @@ -1,46 +1,59 @@ -from pydantic import SecretStr +from typing import Callable, Optional, Union -from langflow.field_typing.constants import NestedDict -from langflow.template.field.base import Input +from pydantic import Field + +from langflow.template.field.input_mixin import ( + BaseInputMixin, + DatabaseLoadMixin, + DropDownMixin, + FieldTypes, + FileMixin, + ListableInputMixin, + RangeMixin, +) -class StrInput(Input): - field_type: str | type | None = str +class PromptInput(BaseInputMixin, ListableInputMixin): + field_type = FieldTypes.PROMPT -class SecretStrInput(Input): - field_type: str | type | None = SecretStr - password = True +# Applying mixins to a specific input type +class StrInput(BaseInputMixin, ListableInputMixin): # noqa: F821 + field_type = FieldTypes.TEXT + multiline: bool = Field(default=False) + """Defines if the field will allow the user to open a text editor. Default is False.""" -class IntInput(Input): - field_type: str | type | None = int +class SecretStrInput(BaseInputMixin, DatabaseLoadMixin): + field_type = FieldTypes.PASSWORD + password: bool = Field(default=True) -class FloatInput(Input): - field_type: str | type | None = float +class IntInput(BaseInputMixin, ListableInputMixin, RangeMixin): + field_type = FieldTypes.INTEGER -class BoolInput(Input): - field_type: str | type | None = bool +class FloatInput(BaseInputMixin, ListableInputMixin, RangeMixin): + field_type = FieldTypes.FLOAT -class NestedDictInput(Input): - field_type: str | type | None = NestedDict +class BoolInput(BaseInputMixin, ListableInputMixin): + field_type = FieldTypes.BOOLEAN -class DictInput(Input): - field_type: str | type | None = dict +class NestedDictInput(BaseInputMixin, ListableInputMixin): + field_type = FieldTypes.NESTED_DICT -class ListInput(Input): - is_list = True +class DictInput(BaseInputMixin, ListableInputMixin): + field_type = FieldTypes.DICT -class DropdownInput(Input): - field_type: str | type | None = str - options = [] +class DropdownInput(BaseInputMixin, DropDownMixin): + field_type = FieldTypes.TEXT + options: Optional[Union[list[str], Callable]] = None + """List of options for the field. Only used when is_list=True. Default is an empty list.""" -class FileInput(Input): - field_type: str | type | None = str +class FileInput(BaseInputMixin, ListableInputMixin, FileMixin): + field_type = FieldTypes.FILE