base_dto#

class litestar.dto.base_dto.AbstractDTO#

Bases: Generic[T]

Base class for DTO types.

config: ClassVar[DTOConfig]#

Config objects to define properties of the DTO.

model_type: type[T]#

If annotation is an iterable, this is the inner type, otherwise will be the same as annotation.

__init__(asgi_connection: ASGIConnection) None#

Create an AbstractDTOFactory type.

Parameters:

asgi_connection – A ASGIConnection instance.

classmethod get_config_for_model_type(config: DTOConfig, model_type: type[Any]) DTOConfig#

Create a new configuration for this specific model_type, during the creation of the factory.

The returned config object will be set as the config attribute on the newly defined factory class.

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.

abstract classmethod generate_field_definitions(model_type: type[Any]) Generator[DTOFieldDefinition, None, None]#

Generate FieldDefinition instances from model_type.

Yields:

FieldDefinition instances.

abstract 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.

classmethod is_supported_model_type_field(field_definition: FieldDefinition) bool#

Check support for the given type.

Parameters:

field_definition – A FieldDefinition instance.

Returns:

Whether the type of the field definition is supported by the DTO.

classmethod create_for_field_definition(field_definition: FieldDefinition, handler_id: str, backend_cls: type[litestar.dto._backend.DTOBackend] | None = None) None#

Creates a DTO subclass for a field definition.

Parameters:
  • field_definition – A FieldDefinition instance.

  • handler_id – ID of the route handler for which to create a DTO instance.

  • backend_cls – Alternative DTO backend class to use

Returns:

None

classmethod create_openapi_schema(field_definition: FieldDefinition, handler_id: str, schema_creator: SchemaCreator) Reference | Schema#

Create an OpenAPI request body.

Parameters:
  • field_definition – A parsed type annotation that represents the annotation used on the handler.

  • handler_id – ID of the route handler for which to create a DTO instance.

  • schema_creator – A factory for creating schemas. Has a for_field_definition() method that accepts a FieldDefinition instance.

Returns:

OpenAPI request body.

classmethod resolve_generic_wrapper_type(field_definition: FieldDefinition) tuple[litestar.typing.FieldDefinition, litestar.typing.FieldDefinition, str] | None#

Handle where DTO supported data is wrapped in a generic container type.

Parameters:

field_definition – A parsed type annotation that represents the annotation used to narrow the DTO type.

Returns:

The data model type.

static get_model_type_hints(model_type: type[Any], namespace: dict[str, Any] | None = None) dict[str, FieldDefinition]#

Retrieve type annotations for model_type.

Parameters:
  • model_type – Any type-annotated class.

  • namespace – Optional namespace to use for resolving type hints.

Returns:

Parsed type hints for model_type resolved within the scope of its module.

static get_dto_config_from_annotated_type(field_definition: FieldDefinition) DTOConfig | None#

Extract data type and config instances from Annotated annotation.

Parameters:

field_definition – A parsed type annotation that represents the annotation used to narrow the DTO type.

Returns:

The type and config object extracted from the annotation.

classmethod resolve_model_type(field_definition: FieldDefinition) FieldDefinition#

Resolve the data model type from a parsed type.

Parameters:

field_definition – A parsed type annotation that represents the annotation used to narrow the DTO type.

Returns:

A FieldDefinition that represents the data model type.