[New-bugs-announce] [issue42470] DeprecationWarning triggers for sequences which happen to be sets as well
Xavier Morel
report at bugs.python.org
Thu Nov 26 03:23:55 EST 2020
New submission from Xavier Morel <xavier.morel at masklinn.net>:
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 <report at bugs.python.org>
<https://bugs.python.org/issue42470>
_______________________________________
More information about the New-bugs-announce
mailing list