Skip to content

validate_assignment does not work with root_validator #1971

@caldwellshane

Description

@caldwellshane

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

             pydantic version: 1.5.1
            pydantic compiled: True
                 install path: /Users/shane/repos/domain-model-specification/Python/.venv/lib/python3.7/site-packages/pydantic
               python version: 3.7.7 (default, Jul 18 2020, 22:28:30)  [Clang 11.0.3 (clang-1103.0.32.62)]
                     platform: Darwin-19.6.0-x86_64-i386-64bit
     optional deps. installed: ['typing-extensions']

Minimal Example

I'd like to enforce a condition on a whole model during arbitrary changes in its state:

from pydantic import BaseModel, root_validator

class Example(BaseModel):
    current_value: float
    max_value: float

    class Config:
        validate_assignment = True

    @root_validator
    def current_lessequal_max(cls, values):
        current_value = values.get("current_value")
        max_value = values.get("max_value")
        assert abs(current_value) <= max_value, f"current_value cannot be greater than max_value"
        return values

Based on my reading of the docs I'd expect this to fail validation if I set current_value above max_value, or set max_value below current_value, on an existing instance. Is that not the intended behavior?

Thanks for an excellent library!

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