You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Not sure if this is a bug or a feature.
So far (pydantic v2.5) , the model_computed_fields (a property) only work on model instances and not from model class.
I do not see obvious reason for that. See code below.
A solution would be to not use builtin python @property but a custom property with a re-writen __get__ method
Example Code
frompydanticimportBaseModel, computed_fieldclassM(BaseModel):
x: float=1.0@computed_field@propertydeftenx(self)->float:
returnself.x*10>>>M.model_computed_fields<propertyat0x7fc4cbcf2860>>>>M.model_computed_fields.__get__(None, M) # actually same as above <propertyat0x7fc4cbcf2860>>>>M.model_computed_fields.__get__(M, None)
{'tenx': ComputedFieldInfo(wrapped_property=<propertyobjectat0x7fc4cbc84360>, return_type=<class'float'>, alias=None, alias_priority=None, title=None, description=None, examples=None, json_schema_extra=None, repr=True)}
Initial Checks
Description
Not sure if this is a bug or a feature.
So far (pydantic v2.5) , the model_computed_fields (a property) only work on model instances and not from model class.
I do not see obvious reason for that. See code below.
A solution would be to not use builtin python
@property
but a custom property with a re-writen__get__
methodExample Code
Python, Pydantic & OS Version
The text was updated successfully, but these errors were encountered: