Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data validation via typing.Annotated #79

Open
CallumJHays opened this issue Jan 2, 2021 · 0 comments
Open

Data validation via typing.Annotated #79

CallumJHays opened this issue Jan 2, 2021 · 0 comments

Comments

@CallumJHays
Copy link

CallumJHays commented Jan 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:

from typing_extensions import Annotated as An
from enforce import runtime_validation, InRange

@runtime_validation
def test_fn(a: An[int, InRange(0, 100)], b: An[float, InRange(0, 1)]): ...

test_fn(50, 0.5) # works
try:
    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

@CallumJHays CallumJHays changed the title Data validation as well as type-checking. Data validation via typing.Annotated Jan 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant