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
Getting more validation errors then I would expect. #8014
Comments
I don't see a specific bug being reported here. Please use discussions to ask questions, a shorter demonstration of your question will increase the chances of an answer. If you really think there's a bug, please provide a clear description of the bug together with a MRE. |
hm, I mean, I just started with pydantic, so I am not sure if it is a bug or I am doing something wrong. What it seems to me that when a union is given, pydantic is able to pick the right type, but when that type fails for other reasons like I raised an error for various other reasons, pydantic just jumps to try to validate all other types in the union which then naturally fail as a consequence. |
Ah, now I understand your question. Yes it does, that's by design. If you want to avoid that, you should use tagged unions - https://docs.pydantic.dev/latest/api/standard_library_types/#discriminated-unions-aka-tagged-unions |
Thank you so much for looking into it. I greatly appreciate it. I have looked the animal examples under tag union before. However it seems to me, that tag selection is based on very specifically given values in the incoming data. So in the example, 'pet_type': 'cat' is in the data to be parsed. However in my example, the data I parse (parmdictdata in the code below) does not include a specific key to be used as a discriminator. So I am not sure if there is any other workaround not to try validating all other types in the union? I have simplified my demonstration code, so maybe this helps better for testing it out. Thanks so much for the help again!
|
#7983 is what you need, will be released next week. |
Thanks so much for that! |
Initial Checks
Description
Hi! I just started to use pydantic, and I really love it.
In my following test, I have purposely provided a data so that the (validate_lock()) model validator would to raise a PydanticCustomError() inside the ParmData class. This error is indeed reported as expected.
However my issue is that I am also getting various other error raised, that I would not expect, and I so far could not figure out why. Such as this for example:
p5.ParmValueData.float
Input should be a valid number [type=float_type, input_value={'value': [1, {'expressio...'locked': [True, False]}, input_type=dict]
For further information visit https://errors.pydantic.dev/2.4/v/float_type
I simplified the example as much as I could, and here is a small explanation:
The data I am trying the validate is a dictionary (ParmDictData) of so called parameters (ParmData). Each parameter, could contain it's value (as ParmValueData), as well as it's locked state or whenever it is hidden or not.
The data for a dictionary of parameters could look something like this:
{
'p1' : { 'value': 5, 'hidden: True, 'locked': True},
'p2': { 'value': 'foo' },
}
However when the parameter data ONLY contains 'value' key, for ease of writing/editing the data, I could throw away 'value' key, and simply write the value of the parameter in the dictionary next to it's name:
{
'p1' : { 'value': 5, 'hidden: True},
'p2': 'foo',
}
Hence, I thought the best way to validate ParmDictData would be to declare the root type with a union such as this : dict[str, ParmData | ParmValueData] = None
But it seems to me, as soon as validate_lock() raises an error for a ParmData instance, pydantic then takes that data and tries to force validate it as ParmValueData as well. And as ParmValueData is also made of union type, (and those fail) it raises an error for each union type.
I have tried to set 'left_to_right' validation order but that did not help, and I am not sure why is this happening. If you uncomment validate_lock() then the data validates just fine otherwise.
Please let me know if I am doing anything wrong, or there is a better way to do this.
Attila
Example Code
Python, Pydantic & OS Version
The text was updated successfully, but these errors were encountered: