Message381885
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. |
|
Date |
User |
Action |
Args |
2020-11-26 08:23:55 | xmorel | set | recipients:
+ xmorel |
2020-11-26 08:23:55 | xmorel | set | messageid: <1606379035.36.0.842330798084.issue42470@roundup.psfhosted.org> |
2020-11-26 08:23:55 | xmorel | link | issue42470 messages |
2020-11-26 08:23:55 | xmorel | create | |
|