8000 Setting `validate_assignment=False` after model initialization and invoking a setter does not disable assignment validation · Issue #11729 · pydantic/pydantic · GitHub
[go: up one dir, main page]

Skip to content
Setting validate_assignment=False after model initialization and invoking a setter does not disable assignment validation #11729
@Emrys-Merlin

Description

@Emrys-Merlin

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

I recently switched from pydantic version 2.10.6 to 2.11.3. I have a pydantic model with validate_assignment=True by default. For testing, I temporarily disable this assignment validation to modify a model instance. After the version bump, the code throws a validation error. I have attached a minimal example below. The code works as expected on v2.10.6, but fails with a validation error on v2.11.0, v2.11.1, and v2.11.3.

I suspect that the issue is related to #10868, because it only occurs if a setter is invoked before the assignment validation is disabled. Translated to the example script below, if you remove the lines

    # Need to invoke a setter (I suspect to trigger the memoization)
    example.list1 = [1, 2, 3]

no validation error is thrown.

Please, let me know if the explanation is not clear enough or I can support in any other way.
Thanks a lot for your help and the great tool! :)

Example Code

# /// script
# requires-python = ">=3.10"
# dependencies = [
#     "pydantic==2.11.3",
# ]
# ///


import pydantic
from pydantic import BaseModel, ConfigDict, model_validator


class Example(BaseModel):
    list1: list[int]
    list2: list[str]

    model_config = ConfigDict(
        validate_assignment=True,
    )

    @model_validator(mode="after")
    def check_list_lengths(self):
        if len(self.list1) != len(self.list2):
            raise ValueError("list1 and list2 must have the same length")
        return self


if __name__ == "__main__":
    print(f"Pydantic version: {pydantic.__version__}")

    example = Example(list1=[1, 2, 3], list2=["a", "b", "c"])
    print(example)

    # Need to invoke a setter (I suspect to trigger the memoization)
    example.list1 = [1, 2, 3]
    print(example)

    example.model_config["validate_assignment"] = False
    example.list1 = [1, 2]
    example.list2 = ["a", "b"]
    example.model_config["validate_assignment"] = True
    print(example)

Python, Pydantic & OS Version

Pydantic version: 2.11.3
             pydantic version: 2.11.3
        pydantic-core version: 2.33.1
          pydantic-core build: profile=release pgo=false
                 install path: /home/tim/.cache/uv/archive-v0/zd6MbOILWOYXhBpQGNkHW/lib/python3.12/site-packages/pydantic
               python version: 3.12.3 (main, Feb  4 2025, 14:48:35) [GCC 13.3.0]
                     platform: Linux-6.8.0-56-generic-x86_64-with-glibc2.39
             related packages: typing_extensions-4.13.1
                       commit: unknown
< 4C29 /div>

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug V2Bug related to Pydantic V2pendingIs unconfirmed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0