Skip to content

Commit

Permalink
[FIX] mass_mailing: use a sequence for random sampling
Browse files Browse the repository at this point in the history
Since Python 3.11, sampling from a set deprecated, the population must
be a sequence.

This commit applies the suggested fix.

Also, the filtering of the deprecation warning about sampling from set
can be disabled when the python version is not 3.9. This warning was
wrongly triggered since 3.9 because recordsets are bot a sequence and a
set.
This was fixed in python 3.10, see https://bugs.python.org/issue42470

closes odoo#112317

Signed-off-by: Xavier Morel (xmo) <xmo@odoo.com>
  • Loading branch information
d-fence committed Feb 10, 2023
1 parent 482cb08 commit 67c7cea
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
2 changes: 1 addition & 1 deletion addons/mass_mailing/models/mailing.py
Original file line number Diff line number Diff line change
Expand Up @@ -990,7 +990,7 @@ def _get_recipients(self):
remaining = set(res_ids).difference(already_mailed)
if topick > len(remaining) or (len(remaining) > 0 and topick == 0):
topick = len(remaining)
res_ids = random.sample(remaining, topick)
res_ids = random.sample(sorted(remaining), topick)
return res_ids

def _get_remaining_recipients(self):
Expand Down
6 changes: 4 additions & 2 deletions odoo/netsvc.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,10 @@ def record_factory(*args, **kwargs):
# ignore deprecation warnings from invalid escape (there's a ton and it's
# pretty likely a super low-value signal)
warnings.filterwarnings('ignore', r'^invalid escape sequence \'?\\.', category=DeprecationWarning)
# recordsets are both sequence and set so trigger warning despite no issue
warnings.filterwarnings('ignore', r'^Sampling from a set', category=DeprecationWarning, module='odoo')
if sys.version_info[:2] == (3, 9):
# recordsets are both sequence and set so trigger warning despite no issue
# Only applies to 3.9 as it was fixed in 3.10 see https://bugs.python.org/issue42470
warnings.filterwarnings('ignore', r'^Sampling from a set', category=DeprecationWarning, module='odoo')
# ignore a bunch of warnings we can't really fix ourselves
for module in [
'babel.util', # deprecated parser module, no release yet
Expand Down

0 comments on commit 67c7cea

Please sign in to comment.