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
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=1validatex>0, "x should be positive"df= ... # a pandas dataframevalidatelen(df) >0andassert_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
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,assert_is_uniformly_sampled
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 examplevalidate <expression> <exception, exception_type or exception_message>
:assert
(when python is run in optimized mode)ValidationError
, even when a custom error subtype is provided (see previous bullet)<expression>
, hence allowing for clear debuggingExample use cases:
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
The text was updated successfully, but these errors were encountered: