demessaging.backend.function module
Transform a python function into a corresponding pydantic model.
The BackendFunction model in this module generates subclasses based
upon a python class (similarly as the
BackendClass does it for classes).
Classes:
A base class for a function model. |
|
|
Configuration class for a backend module function. |
|
A class in the API suitable for RPC via DASF |
|
Functions:
|
Generate field for the return property. |
- class demessaging.backend.function.BackendFunction[source]
Bases:
BaseModelA base class for a function model.
Don’t use this model, rather use
create_model()method to generate new models.Attributes:
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
Methods:
create_model(func[, config, class_name])Create a new pydantic Model from a function.
Get the API info on the function.
model_json_schema(*args, **kwargs)Generates a JSON schema for a model class.
- backend_config: ClassVar[BackendFunctionConfig]
- classmethod create_model(func: Callable, config: FunctionConfig | None = None, class_name=None, **kwargs) Type[BackendFunction][source]
Create a new pydantic Model from a function.
- Parameters:
func (callable) – A function or method
config (FunctionConfig, optional) – The configuration to use. If given, this overrides the
__pulsar_config__of the given funcclass_name (str, optional) – The name for the generated subclass of
pydantic.BaseModel. If not given, the name of func is used**kwargs – Any other parameter for the
pydantic.create_model()function
- Returns:
The newly generated class that represents this function.
- Return type:
Subclass of BackendFunction
- classmethod get_api_info() FunctionAPIModel[source]
Get the API info on the function.
- model_config: ClassVar[ConfigDict] = {'arbitrary_types_allowed': True, 'validate_assignment': True}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- classmethod model_json_schema(*args, **kwargs) Dict[str, Any][source]
Generates a JSON schema for a model class.
- Parameters:
by_alias – Whether to use attribute aliases or not.
ref_template – The reference template.
union_format –
The format to use when combining schemas from unions together. Can be one of:
’any_of’: Use the [anyOf](https://json-schema.org/understanding-json-schema/reference/combining#anyOf)
keyword to combine schemas (the default). - ‘primitive_type_array’: Use the [type](https://json-schema.org/understanding-json-schema/reference/type) keyword as an array of strings, containing each type of the combination. If any of the schemas is not a primitive type (string, boolean, null, integer or number) or contains constraints/metadata, falls back to any_of.
schema_generator – To override the logic used to generate the JSON schema, as a subclass of GenerateJsonSchema with your desired modifications
mode – The mode in which to generate the schema.
- Returns:
The JSON schema for the given model class.
- return_model: ClassVar[Type[BaseModel]]
- class demessaging.backend.function.BackendFunctionConfig(*, doc: str = '', registry: ~demessaging.config.registry.ApiRegistry = <factory>, template: ~demessaging.template.Template = Template(name='function.py', folder=PosixPath('/home/docs/checkouts/readthedocs.org/user_builds/dasf/checkouts/latest/demessaging/templates'), suffix='.jinja2', context={}), name: str = '', signature: ~inspect.Signature | None = None, validators: ~typing.Dict[str, ~typing.List[~pydantic.types.ImportString | ~typing.Annotated[~typing.Callable, ~pydantic.functional_serializers.PlainSerializer(func=~demessaging.utils.object_to_string, return_type=PydanticUndefined, when_used=always)]]] = <factory>, serializers: ~typing.Dict[str, ~pydantic.types.ImportString | ~typing.Annotated[~typing.Callable, ~pydantic.functional_serializers.PlainSerializer(func=~demessaging.utils.object_to_string, return_type=PydanticUndefined, when_used=always)]] = <factory>, return_validators: ~typing.List[~pydantic.types.ImportString | ~typing.Annotated[~typing.Callable, ~pydantic.functional_serializers.PlainSerializer(func=~demessaging.utils.object_to_string, return_type=PydanticUndefined, when_used=always)]] | None = None, return_serializer: ~pydantic.types.ImportString | ~typing.Annotated[~typing.Callable, ~pydantic.functional_serializers.PlainSerializer(func=~demessaging.utils.object_to_string, return_type=PydanticUndefined, when_used=always)] | None = None, field_params: ~typing.Dict[str, ~typing.Dict[str, ~typing.Any]] = <factory>, returns: ~typing.Dict[str, ~typing.Any] = <factory>, return_annotation: ~typing.Any | None = None, annotations: ~typing.Dict[str, ~typing.Any] = <factory>, reporter_args: ~typing.Dict[str, ~deprogressapi.base.BaseReport] = <factory>, json_schema_extra: ~typing.Dict[str, ~typing.Any] = <factory>, function: ~typing.Any, class_name: str)[source]
Bases:
FunctionConfigConfiguration class for a backend module function.
- Parameters:
doc (str) – The documentation of the object. If empty, this will be taken from the corresponding
__doc__attribute.registry (demessaging.config.registry.ApiRegistry) – Utilities for imports and encoders.
template (demessaging.template.Template) – The
demessaging.template.Templatethat is used to render the function for the generated API.name (str) – The name of the function. If empty, this will be taken from the functions
__name__attribute.signature (Optional[inspect.Signature]) – The calling signature for the function. If empty, this will be taken from the function itself.
validators (Dict[str, List[Union[pydantic.types.ImportString, Annotated[Callable, PlainSerializer(func=<function object_to_string at 0x71be7455feb0>, return_type=PydanticUndefined, when_used='always')]]]]) – Custom validators for function arguments. This parameter is a mapping from function argument name to a list of callables that can be used as validator.
serializers (Dict[str, Union[pydantic.types.ImportString, Annotated[Callable, PlainSerializer(func=<function object_to_string at 0x71be7455feb0>, return_type=PydanticUndefined, when_used='always')]]]) – A mapping from function argument to serializing function that is then used for the
pydantic.functional_serializers.PlainSerializer.return_validators (Optional[List[Union[pydantic.types.ImportString, Annotated[Callable, PlainSerializer(func=<function object_to_string at 0x71be7455feb0>, return_type=PydanticUndefined, when_used='always')]]]]) – Validators for the return value. This parameter is a list of callables that can be used as validator for the return value.
return_serializer (Union[pydantic.types.ImportString, Annotated[Callable, PlainSerializer(func=<function object_to_string at 0x71be7455feb0>, return_type=PydanticUndefined, when_used='always')], NoneType]) – A function that is used to serialize the return value.
field_params (Dict[str, Dict[str, Any]]) – custom Field overrides for the constructor parameters. See
pydantic.Fields.Field()return_annotation (Optional[Any]) – The annotation for the return value.
annotations (Dict[str, Any]) – custom annotations for function parameters
reporter_args (Dict[str, deprogressapi.base.BaseReport]) – Arguments that use the dasf-progress-api
json_schema_extra (Dict[str, Any]) – Any extra parameter for the JSON schema export for the function
function (Any) – The function to call.
class_name (str) – Name of the model class
Attributes:
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
Methods:
Update the config from the corresponding function.
- function: Any
- model_config: ClassVar[ConfigDict] = {'arbitrary_types_allowed': True, 'extra': 'forbid'}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- registry: ApiRegistry
- return_validators: List[ImportString | Annotated[Callable, PlainSerializer(object_to_string)]] | None
- signature: inspect.Signature | None
- class demessaging.backend.function.FunctionAPIModel(*, name: str, rpc_schema: dict[str, Any], return_schema: dict[str, Any])[source]
Bases:
BaseModelA class in the API suitable for RPC via DASF
Attributes:
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- return_schema: JsonSchemaValue
- rpc_schema: JsonSchemaValue
- class demessaging.backend.function.ReturnModel(root: RootModelRootType = PydanticUndefined)[source]
Bases:
RootModelAttributes:
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- model_config: ClassVar[ConfigDict] = {'arbitrary_types_allowed': True}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- root: RootModelRootType
- demessaging.backend.function.get_return_model(docstring: Docstring, config: BackendFunctionConfig) Type[BaseModel][source]
Generate field for the return property.
- Parameters:
docstring (docstring_parser.Docstring) – The parser that analyzed the docstring
- Returns:
The pydantic field
- Return type:
Any