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

Implement a new type of callback called validators, which get called before a value changes #42

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

astrofrog
Copy link
Member

This is an experimental pull request adding a new type of callback called 'validators'. The idea here is that one can do e.g.:

In [1]: from echo import HasCallbackProperties, CallbackProperty, ValidationException, SilentValidationException
   ...: 
   ...: class State(HasCallbackProperties):
   ...: 
   ...:     a = CallbackProperty()
   ...:     b = CallbackProperty()
   ...: 
   ...: state = State()
   ...: state.a = 1
   ...: state.b = 2.2
   ...: 

In [2]: def add_one_and_silent_ignore(new_value):
   ...:     if new_value == 'ignore':
   ...:         raise SilentValidationException()
   ...:     return new_value + 1
   ...: 
   ...: def preserve_type(old_value, new_value):
   ...:     if type(new_value) is not type(old_value):
   ...:         raise ValidationException('types should not change')
   ...: 
   ...: state.add_callback('a', add_one_and_silent_ignore, validator=True)
   ...: state.add_callback('b', preserve_type, validator=True, echo_old=True)
   ...: 
   ...: 

In [3]: state.a = 3

In [4]: state.a
Out[4]: 4

In [5]: state.a = 'ignore'

In [6]: state.a
Out[6]: 4

In [7]: state.b = 3.2

In [8]: state.b = 3
---------------------------------------------------------------------------
ValidationException                       Traceback (most recent call last)
...
ValidationException: types should not change

This demonstrates several features:

  • Checks can be carried out on the type and value of the new value (and the old one if echo_old is passed)
  • Validators can raise either full exceptions (ValidationException) or SilentValidationException, the latter of which means that the change will be silently abandonned
  • Validators can change the value to be set, either in-place or by returning a new object

Copy link

codecov bot commented Sep 16, 2024

Codecov Report

Attention: Patch coverage is 98.71795% with 1 line in your changes missing coverage. Please review.

Project coverage is 96.81%. Comparing base (d4d9d2f) to head (b0662e5).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
echo/core.py 96.15% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #42      +/-   ##
==========================================
+ Coverage   96.70%   96.81%   +0.11%     
==========================================
  Files          18       18              
  Lines        2274     2327      +53     
==========================================
+ Hits         2199     2253      +54     
+ Misses         75       74       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

… container, and add support for item validators
@astrofrog astrofrog marked this pull request as ready for review September 17, 2024 10:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant