From c2ab0a5c5480fab059768d1698ac82ca720f50d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Voet=20=28ryv=29?= Date: Thu, 10 Feb 2022 13:29:11 +0000 Subject: [PATCH] [REF] core: simplify some base methods of BaseModel Simplify __bool__(), __repr__(), __hash__() by removing calls to getattr/hasattr. We can expect a performance improvement of these highly used method (for a single record): - __bool__(): 145 -> 108 ns - __hash__(): 330 -> 289 ns Discard support for user id in sudo(), the method now only accepts a bool. Part-of: odoo/odoo#83687 --- odoo/models.py | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/odoo/models.py b/odoo/models.py index a98a6511ef3..ae408f7a5f2 100644 --- a/odoo/models.py +++ b/odoo/models.py @@ -5287,9 +5287,7 @@ Fields: The returned recordset has the same prefetch object as ``self``. """ - if not isinstance(flag, bool): - _logger.warning("deprecated use of sudo(user), use with_user(user) instead", stack_info=True) - return self.with_user(flag) + assert isinstance(flag, bool) return self.with_env(self.env(su=flag)) def with_user(self, user): @@ -5769,7 +5767,8 @@ Fields: def __bool__(self): """ Test whether ``self`` is nonempty. """ - return bool(getattr(self, '_ids', True)) + return True if self._ids else False # fast version of bool(self._ids) + __nonzero__ = __bool__ def __len__(self): @@ -5915,13 +5914,10 @@ Fields: return self.id or 0 def __repr__(self): - return "%s%s" % (self._name, getattr(self, '_ids', "")) + return f"{self._name}{self._ids}" def __hash__(self): - if hasattr(self, '_ids'): - return hash((self._name, frozenset(self._ids))) - else: - return hash(self._name) + return hash((self._name, frozenset(self._ids))) def __getitem__(self, key): """ If ``key`` is an integer or a slice, return the corresponding record