Implements validates_with_beartype
#128
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.
(Illustration by Keith Negley)
As promised, this PR adds
validates_with_beartype
, which is designed to be used with the new zen-wrappers feature. Everything that you instantiate can now be validated by beartype!The Basics
This is designed to be used with the
zen_wrappers
feature ofbuilds
.Note that sequence-coercion is enabled to ensure smooth compatibility with Hydra.
Some Implementation Details
This would have been a really tiny PR except...
@beartype
will not coerce sequence-type data based on their annotations. This is totally fair/expected, but it is a show-stopper for use with Hydra, as all sequential data will be pass from configs as lists. Thusvalidates_with_beartype
leverageshydra_zen._utils.coerce.coerce_sequences
.This is a wrapper that checks if a function has any fields in its signature that are annotated with a (non-string) sequence-type annotation; if it does, then it will wrap the function so that list (or list-config)-inputs will be cast to the annotated sequence (e.g a tuple). Only list/ListConfig values will be converted; others will be passed as-is so that beartype will flag them appropriately.
Functions with no such annotations are passed-through unchanged.
Writing
coerce_sequences
sucked! Testingcoerce_sequences
sucked! It was important to make sure thatcoerce_sequences
never errors-out or produces unexpected results, so we test the *&!@ out of it! Shout out to Hypothesis for giving me the power to test against everything withst.fromtype(type).flatmap(st.fromtype)
#ihavespenttoomuchtimewithhypothesis