FastUI
FastUI copied to clipboard
Select description gets attached to multiple field
In the demo here if we add a description to the first select as:
class SelectForm(BaseModel):
select_single: ToolEnum = Field(title='Select Single', description="First selector")
select_multiple: list[ToolEnum] = Field(title='Select Multiple')
search_select_single: str = Field(json_schema_extra={'search_url': '/api/forms/search'})
search_select_multiple: list[str] = Field(json_schema_extra={'search_url': '/api/forms/search'})
it produces the following, where we can see the description leaking to the next select:
However, I noticed that if I use a different Enum for the second selector this does not happen; e.g:
class ToolEnum2(str, enum.Enum):
hammer = 'hammer'
screwdriver = 'screwdriver'
saw = 'saw'
claw_hammer = 'claw_hammer'
class SelectForm(BaseModel):
select_single: ToolEnum = Field(title='Select Single', description="First selector")
select_multiple: list[ToolEnum2] = Field(title='Select Multiple')
search_select_single: str = Field(json_schema_extra={'search_url': '/api/forms/search'})
search_select_multiple: list[str] = Field(json_schema_extra={'search_url': '/api/forms/search'})
Note: adding a description to the second select in the initial case doesn't work either, the first description still keeps it spot:
class SelectForm(BaseModel):
select_single: ToolEnum = Field(title='Select Single', description="First selector")
select_multiple: list[ToolEnum] = Field(title='Select Multiple', description="Second selector")
search_select_single: str = Field(json_schema_extra={'search_url': '/api/forms/search'})
search_select_multiple: list[str] = Field(json_schema_extra={'search_url': '/api/forms/search'})
my 2 cents; I believe the description gets attached to the components by it's name and the component name is derived by the Enum name - though i am unsure how to solve it for now
Thanks for reporting, this looks like a bug.
Based on the output from the follwing the bug doesn't seem to be in Pydantic, but rather in FastUI.
PR welcome to fix this
import enum
from pydantic import BaseModel, Field
from devtools import debug
class ToolEnum(str, enum.Enum):
hammer = 'hammer'
screwdriver = 'screwdriver'
saw = 'saw'
claw_hammer = 'claw_hammer'
class SelectForm(BaseModel):
select_single: ToolEnum = Field(title='Select Single', description='First selector')
select_multiple: list[ToolEnum] = Field(title='Select Multiple')
search_select_single: str
search_select_multiple: list[str]
debug(SelectForm.model_json_schema())
foobar.py:20 <module>
SelectForm.model_json_schema(): {
'$defs': {
'ToolEnum': {
'enum': [
'hammer',
'screwdriver',
'saw',
'claw_hammer',
],
'title': 'ToolEnum',
'type': 'string',
},
},
'properties': {
'select_single': {
'allOf': [
{
'$ref': '#/$defs/ToolEnum',
},
],
'description': 'First selector',
'title': 'Select Single',
},
'select_multiple': {
'items': {
'$ref': '#/$defs/ToolEnum',
},
'title': 'Select Multiple',
'type': 'array',
},
'search_select_single': {
'title': 'Search Select Single',
'type': 'string',
},
'search_select_multiple': {
'items': {
'type': 'string',
},
'title': 'Search Select Multiple',
'type': 'array',
},
},
'required': [
'select_single',
'select_multiple',
'search_select_single',
'search_select_multiple',
],
'title': 'SelectForm',
'type': 'object',
} (dict) len=5