FastUI icon indicating copy to clipboard operation
FastUI copied to clipboard

Select description gets attached to multiple field

Open tim-x-y-z opened this issue 1 year ago • 1 comments

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: image

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'})

Screenshot 2024-02-09 at 17 59 48

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'})

image

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

tim-x-y-z avatar Feb 09 '24 18:02 tim-x-y-z

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

samuelcolvin avatar Feb 10 '24 19:02 samuelcolvin