summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRaymond Hettinger <rhettinger@users.noreply.github.com>2020-04-19 00:36:42 -0700
committerGitHub <noreply@github.com>2020-04-19 00:36:42 -0700
commit4fe002045fcf40823154b709fef0948b2bc5e934 (patch)
treeea9d2c899b682f91fd21f50c13bfae8903f58f11
parent482259d0dcf27714a84cf56b93977320bea7e093 (diff)
downloadcpython-git-4fe002045fcf40823154b709fef0948b2bc5e934.tar.gz
bpo-40325: Deprecate set object support in random.sample() (GH-19591)
-rw-r--r--Doc/library/random.rst7
-rw-r--r--Lib/random.py5
-rw-r--r--Lib/test/test_random.py6
-rw-r--r--Misc/NEWS.d/next/Library/2020-04-18-19-40-00.bpo-40325.KWSvix.rst1
4 files changed, 17 insertions, 2 deletions
diff --git a/Doc/library/random.rst b/Doc/library/random.rst
index 9964af46b2..82e900d3a2 100644
--- a/Doc/library/random.rst
+++ b/Doc/library/random.rst
@@ -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
-------------------------
diff --git a/Lib/random.py b/Lib/random.py
index 3243938282..f1df18d5c1 100644
--- a/Lib/random.py
+++ b/Lib/random.py
@@ -367,9 +367,12 @@ class Random(_random.Random):
# 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:
diff --git a/Lib/test/test_random.py b/Lib/test/test_random.py
index 50d4e94ca2..42c68dd1c2 100644
--- a/Lib/test/test_random.py
+++ b/Lib/test/test_random.py
@@ -147,7 +147,6 @@ class TestBasicOps:
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)
@@ -156,6 +155,11 @@ class TestBasicOps:
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']
diff --git a/Misc/NEWS.d/next/Library/2020-04-18-19-40-00.bpo-40325.KWSvix.rst b/Misc/NEWS.d/next/Library/2020-04-18-19-40-00.bpo-40325.KWSvix.rst
new file mode 100644
index 0000000000..3df5fade66
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-04-18-19-40-00.bpo-40325.KWSvix.rst
@@ -0,0 +1 @@
+Deprecated support for set objects in random.sample().