Allow __post_validate__() with context arguments but without **kwargs #105
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.
Currently, for implementing a context-sensitive
__post_validate__()method in dataclasses, it is required to accept arbitrary keyword arguments (**kwargs). If the__post_validate__()method does not accept those, NO context arguments will be passed to it at all.This can cause confusion and very annoying debugging, and also there's not really a reason to it other than a slightly simpler implementation.
This PR changes this:
__post_validate__()can now be defined with specific keyword-arguments (i.e. only those that your post validation method actually needs) without accepting**kwargs. TheDataclassValidatorwill make sure to only pass parameters to the method that it accepts.It is also allowed to define them as positional arguments (as long as they are not positional-only), but this is disencouraged and will raise a warning. Please always use keyword-only arguments (i.e.
def __post_validate__(self, *, some_param=None)instead ofdef __post_validate__(self, some_param=None)).