From 1de0767eaaa281ba80926204b8169d13f588526f Mon Sep 17 00:00:00 2001 From: ogabrielluiz Date: Mon, 17 Jun 2024 16:32:55 -0300 Subject: [PATCH] refactor: Update StrictBoolean to CoalesceBool in inputs.py and validators.py --- src/backend/base/langflow/inputs/inputs.py | 10 +++++----- src/backend/base/langflow/inputs/validators.py | 16 +++++++++++++++- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/backend/base/langflow/inputs/inputs.py b/src/backend/base/langflow/inputs/inputs.py index ee0e913f9..540e1f894 100644 --- a/src/backend/base/langflow/inputs/inputs.py +++ b/src/backend/base/langflow/inputs/inputs.py @@ -2,7 +2,7 @@ from typing import Any, Callable, Optional, Union from pydantic import Field, field_validator -from langflow.inputs.validators import StrictBoolean +from langflow.inputs.validators import CoalesceBool from langflow.schema.data import Data from langflow.schema.message import Message @@ -35,7 +35,7 @@ class PromptInput(BaseInputMixin, ListableInputMixin): # Applying mixins to a specific input type class StrInput(BaseInputMixin, ListableInputMixin, DatabaseLoadMixin): # noqa: F821 field_type: Optional[SerializableFieldTypes] = FieldTypes.TEXT - load_from_db: StrictBoolean = False + load_from_db: CoalesceBool = False input_types: list[str] = ["Text"] """Defines if the field will allow the user to open a text editor. Default is False.""" @@ -84,12 +84,12 @@ class TextInput(StrInput): class MultilineInput(BaseInputMixin): field_type: Optional[SerializableFieldTypes] = FieldTypes.TEXT - multiline: StrictBoolean = True + multiline: CoalesceBool = True class SecretStrInput(BaseInputMixin, DatabaseLoadMixin): field_type: Optional[SerializableFieldTypes] = FieldTypes.PASSWORD - password: StrictBoolean = Field(default=True) + password: CoalesceBool = Field(default=True) input_types: list[str] = ["Text"] @@ -103,7 +103,7 @@ class FloatInput(BaseInputMixin, ListableInputMixin, RangeMixin): class BoolInput(BaseInputMixin, ListableInputMixin): field_type: Optional[SerializableFieldTypes] = FieldTypes.BOOLEAN - value: StrictBoolean = False + value: CoalesceBool = False class NestedDictInput(BaseInputMixin, ListableInputMixin): diff --git a/src/backend/base/langflow/inputs/validators.py b/src/backend/base/langflow/inputs/validators.py index e87557f19..7056265f8 100644 --- a/src/backend/base/langflow/inputs/validators.py +++ b/src/backend/base/langflow/inputs/validators.py @@ -2,4 +2,18 @@ from typing import Annotated from pydantic import PlainValidator -StrictBoolean = Annotated[bool, PlainValidator(lambda v: v is True or v is False)] + +def validate_boolean(value: bool) -> bool: + valid_trues = ["True", "true", "1", "yes"] + valid_falses = ["False", "false", "0", "no"] + if value in valid_trues: + return True + if value in valid_falses: + return False + if isinstance(value, bool): + return value + else: + raise ValueError("Value must be a boolean") + + +CoalesceBool = Annotated[bool, PlainValidator(validate_boolean)]