refactor: Update StrictBoolean to CoalesceBool in inputs.py and validators.py
This commit is contained in:
parent
1c1418596a
commit
1de0767eaa
2 changed files with 20 additions and 6 deletions
|
|
@ -2,7 +2,7 @@ from typing import Any, Callable, Optional, Union
|
||||||
|
|
||||||
from pydantic import Field, field_validator
|
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.data import Data
|
||||||
from langflow.schema.message import Message
|
from langflow.schema.message import Message
|
||||||
|
|
||||||
|
|
@ -35,7 +35,7 @@ class PromptInput(BaseInputMixin, ListableInputMixin):
|
||||||
# Applying mixins to a specific input type
|
# Applying mixins to a specific input type
|
||||||
class StrInput(BaseInputMixin, ListableInputMixin, DatabaseLoadMixin): # noqa: F821
|
class StrInput(BaseInputMixin, ListableInputMixin, DatabaseLoadMixin): # noqa: F821
|
||||||
field_type: Optional[SerializableFieldTypes] = FieldTypes.TEXT
|
field_type: Optional[SerializableFieldTypes] = FieldTypes.TEXT
|
||||||
load_from_db: StrictBoolean = False
|
load_from_db: CoalesceBool = False
|
||||||
input_types: list[str] = ["Text"]
|
input_types: list[str] = ["Text"]
|
||||||
"""Defines if the field will allow the user to open a text editor. Default is False."""
|
"""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):
|
class MultilineInput(BaseInputMixin):
|
||||||
field_type: Optional[SerializableFieldTypes] = FieldTypes.TEXT
|
field_type: Optional[SerializableFieldTypes] = FieldTypes.TEXT
|
||||||
multiline: StrictBoolean = True
|
multiline: CoalesceBool = True
|
||||||
|
|
||||||
|
|
||||||
class SecretStrInput(BaseInputMixin, DatabaseLoadMixin):
|
class SecretStrInput(BaseInputMixin, DatabaseLoadMixin):
|
||||||
field_type: Optional[SerializableFieldTypes] = FieldTypes.PASSWORD
|
field_type: Optional[SerializableFieldTypes] = FieldTypes.PASSWORD
|
||||||
password: StrictBoolean = Field(default=True)
|
password: CoalesceBool = Field(default=True)
|
||||||
input_types: list[str] = ["Text"]
|
input_types: list[str] = ["Text"]
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -103,7 +103,7 @@ class FloatInput(BaseInputMixin, ListableInputMixin, RangeMixin):
|
||||||
|
|
||||||
class BoolInput(BaseInputMixin, ListableInputMixin):
|
class BoolInput(BaseInputMixin, ListableInputMixin):
|
||||||
field_type: Optional[SerializableFieldTypes] = FieldTypes.BOOLEAN
|
field_type: Optional[SerializableFieldTypes] = FieldTypes.BOOLEAN
|
||||||
value: StrictBoolean = False
|
value: CoalesceBool = False
|
||||||
|
|
||||||
|
|
||||||
class NestedDictInput(BaseInputMixin, ListableInputMixin):
|
class NestedDictInput(BaseInputMixin, ListableInputMixin):
|
||||||
|
|
|
||||||
|
|
@ -2,4 +2,18 @@ from typing import Annotated
|
||||||
|
|
||||||
from pydantic import PlainValidator
|
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)]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue