feat: validate component inputs if types are given #629
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.
As I mention in #622 (comment), this MR adds validation of args, kwargs, slots, and data (returned from
get_context_data
) based on the types passed to theComponent
's generics.The reason I wanted to add this is following:
When I was adding the typing support to the components, I had two options:
ParamSpec
as one of the Component's generics, which would be bound to theget_context_data
. This way, theget_context_data
function would have been the source of truth for the type.Tuple
andTypedDict
. This way, theComponent.render
would still be correctly typed, but the signature ofget_context_data
could diverge.I went with option 2, because even I haven't worked with
ParamSpec
before, and I didn't want to ask users to pass around something which they might have never used before. It seemed complex. On the other hand, defining just tuples and typed dicts seems a lot more intuitive.But that means that one could add typing for the Component, and the args and kwargs declared in the types could be totally different from the actual implementation of
get_context_data
.So this MR adds some guard rails so that the component's implementation must be compatible with the declared args and kwargs types.
Altho, this is applicable only for Python 3.11 and later. In older versions, args are still validated, but TypedDicts like kwargs, slots, and data, are typed only, but not validated.