Thanks to visit codestin.com
Credit goes to github.com

Skip to content

bpo-40325: Deprecate set object support in random.sample() #19591

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Doc/library/random.rst
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,13 @@ Functions for sequences
If the sample size is larger than the population size, a :exc:`ValueError`
is raised.

.. deprecated:: 3.9
In the future, the *population* must be a sequence. Instances of
:class:`set` are no longer supported. The set must first be converted
to a :class:`list` or :class:`tuple`, preferably in a deterministic
order so that the sample is reproducible.


Real-valued distributions
-------------------------

Expand Down
5 changes: 4 additions & 1 deletion Lib/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,12 @@ def sample(self, population, k):
# causing them to eat more entropy than necessary.

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 or set. For dicts, use list(d).")
raise TypeError("Population must be a sequence. For dicts or sets, use sorted(d).")
randbelow = self._randbelow
n = len(population)
if not 0 <= k <= n:
Expand Down
6 changes: 5 additions & 1 deletion Lib/test/test_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ def test_sample_distribution(self):

def test_sample_inputs(self):
# SF bug #801342 -- population can be any iterable defining __len__()
self.gen.sample(set(range(20)), 2)
self.gen.sample(range(20), 2)
self.gen.sample(range(20), 2)
self.gen.sample(str('abcdefghijklmnopqrst'), 2)
Expand All @@ -156,6 +155,11 @@ def test_sample_inputs(self):
def test_sample_on_dicts(self):
self.assertRaises(TypeError, self.gen.sample, dict.fromkeys('abcdef'), 2)

def test_sample_on_sets(self):
with self.assertWarns(DeprecationWarning):
population = {10, 20, 30, 40, 50, 60, 70}
self.gen.sample(population, k=5)

def test_choices(self):
choices = self.gen.choices
data = ['red', 'green', 'blue', 'yellow']
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Deprecated support for set objects in random.sample().