Repository navigation
Conversation
Service callbacks can annotate their message, and an optional parameters argument, with pydantic models. By default the parameters come from the recipe step only; parameters_from lets a service merge in values from the message, or nested dictionaries such as message.parameters, in an explicit order of precedence. Services can opt in to substituting values from the recipe environment. Messages failing validation are logged and nacked without requeueing, or optionally dropped. CommonService.subscribe now rejects callbacks that can not be called with (rw, header, message), and points callbacks declaring a parameters argument at typed_handler, instead of failing when the first message arrives. Also adds a NonEmpty validator, for fields such as Annotated[Path, NonEmpty]. pydantic is added as a dependency.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds
workflows.recipe.typed.typed_handler, a decorator for recipe callbacks that validates incoming messages and recipe parameters with pydantic before the callback runs. This is so that we can replace the hand-written checks that services currently repeat, eg.isinstance(message, dict),parameters.get(...)with defaults, and building pydantic models by hand inside the callback.parametersobject (which is pulled out of the RecipeWrapper) so they cannot be accidentally passed toCommonService.subscribepydantic.TypeAdapteraccepts.parametersargument, the recipe parameters are assembled, validated against its annotation, and passed in as a keyword argument.The decorator has the following signature:
Where
on_invaliddefines the behaviour when a message fails parsing.substitute=Truereplaces$name/${name}in parameter string values with values from the recipe environment (and$$with$). It is off by default.parameters_fromhandles merging of parameters from multiple dictionaries/locationsWhere parameters come from
By default
parametersis built only from the current recipe step's parameters. Services that let incoming messages supply or override parameters say so explicitly withparameters_from, listing sources highest precedence first:Sources are
"step"or"message", optionally followed by.keyto select a nested dictionary. Merging is shallow, a key present in a higher source wins even if its value is falsy, and missing or non-dictionary sources are skipped. These cover the precedence orders currently implemented by hand (mostly withChainMapWithReplacement) in the dlstbx services. Invalid sources are rejected when the module is imported.Also
NonEmptyvalidator, for fields such asAnnotated[Path, NonEmpty]. A plainPathsilently turns""intoPath("."), andmin_lengthconstraints can't be applied to aPath.>=2) is added as a dependency. It is only imported byworkflows.recipe.typed.