callables
prefect.utilities.callables
Utilities for working with Python callables.
Functions
get_call_parameters
Bind a call to a function to get parameter/value mapping. Default values on the signature will be included if not overridden.
If the function has a __prefect_self__
attribute, it will be included as
the first parameter. This attribute is set when Prefect decorates a bound
method, so this approach allows Prefect to work with bound methods in a way
that is consistent with how Python handles them (i.e. users don’t have to
pass the instance argument to the method) while still making the implicit self
argument visible to all of Prefect’s parameter machinery (such as cache key
functions).
Raises a ParameterBindError if the arguments/kwargs are not valid for the function
get_parameter_defaults
Get default parameter values for a callable.
explode_variadic_parameter
Given a parameter dictionary, move any parameters stored in a variadic keyword argument parameter (i.e. **kwargs) into the top level.
Example:
collapse_variadic_parameters
Given a parameter dictionary, move any parameters stored not present in the signature into the variadic keyword argument.
Example:
parameters_to_args_kwargs
Convert a parameters
dictionary to positional and keyword arguments
The function must have an identical signature to the original function or this will return an empty tuple and dict.
call_with_parameters
Call a function with parameters extracted with get_call_parameters
The function must have an identical signature to the original function or this
will fail. If you need to send to a function with a different signature, extract
the args/kwargs using parameters_to_positional_and_keyword
directly
cloudpickle_wrapped_call
Serializes a function call using cloudpickle then returns a callable which will execute that call and return a cloudpickle serialized return value
This is particularly useful for sending calls to libraries that only use the Python
built-in pickler (e.g. anyio.to_process
and multiprocessing
) but may require
a wider range of pickling support.
parameter_docstrings
Given a docstring in Google docstring format, parse the parameter section and return a dictionary that maps parameter names to docstring.
Args:
docstring
: The function’s docstring.
Returns:
- Mapping from parameter names to docstrings.
process_v1_params
create_v1_schema
parameter_schema
Given a function, generates an OpenAPI-compatible description of the function’s arguments, including:
- name
- typing information
- whether it is required
- a default value
- additional constraints (like possible enum values)
Args:
fn
: The function whose arguments will be serialized
Returns:
- the argument schema
parameter_schema_from_entrypoint
Generate a parameter schema from an entrypoint string.
Will load the source code of the function and extract the signature and docstring to generate the schema.
Useful for generating a schema for a function when instantiating the function may not be possible due to missing imports or other issues.
Args:
entrypoint
: A string representing the entrypoint to a function. The string should be in the format ofmodule.path.to.function\:do_stuff
.
Returns:
- The parameter schema for the function.
generate_parameter_schema
Generate a parameter schema from a function signature and docstrings.
To get a signature from a function, use inspect.signature(fn)
or
_generate_signature_from_source(source_code, func_name)
.
Args:
signature
: The function signature.docstrings
: A dictionary mapping parameter names to docstrings.
Returns:
- The parameter schema.
raise_for_reserved_arguments
Raise a ReservedArgumentError if fn
has any parameters that conflict
with the names contained in reserved_arguments
.
expand_mapping_parameters
Generates a list of call parameters to be used for individual calls in a mapping operation.
Args:
func
: The function to be calledparameters
: A dictionary of parameters with iterables to be mapped over
Returns:
- A list of dictionaries to be used as parameters for each call in the mapping operation
Classes
ParameterSchema
Simple data model corresponding to an OpenAPI Schema
.
Methods: