From c77f7fc5cdd12df85a574f8ec8c0385f018da9f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20V=C3=A1zquez=20Acosta?= Date: Mon, 9 Oct 2017 13:58:32 +0000 Subject: [PATCH] [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 --- odoo/models.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/odoo/models.py b/odoo/models.py index 483c79521fe..fe3a9dd4781 100644 --- a/odoo/models.py +++ b/odoo/models.py @@ -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)