You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Heya, cool library. I haven't given it a try yet personally, but I've been using typing.Annotated for runtime data validation in a similar way to what you've accomplished here. With your library it might look little something like this:
fromtyping_extensionsimportAnnotatedasAnfromenforceimportruntime_validation, InRange@runtime_validationdeftest_fn(a: An[int, InRange(0, 100)], b: An[float, InRange(0, 1)]): ...
test_fn(50, 0.5) # workstry:
test_fn(0.5, 0.5) # TypeError: Expected a to have type <class 'int'> but got <class 'float'>test_fn(b=2, a=50) # AssertionError: Argument b=2 not in range [0, 1)except: ...
Basically the idea is to enable most (if not all) input assertions to be provided by the type of the input rather than asserts in the function body. It's working well for me so far. I've put together a more complete example here: https://gist.github.com/CallumJHays/e4ad98925894a8e1cd7ef57e90fe2807
The text was updated successfully, but these errors were encountered:
CallumJHays
changed the title
Data validation as well as type-checking.
Data validation via typing.AnnotatedJan 2, 2021
Heya, cool library. I haven't given it a try yet personally, but I've been using
typing.Annotated
for runtime data validation in a similar way to what you've accomplished here. With your library it might look little something like this:Basically the idea is to enable most (if not all) input assertions to be provided by the type of the input rather than asserts in the function body. It's working well for me so far. I've put together a more complete example here: https://gist.github.com/CallumJHays/e4ad98925894a8e1cd7ef57e90fe2807
The text was updated successfully, but these errors were encountered: