[FIX] model validation for instances of BaseModel with from_attributes=True#6902
[FIX] model validation for instances of BaseModel with from_attributes=True#6902marielejeune wants to merge 1 commit intopydantic:mainfrom
from_attributes=True#6902Conversation
9801379 to
8cc05a9
Compare
|
It seems this is just meant to address the case where you do an invalid
As a result, I think this PR is not likely to be merged. |
|
@dmontagu even if I can hear the motivations for performance, I have to admit I find it rather strange that by default an explicit call to the validation method doesn't do anything, even if the model has been modified after it was created in the code. So once the object has been created, there's no way to validate it again unless you explicitly allow it via the model_config attribute... 🤔 When you develop a service and you get the response model instance from a call to an other method or ??? do you have to dig into the code to check that the model has been properly initialized nor have been modified after the creation in a way that it's no more valid since you can't expect that the the call to the validation method done by fastapi when the response is build will effectively validate again the model? |
|
FastAPI will never create an invalid model from data, which is what it is meant to handle. If you just don’t use |
@dmontagu Thank you for all your explanations. |
Change Summary
Validating a Pydantic model directly using the model object does not work as expected when object is an instance of BaseModel and
from_attributes = TrueRelated issue number
fix #6901
Checklist