pydantic#

class litestar.contrib.pydantic.PydanticDTO#

Bases: AbstractDTO, Generic[T]

Support for domain modelling with Pydantic.

decode_builtins(value: dict[str, Any]) Any#

Decode a dictionary of Python values into an the DTO’s datatype.

decode_bytes(value: bytes) Any#

Decode a byte string into an the DTO’s datatype.

classmethod generate_field_definitions(model_type: type[pydantic_v1.BaseModel | pydantic_v2.BaseModel]) Generator[DTOFieldDefinition, None, None]#

Generate FieldDefinition instances from model_type.

Yields:

FieldDefinition instances.

classmethod detect_nested_field(field_definition: FieldDefinition) bool#

Return True if field_definition represents a nested model field.

Parameters:

field_definition – inspect type to determine if field represents a nested model.

Returns:

True if field_definition represents a nested model field.

class litestar.contrib.pydantic.PydanticInitPlugin#

Bases: InitPluginProtocol

__init__(prefer_alias: bool = False) None#
on_app_init(app_config: AppConfig) AppConfig#

Receive the AppConfig instance after on_app_init hooks have been called.

Examples

from litestar import Litestar, get
from litestar.di import Provide
from litestar.plugins import InitPluginProtocol


def get_name() -> str:
    return "world"


@get("/my-path")
def my_route_handler(name: str) -> dict[str, str]:
    return {"hello": name}


class MyPlugin(InitPluginProtocol):
    def on_app_init(self, app_config: AppConfig) -> AppConfig:
        app_config.dependencies["name"] = Provide(get_name)
        app_config.route_handlers.append(my_route_handler)
        return app_config


app = Litestar(plugins=[MyPlugin()])
Parameters:

app_config – The AppConfig instance.

Returns:

The app config object.

class litestar.contrib.pydantic.PydanticSchemaPlugin#

Bases: OpenAPISchemaPlugin

__init__(prefer_alias: bool = False) None#
static is_plugin_supported_type(value: Any) bool#

Given a value of indeterminate type, determine if this value is supported by the plugin.

This is called by the default implementation of is_plugin_supported_field() for backwards compatibility. User’s should prefer to override that method instead.

Parameters:

value – An arbitrary value.

Returns:

A bool indicating whether the value is supported by the plugin.

static is_undefined_sentinel(value: Any) bool#

Return True if value should be treated as an undefined field

static is_constrained_field(field_definition: FieldDefinition) bool#

Return True if the field should be treated as constrained. If returning True, constraints should be defined in the field’s extras

to_openapi_schema(field_definition: FieldDefinition, schema_creator: SchemaCreator) Schema#

Given a type annotation, transform it into an OpenAPI schema class.

Parameters:
  • field_definition – FieldDefinition instance.

  • schema_creator – An instance of the schema creator class

Returns:

An OpenAPI instance.

classmethod for_pydantic_model(field_definition: FieldDefinition, schema_creator: SchemaCreator) Schema#

Create a schema object for a given pydantic model class.

Parameters:
  • field_definition – FieldDefinition instance.

  • schema_creator – An instance of the schema creator class

Returns:

A schema instance.

class litestar.contrib.pydantic.PydanticPlugin#

Bases: InitPluginProtocol

A plugin that provides Pydantic integration.

__init__(prefer_alias: bool = False) None#

Initialize PydanticPlugin.

Parameters:

prefer_alias – OpenAPI and type_encoders will export by alias

on_app_init(app_config: AppConfig) AppConfig#

Configure application for use with Pydantic.

Parameters:

app_config – The AppConfig instance.

class litestar.contrib.pydantic.PydanticDIPlugin#

Bases: DIPlugin

has_typed_init(type_: Any) bool#

Return True if type_ has type information available for its __init__() method that cannot be extracted from this method’s type annotations (e.g. a Pydantic BaseModel subclass), and DIPlugin.get_typed_init() supports extraction of these annotations.

get_typed_init(type_: Any) tuple[inspect.Signature, dict[str, Any]]#

Return signature and type information about the type_s __init__() method.