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

[Rejected] consistent validation api - validate statement #12

Open
smarie opened this issue Sep 7, 2020 · 0 comments
Open

[Rejected] consistent validation api - validate statement #12

smarie opened this issue Sep 7, 2020 · 0 comments

Comments

@smarie
Copy link
Owner

smarie commented Sep 7, 2020

In value validation scenarii, a common problem is to be able to raise a consistent error type (e.g. ValidationError or custom subtype) and error message (e.g. "invalid input dataframe" + precise details about the name or formula that failed) while relying on the composition a broad variety of elementary callables to perform the validation,

  • some raising errors when validation fails and returning nothing (silent) when validation succeeds, e.g. assert_is_uniformly_sampled
  • some returning True/False to indicate the validation outcome, e.g. is_valid_timedelta

and of course, all of them raising error sometimes, in some edge conditions - typically when executed on some input whose type was not in the mind of the developer when he wrote the validation code :)

One way to solve this problem could be to come up with a new statement in the language, similar to assert but with major differences. For example validate <expression> <exception, exception_type or exception_message> :

  • first users would not be able to deactivate it as it is the case for assert (when python is run in optimized mode)
  • users would be able to not only specify an error message as in assert, but possibly an error subtype
  • the exception raised would always be an instance of a new type ValidationError, even when a custom error subtype is provided (see previous bullet)
  • the exception raised would clearly show the failing <expression>, hence allowing for clear debugging

Example use cases:

x = 1
validate x > 0, "x should be positive"

df = ... # a pandas dataframe
validate len(df) > 0 and assert_sampling_uniform(df), "df should be non-empty and uniformly sampled", InvalidDataframeError

Since it was obviously not possible to change the language, I tried various ways to implement this in the valid8 project: as a function, a context manager, a function decorator, and a class decorator.

I still think that having an elegant solution directly baked in in the language could sometimes simplify many API consistency anduser-friendlyness efforts.

Note that this is also related to the ability to create lambda expressions simpler and with string representation, see #4

Original suggestion and discussion: python-ideas

@smarie smarie changed the title [Rejected] consistent validation api [Rejected] consistent validation api - validate statement Sep 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant