[FIX] models: Allow rich comparison with third party objects.

Instead of returning False directly when comparing a non-model, return
NotImplemented that will trigger the __eq__ test from the other side.

This allows third party objects (for instance a serialization/deserialization
scheme) that cannot be a Model to be considered equal is some scenarios.

closes odoo/odoo#19977
This commit is contained in:
Manuel Vázquez Acosta
2018-10-11 16:59:49 +00:00
committed by Xavier Morel
parent a5b3246dcd
commit c77f7fc5cd
+5 -8
View File
@@ -4978,20 +4978,17 @@ class BaseModel(MetaModel('DummyModel', (object,), {'_register': False})):
filename, lineno = frame_codeinfo(currentframe(), 1)
_logger.warning("Comparing apples and oranges: %r == %r (%s:%s)",
self, other, filename, lineno)
return False
return NotImplemented
return self._name == other._name and set(self._ids) == set(other._ids)
def __ne__(self, other):
return not self == other
def __lt__(self, other):
if not isinstance(other, BaseModel) or self._name != other._name:
raise TypeError("Mixing apples and oranges: %s < %s" % (self, other))
return NotImplemented
return set(self._ids) < set(other._ids)
def __le__(self, other):
if not isinstance(other, BaseModel) or self._name != other._name:
raise TypeError("Mixing apples and oranges: %s <= %s" % (self, other))
return NotImplemented
# these are much cheaper checks than a proper subset check, so
# optimise for checking if a null or singleton are subsets of a
# recordset
@@ -5001,12 +4998,12 @@ class BaseModel(MetaModel('DummyModel', (object,), {'_register': False})):
def __gt__(self, other):
if not isinstance(other, BaseModel) or self._name != other._name:
raise TypeError("Mixing apples and oranges: %s > %s" % (self, other))
return NotImplemented
return set(self._ids) > set(other._ids)
def __ge__(self, other):
if not isinstance(other, BaseModel) or self._name != other._name:
raise TypeError("Mixing apples and oranges: %s >= %s" % (self, other))
return NotImplemented
if not other or other in self:
return True
return set(self._ids) >= set(other._ids)