dto#

Application DTO configuration

app.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]#
app.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

_summary_

Returns:

Configured DTO class

Return type:

DTOConfig

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

class app.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: object

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

  • ‘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.

  • ‘include’ mutually exclusive with ‘exclude’ - specifying both values will raise an

    ImproperlyConfiguredException.

rename_fields: dict[str, str]#

Mapping of field names, to new name.

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_fields are 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.

experimental_codegen_backend: bool | None = None#

Use the experimental codegen backend

forbid_unknown_fields: bool = False#

Raise an exception for fields present in the raw data that are not defined on the model

class app.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 ASGIConnection instance.

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 FieldDefinition instances from model_type.

Yields:

FieldDefinition instances.

classmethod detect_nested_field(field_definition: FieldDefinition) bool[source]#

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 app.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 ASGIConnection instance.

classmethod generate_field_definitions(model_type: type[DataclassProtocol]) Generator[DTOFieldDefinition, None, None][source]#

Generate FieldDefinition instances from model_type.

Yields:

FieldDefinition instances.

classmethod detect_nested_field(field_definition: FieldDefinition) bool[source]#

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.