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

each_item causes validator to not run when parent class has attribute #1566

Open
samueldeklund opened this issue May 27, 2020 · 5 comments
Open

Comments

@samueldeklund
Copy link
Contributor

@samueldeklund samueldeklund commented May 27, 2020

Bug

Output of python -c "import pydantic.utils; print(pydantic.utils.version_info())":

             pydantic version: 1.5.1
            pydantic compiled: True
                 install path: /Users/samuel/Desktop/venv/lib/python3.8/site-packages/pydantic
               python version: 3.8.3 (default, May 16 2020, 22:17:30)  [Clang 11.0.3 (clang-1103.0.32.59)]
                     platform: macOS-10.15.4-x86_64-i386-64bit
     optional deps. installed: []

I use a validator on a subclass and reference a List attribute on the parent class. However, the validator does not get called.

from typing import List
from pydantic import BaseModel, validator

class Thing1(BaseModel):
    item: str
    item2: List[str]

class Thing2(Thing1):
    @validator("item2", for_each=True)
    def check_item(cls, v, values):
        assert v == values.get("item")

# This will NOT raise a ValidationError like expected; it won't even call the validator, so this is a silent error
hat = Thing2(item="hi", item2=["hi", "h"])

A workaround is to just treat v as a list (leave each_item=False):

from typing import List
from pydantic import BaseModel, validator

class Thing1(BaseModel):
    item: str
    item2: List[str]

class Thing2(Thing1):
    @validator("item2")
    def check_item(cls, v, values):
        for string in v:
            assert v == values.get("item")
        return v

# This will not raise a ValidationError as expected
cat = Thing2(item="hi", item2["hi", "hi"])

# This will raise a ValidationError as expected
hat = Thing2(item="hi", item2=["hi", "h"])
@PrettyWood
Copy link
Collaborator

@PrettyWood PrettyWood commented May 27, 2020

Hello @samueldeklund
Yes it's a limitation of the present implementation of the validators with each_item.

@samueldeklund
Copy link
Contributor Author

@samueldeklund samueldeklund commented May 27, 2020

Hmm, is this something that can be fixed or perhaps just this special case added to documentation somewhere?

@samuelcolvin
Copy link
Owner

@samuelcolvin samuelcolvin commented May 31, 2020

validation might change significantly in v2 and make this possible.

In the meantime, I'd accept a PR to update the docs.

@samueldeklund
Copy link
Contributor Author

@samueldeklund samueldeklund commented Jun 5, 2020

Submitted a PR.

samuelcolvin pushed a commit that referenced this issue Jun 27, 2020
* add description of subclass each_item scenario

* add example of subclass validator with each_item

* each_item causes validator to not run when parent class has List field #1566
@samuelcolvin samuelcolvin added this to the Version 2 milestone Jun 27, 2020
@samuelcolvin
Copy link
Owner

@samuelcolvin samuelcolvin commented Jun 27, 2020

hopefully this can be fixed properly in v2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants