Skip to content

How to hint lists of fields #9119

Answered by uriyyo
Wyko asked this question in Question
Discussion options

You must be logged in to vote

Hi @Wyko,

If you are using pydantic v2 you can use Annotated to add Field to specific type:

from typing import Annotated, TypeAlias

from pydantic import Field, BaseModel

ConfigField: TypeAlias = Annotated[
    dict,
    Field(
        default_factory=dict,
        title="The configuration.",
        description="A dictionary containing the configuration.",
    )
]


class Config(BaseModel):
    config: ConfigField


class MultiConfig(BaseModel):
    configs: list[ConfigField]


multi_config = MultiConfig(
    configs=[
        {"key": "value"},
        {"key": "value"},
    ]
)

print(multi_config)

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by Wyko
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants