Support __post_init__
for Struct
types
#470
Merged
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.
This adds support for an optional
__post_init__
hook method onmsgspec.Struct
types. If defined, this method is called at the end of the generated__init__
method. It can be used to customize the builtin__init__
as needed, as well as provides a spot to do some additional validation.As with
dec_hook
errors, anyValueError
orTypeError
raised in__post_init__
will be wrapped in aValidationError
when decoding. In the long run I plan to add support for more granular (and obviously named) custom validator support, but for now doing some validation in__post_init__
is a fine and simple solution. There are other use cases for__post_init__
besides validation, so we'd want to add this ability either way.Fixes #442.