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

"values" parameter in validator receives ambiguous types #1999

Closed
3 tasks done
ghost opened this issue Oct 12, 2020 · 0 comments · Fixed by #2000
Closed
3 tasks done

"values" parameter in validator receives ambiguous types #1999

ghost opened this issue Oct 12, 2020 · 0 comments · Fixed by #2000
Labels
bug V1 Bug related to Pydantic V1.X

Comments

@ghost
Copy link

ghost commented Oct 12, 2020

Checks

  • I added a descriptive title to this issue
  • I have searched (google, github) for similar issues and couldn't find anything
  • I have read and followed the docs and still think this is a bug

Bug

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

python -c "import pydantic.utils; print(pydantic.utils.version_info())"                                          
             pydantic version: 1.6.1
            pydantic compiled: True
                 install path: /homes/<redacted>/.conda/envs/<redacted>/lib/python3.7/site-packages/pydantic
               python version: 3.7.8 | packaged by conda-forge | (default, Jul 31 2020, 02:25:08)  [GCC 7.5.0]
                     platform: Linux-4.9.0-0.bpo.6-amd64-x86_64-with-debian-8.11
     optional deps. installed: ['typing-extensions']

Hi,
In a validator method, when adding the values parameter, I expect it to be a map from field names to validated types.
When specifying validate_assignment = True in model config, values gets a different type (just on assignment).
This is illustrated by the example below:

import pydantic

class ModelOne(pydantic.BaseModel):
    a: int

class ModelTwo(pydantic.BaseModel):
    m: ModelOne
    b: int

    @pydantic.validator('b')
    def validate_b(cls, b, values):
        print(values)
        if 'm' in values:
            return b + values['m'].a  # this fails with AttributeError if values['m'] is a dict
        else:
            return b

    class Config:
        validate_assignment = True

model = ModelTwo(m=ModelOne(a=1), b=2)
#> {'m': ModelOne(a=1)}
model.b = 3
#> {'m': {'a': 1}}

As far as I can tell, this behavior is not documented, and I'm pretty sure it's not intended.

edit: created a PR

@ghost ghost added the bug V1 Bug related to Pydantic V1.X label Oct 12, 2020
samuelcolvin pushed a commit that referenced this issue Oct 25, 2020
)

* call validator with correct "values" type in BaseModel.__setattr__

* Fix code review comments
- use `self.__dict__` as the values parameter
- refine changed markdown

Co-authored-by: Ran Shaham <[email protected]>
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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

0 participants