This repository was archived by the owner on Apr 29, 2024. It is now read-only.
fix(validation): accept null for optional union types. #24
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.
Validating a field that is an optional union will fail when the value is null:
The cause of this is that when determining if a field is optional in
is_optional_annotationthe simply checks the second argument of the Union.This works when the annotation is
Optional[X]which expands toUnion[X, NoneType]but not, for instance, withOptional[Union[X, Y]]whichexpands to
Union[X, Y, NoneType].We could check the last argument to
Unionbut that would still fail on the,slightly more contrived, example of
Union[Optional[X], Y]which expands toUnion[X, NoneType, Y]and for whichnullwould be perfectly valid.Therefore, in this implementation,
is_optional_annotationis dropped asthat involves iterating over the arguments to determine if one is
NoneTypeand we also need to iterate over the arguments in
extract_optional_annotationto produce the "inner annotation" so I do both in the same place now instead.
is_optional_annotationis not referenced anywhere else in the project, butas this affects what is possibly the external API this is possibly a breaking
change.
Adds tests for the examples in
test_fields.py.BREAKING CHANGE: removes
is_optional_annotationfrommolten.typing