New submission from Xavier Morel <[email protected]>:
In 3.9, using `random.sample` on sets triggers
DeprecationWarning: Sampling from a set deprecated
since Python 3.9 and will be removed in a subsequent version.
*However* it also triggers on types which implement *both* Sequence and Set,
despite Sequence on its own being fine.
The issue is that it first checks for Set and triggers a warning, and only then
checks that the input is a sequence:
if isinstance(population, _Set):
_warn('Sampling from a set deprecated\n'
'since Python 3.9 and will be removed in a subsequent
version.',
DeprecationWarning, 2)
population = tuple(population)
if not isinstance(population, _Sequence):
raise TypeError("Population must be a sequence. For dicts or sets,
use sorted(d).")
the check should rather be:
if not isinstance(population, _Sequence):
if isinstance(population, _Set):
_warn('Sampling from a set deprecated\n'
'since Python 3.9 and will be removed in a subsequent
version.',
DeprecationWarning, 2)
population = tuple(population)
else:
raise TypeError("Population must be a sequence. For dicts or
sets, use sorted(d).")
this also only incurs a single instance check for `_Sequence` types instead of
two.
----------
messages: 381885
nosy: xmorel
priority: normal
severity: normal
status: open
title: DeprecationWarning triggers for sequences which happen to be sets as well
versions: Python 3.9
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue42470>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com