-
Notifications
You must be signed in to change notification settings - Fork 8
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
pydantic handling multiple errors #301
Comments
Hi @dayantur, I've done some quick research on different validation approaches in Pydantic. Please see below:
Please let me know your thoughts. |
Hi @sunt05, I will have a look at this and let you know. I've converted some Field ranges into model_validator (as comments) today, and committed in my branch dayantur/adding_pydantic_rules. But before merging these commits to sunt05/issue299 (or continuing with the conversion), I think it might be probably better for me to read first this documentation and have a clearer idea of these validation approaches |
Hi @sunt05 -- as discussed, I found a way to use Field(), @model_validator, and still get all the errors from the program (from both Field() and @model_validator). This is a test_script.py that mimic the structure we have now in def_config_suews.py, and has one validation made in a Field(), and one validation made in a @model_validator (n.b.: the rules here are nonsense and made up just for testing):
If fed with this config_test.yml:
It correctly runs until the end AND prints together errors from Field() and @model_validator:
If we agree that this might be useful/a good strategy to use, I can edit the code accordingly in the next few days. |
Looks brilliant - go ahead and get these in. Thanks, @dayantur! |
Bad news - I've tried to convert the def_config_suews.py today to handle the errors as above and failed.
If we set faibldg: 4 and snowalb: -0.5 in the test.yml:
The program halts before validate_second AND validate_third, despite faibldg being <=5 and <=6, which errors are not printed.
We get error from validate_second, but not from validate_third.
|
Just for completeness, this approach:
with
Can collect all the errors without halting:
|
In def_config_suews.py in branch dayantur/adding_pydantic_rules (from sunt05/issue29), I've implemented a way to see all the exceptions together instead of having the script stopping after raising the first ValueError. This might be useful for the user, so they can see all the consistency errors together/save time in understanding the problem they are having with the configuration of the .yml file (see screenshot below).
But this might not be enough. For how it is now structured the code, we also have cases like:
If we change the alb_min in the config-suews.yml to be alb_min < 0 AND we also change pormin_dec and pormax_dec to be inconsistent:
we only have printed:
so we are not actually able to see that there is also another problem in another set of parameters.
So, my suggestion is -if we agree that it might be actually useful for the user to see all the exceptions and not only the first one - to avoid using ge and le in Field() and convert them into @model_validator cases (leaving in Field() only the description of the variable).
Let me know what you think!
The text was updated successfully, but these errors were encountered: