base#

class starlite.plugins.base.PluginProtocol#

Bases: Protocol[ModelT]

Base plugin protocol to be inherited when implementing plugins.

on_app_init(app: Starlite) None#

Receive the Starlite application instance before init is finalized and allow the plugin to update various attributes.

Examples

Parameters:

app – The Starlite instance.

Returns:

None

static is_plugin_supported_type(value: Any) TypeGuard[ModelT]#

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_pydantic_model_class(model_class: Type[ModelT], **kwargs: Any) Type[BaseModel]#

Given a model_class supported by the plugin, convert it to a subclass of the pydantic BaseModel.

Parameters:
  • model_class – A model class supported by the plugin.

  • **kwargs – Any additional kwargs.

Returns:

A pydantic model class.

from_pydantic_model_instance(model_class: Type[ModelT], pydantic_model_instance: BaseModel) ModelT#

Given an instance of a pydantic model created using a plugin’s to_pydantic_model_class, return an instance of the class from which that pydantic model has been created.

This class is passed in as the model_class kwarg.

Parameters:
  • model_class – A model class supported by the plugin.

  • pydantic_model_instance – A pydantic model instance.

Returns:

A model instance.

to_dict(model_instance: ModelT) Union[Dict[str, Any], Awaitable[Dict[str, Any]]]#

Given an instance of a model supported by the plugin, return a dictionary of serializable values.

Parameters:

model_instance – A model instance of the type supported by the plugin.

Notes

  • This method can be async as well.

Returns:

A string keyed dictionary of values.

from_dict(model_class: Type[ModelT], **kwargs: Any) ModelT#

Given a class supported by this plugin and a dict of values, create an instance of the class.

Parameters:
  • model_class – A model class supported by the plugin.

  • **kwargs – A string keyed mapping of values.

Returns:

A model instance.

__init__(*args, **kwargs)#
starlite.plugins.base.get_plugin_for_value(value: Any, plugins: List[PluginProtocol]) Optional[PluginProtocol]#

Return a plugin for handling the given value, if any plugin supports it.

Parameters:
  • value – An arbitrary value.

  • plugins – A list of plugins

Returns:

A plugin supporting the given value, or None.

class starlite.plugins.base.PluginMapping#

Bases: NamedTuple

Named tuple, mapping plugins > models.

plugin: PluginProtocol#

Alias for field number 0

model_class: Any#

Alias for field number 1

get_model_instance_for_value(value: Union[BaseModel, List[BaseModel], Tuple[BaseModel, ...]]) Any#

Given a value generated by plugin, return an instance of the original class.

Can also accept a list or tuple of values.

Parameters:

value – A pydantic model instance or sequence of instances.

Returns:

Any

static __new__(_cls, plugin: PluginProtocol, model_class: Any)#

Create new instance of PluginMapping(plugin, model_class)