I'm working on Pydantic now and trying to solve an issue
# pydantic/issues/11823
I have to read and trace most Pydantic code
You can see in the issue's model, validate_assignment=True exists, and because of that, our error occurred, so I have to find out where this _model_wrap_validator is running. I didn't know anything about the basemodel metaclass, so I have to read the first model_construction.ModelMetaclass and i found something filled there named pydantic_validator because I've seen it in basemodel init as validated_self = self.pydantic_validator.validate_python(data, self_instance=self) in the metaclass this variable filled with a function named create_schema_validator as i dig in the project i find out it return SchemaValidator(schema, config) from pydantic_core that write with rust lang i don't wanna deep more but i have to say SchemaValidator create before my class running over pydantic_core i find_out the problem occured when pydantic_core generate as pythonI'm currently working on Pydantic and trying to resolve an issue. I need to read through and trace most of the Pydantic code. In the model for the issue, validate_assignment=True is present, which is causing the error. Therefore, I need to find out where the _model_wrap_validator is being executed.
I was initially unfamiliar with the BaseModel metaclass, so I looked into the model_construction.ModelMetaclass. I discovered a variable named __pydantic_validator__, which I noticed being used in the __init__ method of BaseModel as validated_self = self.__pydantic_validator__.validate_python(data, self_instance=self).
In the metaclass, a variable is initialized with a function called create_schema_validator. As I investigated the project further, I discovered that this function returns SchemaValidator(schema, config) from pydantic_core, which is implemented in Rust. I don’t want to delve too deeply into that at the moment, but I found that the SchemaValidator is created before my class is processed by pydantic_core. I realized that the issue arises when pydantic_core is generated as Python.
Before I dive into pydantic_core, I want to learn more about model_validator. Through my research, I found it in GenerateSchema. As you know, this is where the schema is created from pydantic_core before everything else. I discovered that it has a method called _model_schema, which is only called when our object has a BaseModel type.
Upon further investigation, I came across a method called apply_model_validators, which sets the order of our overridden model validators, but it only creates the schema without executing any validation.
For the model, everything works fine when I create an instance, like this: m = M(x="foo"). The model's validation shows: {'x': 'foo'}
Therefore, I examined the `setattr` method in BaseModel. After some digging, I found a lambda function called validate_assignment, which looks like this: lambda model, name, val: model.__pydantic_validator__.validate_assignment(model, name, val). This has led me to believe that I need to check validate_assignment either in pydantic_core or in the GenerateSchema module in Python.