Bug or Question
import sys; print(sys.version)
: 3.7.2import pydantic; print(pydantic.VERSION)
: 0.29Hi,
I work with some models and below I tried to simplify the idea:
class Parent(BaseModel): siblings: List[Parent] @validator('siblings', whole=True, pre=True) def validate_siblings(cls, siblings): ... class Child1(Parent): siblings: List[Union[Child2, Child3]] class Child2(Parent): siblings: List[Union[Child3, Child5, Child6]] .....
Note that the parent validator may be overwritten or not, it doesn't matter in this situation.
When a Child2
is instantiated (with siblings=[child6_obj]
for example) --> the validator is called for the Child2
class. The problem is that all the validators for the possible Child2.siblings
types are also evaluated (which are Child3, Child5, Child6
). And from my debugging, they are called with child_obj.dict()
, so validate_siblings
validator receives a list of dict
s as param even if the Child2
object is instantiated with a list of objects (siblings=[child6_obj]
).
In my case, the validator of Child3
will raise an error.
First of all, Is this the correct behavior?
Secondly, do you spot any bad practice/idea in my snippet?
--- EDIT
Some more details about def validate_siblings(cls, siblings)
implementation:
siblings
param (which is a list)Child1
is instantiated (c = Child1(siblings=[obj1, obj2])
), I expect the validator is called with [obj1, obj2]
, and its logic is to check if both objects in the list are instances of Child2
or Child3
. I achieve this by looking in Child1.__annotations__
.dict
in validate_siblings
validator, but only the objects I use in my code. Maybe I didn't understand how validators work and what are the situations in which they are called/evaluated.RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4