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
The comparison of two objects of a pydantic model with a cached property shows unexpected behavior. The __eq__ operator does not explicitly evaluate cached properties. Therefore, the objects are compared based on previous evaluations. If a cached property was previously evaluated for exactly one of the compared objects, __eq__ results in False.
Expected behavior: If the cached properties have not been evaluated before a call to __eq__, they are evaluated during the comparison.
The issue is already resolved on main due to the changes of BaseModel.__eq__ (
Thanks for reporting this. @alexmojaki and @QuentinSoubeyranAqemia worked hard to fix this in #7825. This fix will be released with 2.6.0. If you need the fix now, you can pull from main :)
Initial Checks
Description
The comparison of two objects of a pydantic model with a cached property shows unexpected behavior. The
__eq__
operator does not explicitly evaluate cached properties. Therefore, the objects are compared based on previous evaluations. If a cached property was previously evaluated for exactly one of the compared objects,__eq__
results inFalse
.Expected behavior: If the cached properties have not been evaluated before a call to
__eq__
, they are evaluated during the comparison.The issue is already resolved on main due to the changes of
BaseModel.__eq__
(pydantic/pydantic/main.py
Line 863 in 420d8c2
Example Code
Python, Pydantic & OS Version
The text was updated successfully, but these errors were encountered: