Skip to content

API Reference

from_pydantic

from_pydantic(
    __var_or_model: Union[str, type[BaseModel]],
    model: Optional[type[BaseModel]] = None,
    *,
    exclude: Sequence[str] = (),
    rename: Optional[dict[str, str]] = None,
    shorten: Optional[dict[str, str]] = None,
    prefix: Optional[str] = None,
    parse_docstring: bool = True,
    docstring_style: Literal[
        "google", "numpy", "sphinx"
    ] = "google",
    extra_options: Optional[
        dict[str, _ParameterKwargs]
    ] = None,
    ignore_unsupported: Optional[bool] = False,
    unpack_list: bool = False
) -> Callable[[Callable[..., T]], Callable[..., T]]

Decorator to add fields from a Pydantic model as options to a Click command.

Parameters:

Name Type Description Default
__var_or_model Union[str, type[BaseModel]]

name of the variable that will receive the Pydantic model in the decorated function

required
model Optional[type[BaseModel]]

Pydantic model

None
exclude Sequence[str]

field names that won't be added to the command

()
rename Optional[dict[str, str]]

a mapping from field names to command line option names (this will override any prefix). Option names must start with two dashes

None
shorten Optional[dict[str, str]]

a mapping from field names to short command line option names. Option names must start with one dash

None
prefix Optional[str]

a prefix to add to option names (without any dash)

None
parse_docstring bool

if True and griffe is installed, parse the docstring of the Pydantic model and pass argument documentation to the Click help option

True
docstring_style Literal['google', 'numpy', 'sphinx']

style of the docstring (google, numpy or sphinx). Ignored if parse_docstring is False

'google'
extra_options Optional[dict[str, _ParameterKwargs]]

a mapping from field names to a dictionary of options passed to the click.option() function

None
ignore_unsupported Optional[bool]

ignore unsupported model fields instead of raising

False
unpack_list bool

if True, a list of nested models (e.g. list[Foo]) will be yield one command-line option for each field in the nested model. Each field can be specified multiple times. This API is experimental.

False

Returns:

Type Description
Callable[[Callable[..., T]], Callable[..., T]]

a decorator that adds options to a function

options: show_root_heading: false show_root_members_full_path: true show_root_full_path: true