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

Validator ignoring 'always=True' on optional fields on inheritance #1155

Closed
gaja-dev opened this issue Jan 8, 2020 · 2 comments
Closed

Validator ignoring 'always=True' on optional fields on inheritance #1155

gaja-dev opened this issue Jan 8, 2020 · 2 comments
Labels
bug V1 Bug related to Pydantic V1.X help wanted Pull Request welcome

Comments

@gaja-dev
Copy link

gaja-dev commented Jan 8, 2020

Possible Bug

Validator seems to ignore the 'always' parameter on inheritance since pydantic >= 1.2

I had a running code that uses inheritance on the models with an enforced field that is used to decide if other optional fields will be checked, or not. This worked well until I updated to pydantic 1.3. Further testing shows that the code fails since pydantic 1.2. So there seem to be some changes from 1.1 to 1.2 that leads to this issue.

Unfortunately I could not find a previous issue, or documentation hint that explains this deviating behavior to me.

If I missed something in the doc, please give me a hint and excuse my issue. Otherwise, I would be thankful if someone could fix this, or explain how the intended behavior can be achieved on pydantic >=1.2

Please find an example code and corresponding output below.

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

pydantic version: 1.3
pydantic compiled: False
install path: [...]\python_pydantic-ni4Wh3vY\Lib\site-packages\pydantic
python version: 3.6.8 (tags/v3.6.8:3c6b436a57, Dec 24 2018, 00:16:47) [MSC v.1916 64 bit (AMD64)]
platform: Windows-10-10.0.18362-SP0
optional deps. installed: []

Here is an example code to reproduce this issue and below that, the corresponding output on pydantic 1.1 and >=1.2:

import typing

import pydantic

class A(pydantic.BaseModel):

    must_validate: bool

    class Config:
        extra = pydantic.Extra.ignore


class B(pydantic.BaseModel):

    value_a: typing.Optional[str] = None
    value_b: typing.Optional[str] = None

    class Config:
        extra = pydantic.Extra.ignore

class C(B, A):

    @pydantic.validator("value_a", "value_b", always=True)
    def validate_if_enforced(cls, value, values):
        if "must_validate" in values and values["must_validate"]:
            if not value:
                raise ValueError("must be set if 'must_validate' is True")
        return value
    
    class Config:
        extra = pydantic.Extra.forbid


if __name__ == "__main__":
    success_data = {
        "must_validate": True,
        "value_a": "foo",
        "value_b": "bar",
    }

    fail_data = {
        "must_validate": True,
        "value_a": "foo",
    }

    try:
        success = C.parse_obj(success_data)
        print(success.dict())

        fail = C.parse_obj(fail_data)
        print(fail.dict())
    except pydantic.ValidationError as exc:
        print(exc)
...

Output on pydantic 1.1 (This is the intended behavior!)

{'must_validate': True, 'value_a': 'foo', 'value_b': 'bar'}
1 validation error for C
value_b
  must be set if 'must_validate' is True (type=value_error)

Output on pydantic >=1.2

{'must_validate': True, 'value_a': 'foo', 'value_b': 'bar'}
{'must_validate': True, 'value_a': 'foo', 'value_b': None}
@gaja-dev gaja-dev added the bug V1 Bug related to Pydantic V1.X label Jan 8, 2020
@samuelcolvin
Copy link
Member

Thanks for reporting, I get the same.

The intermediate model B is not required to produce the error.

The problem is b521f6b and almost certainly the change main.py:196.

It might be as simple as reversing that change, but I made it for a reason (that currently escapes me). I'll look at it next week or when I get a chance, if it's urgent feel free to investigate and submit a PR if you can find a fix.

@samuelcolvin samuelcolvin added the help wanted Pull Request welcome label Jan 8, 2020
dcHHH pushed a commit to dcHHH/pydantic that referenced this issue May 21, 2020
samuelcolvin added a commit that referenced this issue Jun 29, 2020
* fix issure #1155

* add changes.1545-dcHHH.md

* improve change description

Co-authored-by: dchhh <hudacong@geetest.com>
Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
sthagen pushed a commit to sthagen/pydantic-pydantic that referenced this issue Jun 29, 2020
fix pydantic#1155

* fix issure pydantic#1155

* add changes.1545-dcHHH.md

* improve change description

Co-authored-by: dchhh <hudacong@geetest.com>
Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
sthagen pushed a commit to sthagen/pydantic-pydantic that referenced this issue Jun 29, 2020
* fix issure pydantic#1155

* add changes.1545-dcHHH.md

* improve change description

Co-authored-by: dchhh <hudacong@geetest.com>
Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
@PrettyWood
Copy link
Member

Fixed by #1545

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V1 Bug related to Pydantic V1.X help wanted Pull Request welcome
Projects
None yet
Development

No branches or pull requests

3 participants