dtos¶
DTOs for the server.
DTO Library layer module.
- class byte_bot.server.lib.dto.DTOConfig(exclude: AbstractSet[str] = <factory>, include: AbstractSet[str] = <factory>, rename_fields: dict[str, str] = <factory>, rename_strategy: RenameStrategy | None = None, max_nested_depth: int = 1, partial: bool = False, underscore_fields_private: bool = True, experimental_codegen_backend: bool | None = None, forbid_unknown_fields: bool = False)[source]
 Bases:
objectControl the generated DTO.
- exclude: AbstractSet[str]
 Explicitly exclude fields from the generated DTO.
If exclude is specified, all fields not specified in exclude will be included by default.
Notes
- The field names are dot-separated paths to nested fields, e.g. 
"address.street"will exclude the
"street"field from a nested"address"model.
- The field names are dot-separated paths to nested fields, e.g. 
 - ‘exclude’ mutually exclusive with ‘include’ - specifying both values will raise an
 ImproperlyConfiguredException.
- include: AbstractSet[str]
 Explicitly include fields in the generated DTO.
If include is specified, all fields not specified in include will be excluded by default.
Notes
- The field names are dot-separated paths to nested fields, e.g. 
"address.street"will include the
"street"field from a nested"address"model.
- The field names are dot-separated paths to nested fields, e.g. 
 - ‘include’ mutually exclusive with ‘exclude’ - specifying both values will raise an
 ImproperlyConfiguredException.
- rename_strategy: RenameStrategy | None = None
 Rename all fields using a pre-defined strategy or a custom strategy.
The pre-defined strategies are: upper, lower, camel, pascal.
A custom strategy is any callable that accepts a string as an argument and return a string.
Fields defined in
rename_fieldsare ignored.
- max_nested_depth: int = 1
 The maximum depth of nested items allowed for data transfer.
- __init__(exclude: AbstractSet[str] = <factory>, include: AbstractSet[str] = <factory>, rename_fields: dict[str, str] = <factory>, rename_strategy: RenameStrategy | None = None, max_nested_depth: int = 1, partial: bool = False, underscore_fields_private: bool = True, experimental_codegen_backend: bool | None = None, forbid_unknown_fields: bool = False) None
 
- partial: bool = False
 Allow transfer of partial data.
- underscore_fields_private: bool = True
 Fields starting with an underscore are considered private and excluded from data transfer.
- forbid_unknown_fields: bool = False
 Raise an exception for fields present in the raw data that are not defined on the model
- class byte_bot.server.lib.dto.DataclassDTO(asgi_connection: ASGIConnection)[source]
 Bases:
AbstractDTO,Generic[T]Support for domain modelling with dataclasses.
Create an AbstractDTOFactory type.
- Parameters:
 asgi_connection – A
ASGIConnectioninstance.
- classmethod generate_field_definitions(model_type: type[DataclassProtocol]) Generator[DTOFieldDefinition, None, None][source]
 Generate
FieldDefinitioninstances frommodel_type.- Yields:
 FieldDefinitioninstances.
- classmethod detect_nested_field(field_definition: FieldDefinition) bool[source]
 Return
Trueiffield_definitionrepresents a nested model field.- Parameters:
 field_definition – inspect type to determine if field represents a nested model.
- Returns:
 Trueiffield_definitionrepresents a nested model field.
- class byte_bot.server.lib.dto.SQLAlchemyDTO(asgi_connection: ASGIConnection)[source]
 Bases:
AbstractDTO,Generic[T]Support for domain modelling with SQLAlchemy.
Create an AbstractDTOFactory type.
- Parameters:
 asgi_connection – A
ASGIConnectioninstance.
- config: ClassVar[SQLAlchemyDTOConfig]
 Config objects to define properties of the DTO.
- classmethod generate_field_definitions(model_type: type[DeclarativeBase]) Generator[DTOFieldDefinition, None, None][source]
 Generate DTO field definitions from a SQLAlchemy model.
- Parameters:
 model_type (Type[sqlalchemy.orm.DeclarativeBase]) – The SQLAlchemy model type to generate field definitions from.
- Yields:
 collections.abc.Generator[litestar.dto.data_structures.DTOFieldDefinition, None, None] – A generator yielding DTO field definitions.
- Raises:
 RuntimeError – If the mapper cannot be found for the model type.
- classmethod detect_nested_field(field_definition: FieldDefinition) bool[source]
 Return
Trueiffield_definitionrepresents a nested model field.- Parameters:
 field_definition – inspect type to determine if field represents a nested model.
- Returns:
 Trueiffield_definitionrepresents a nested model field.
- byte_bot.server.lib.dto.config(backend: Literal['sqlalchemy'] = 'sqlalchemy', exclude: AbstractSet[str] | None = None, rename_fields: dict[str, str] | None = None, rename_strategy: RenameStrategy | None = None, max_nested_depth: int | None = None, partial: bool | None = None) SQLAlchemyDTOConfig[source]
 - byte_bot.server.lib.dto.config(backend: Literal['dataclass'] = 'dataclass', exclude: AbstractSet[str] | None = None, rename_fields: dict[str, str] | None = None, rename_strategy: RenameStrategy | None = None, max_nested_depth: int | None = None, partial: bool | None = None) DTOConfig
 Construct a DTO config.
- Parameters:
 backend (Literal["dataclass", "sqlalchemy"], optional) – Backend to use. Defaults to “dataclass”.
exclude (AbstractSet[str] | None, optional) – Fields to exclude. Defaults to None.
rename_fields (dict[str, str] | None, optional) – Fields to rename. Defaults to None.
rename_strategy (RenameStrategy | None, optional) – Rename strategy to use. Defaults to None.
max_nested_depth (int | None, optional) – Max nested depth. Defaults to None.
partial (bool | None, optional) – Whether to make the DTO partial. Defaults to None.
- Returns:
 Configured DTO class
- Return type:
 DTOConfig
- byte_bot.server.lib.dto.dto_field(mark: Literal['read-only', 'write-only', 'private'] | Mark) dict[str, DTOField][source]
 Create a field metadata mapping.
- Parameters:
 mark – A DTO mark for the field, e.g., “read-only”.
- Returns:
 A dict for setting as field metadata, such as the dataclass “metadata” field key, or the SQLAlchemy “info” field.
Marking a field automates its inclusion/exclusion from DTO field definitions, depending on the DTO’s purpose.