A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/pydantic/pydantic/issues/635 below:

The validator of the type hints classes are evaluated with apparently weird input · Issue #635 · pydantic/pydantic · GitHub

Bug or Question

Hi,

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 dicts 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:


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