Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Select description gets attached to multiple field #178

Closed
tim-habitat opened this issue Feb 9, 2024 · 1 comment · Fixed by #184
Closed

Select description gets attached to multiple field #178

tim-habitat opened this issue Feb 9, 2024 · 1 comment · Fixed by #184
Labels
bug Something isn't working
Milestone

Comments

@tim-habitat
Copy link
Contributor

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

@samuelcolvin samuelcolvin added the bug Something isn't working label Feb 10, 2024
@samuelcolvin
Copy link
Member

samuelcolvin commented Feb 10, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants