plugins#

class litestar.plugins.SerializationPluginProtocol#

Bases: Protocol

Protocol used to define a serialization plugin for DTOs.

supports_type(field_definition: FieldDefinition) bool#

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

Parameters:

field_definition – A parsed type.

Returns:

Whether the type is supported by the plugin.

create_dto_for_type(field_definition: FieldDefinition) type[AbstractDTO]#

Given a parsed type, create a DTO class.

Parameters:

field_definition – A parsed type.

Returns:

A DTO class.

__init__(*args, **kwargs)#
class litestar.plugins.DIPlugin#

Bases: ABC

Extend dependency injection

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

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

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

class litestar.plugins.CLIPlugin#

Bases: CLIPluginProtocol

Plugin protocol to extend the CLI Server Lifespan.

class litestar.plugins.InitPluginProtocol#

Bases: Protocol

Protocol used to define plugins that affect the application’s init process.

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.

__init__(*args, **kwargs)#
class litestar.plugins.OpenAPISchemaPluginProtocol#

Bases: Protocol

Plugin protocol to extend the support of OpenAPI schema generation for non-library types.

static is_plugin_supported_type(value: Any) bool#

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

Parameters:

value – An arbitrary value.

Returns:

A typeguard dictating whether the value is supported by the plugin.

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

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

Parameters:
  • field_definition – An OpenAPI instance.

  • schema_creator – An instance of the openapi SchemaCreator.

Returns:

An OpenAPI instance.

__init__(*args, **kwargs)#
class litestar.plugins.OpenAPISchemaPlugin#

Bases: OpenAPISchemaPluginProtocol

Plugin to extend the support of OpenAPI schema generation for non-library types.

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.

is_plugin_supported_field(field_definition: FieldDefinition) bool#

Given a FieldDefinition that represents an indeterminate type, determine if this value is supported by the plugin

Parameters:

field_definition – A parsed type.

Returns:

Whether the type 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

class litestar.plugins.CLIPluginProtocol#

Bases: Protocol

Plugin protocol to extend the CLI.

on_cli_init(cli: Group) None#

Called when the CLI is initialized.

This can be used to extend or override existing commands.

Parameters:

cli – The root click.Group of the Litestar CLI

Examples

from litestar import Litestar
from litestar.plugins import CLIPluginProtocol
from click import Group


class CLIPlugin(CLIPluginProtocol):
    def on_cli_init(self, cli: Group) -> None:
        @cli.command()
        def is_debug_mode(app: Litestar):
            print(app.debug)


app = Litestar(plugins=[CLIPlugin()])
__init__(*args, **kwargs)#