8000 "values" parameter in validator receives ambiguous types · Issue #1999 · pydantic/pydantic · GitHub
[go: up one dir, main page]

Skip to content

"values" parameter in validator receives ambiguous types #1999

@ghost

Description

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug V1Bug related to Pydantic V1.X

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0