Support __post_init__ for Struct types - #470
Merged
Merged
Conversation
Member
Author
|
A quick example: >>> import msgspec
>>> class Interval(msgspec.Struct):
... low: float
... high: float
...
... def __post_init__(self):
... if self.low > self.high:
... raise ValueError("`low` may not be greater than `high`")
>>> Interval(1, 2) # valid interval
Interval(low=1, high=2)
>>> Interval(2, 1) # invalid interval
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 6, in __post_init__
ValueError: `low` may not be greater than `high`
>>> msgspec.json.decode(b'{"low": 2, "high": 1}', type=Interval) # invalid interval from JSON
Traceback (most recent call last):
File "<stdin>", line 6, in __post_init__
ValueError: `low` may not be greater than `high`
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
msgspec.ValidationError: `low` may not be greater than `high` |
This adds support for an optional `__post_init__` hook method on `msgspec.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, any `ValueError` or `TypeError` raised in `__post_init__` will be wrapped in a `ValidationError` when decoding.
|
Amazing, thank you! |
This was referenced Jul 12, 2023
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.
This adds support for an optional
__post_init__hook method onmsgspec.Structtypes. 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_hookerrors, anyValueErrororTypeErrorraised in__post_init__will be wrapped in aValidationErrorwhen 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.