From 04eb965a4047a572a79181a635884db15890e715 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Sat, 18 Apr 2020 19:40:34 -0700 Subject: [PATCH 1/3] bpo-40325: Deprecate set object support in random.sample() --- Doc/library/random.rst | 7 +++++++ Lib/random.py | 5 ++++- Lib/test/test_random.py | 6 +++++- .../next/Library/2020-04-18-19-40-00.bpo-40325.KWSvix.rst | 1 + 4 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2020-04-18-19-40-00.bpo-40325.KWSvix.rst diff --git a/Doc/library/random.rst b/Doc/library/random.rst index 9964af46b2efee..4f5040b41f25ee 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. They 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 3243938282b50e..f1df18d5c187b8 100644 --- a/Lib/random.py +++ b/Lib/random.py @@ -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: diff --git a/Lib/test/test_random.py b/Lib/test/test_random.py index 50d4e94ca22f89..42c68dd1c24422 100644 --- a/Lib/test/test_random.py +++ b/Lib/test/test_random.py @@ -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) @@ -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'] 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 00000000000000..0b468488456e60 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-04-18-19-40-00.bpo-40325.KWSvix.rst @@ -0,0 +1 @@ +Deprecatated support for set objects in random.sample(). From d9fc620b7a1fe4a6cb395a4e3c7fa4dbe7c924eb Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Sat, 18 Apr 2020 19:50:21 -0700 Subject: [PATCH 2/3] Fix markup --- Doc/library/random.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Doc/library/random.rst b/Doc/library/random.rst index 4f5040b41f25ee..82e900d3a20ab0 100644 --- a/Doc/library/random.rst +++ b/Doc/library/random.rst @@ -231,10 +231,10 @@ Functions for sequences is raised. .. deprecated:: 3.9 - In the future, the *population* must be a sequence. Instances of - :class:`set` are no longer supported. They must first be converted - to a :class:`list` or :class:`tuple`, preferably in a deterministic - order so that the sample is reproducible. + 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 From d08eca3ac72a38c26a4bcf9ce3cde7cae07d00d4 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Sat, 18 Apr 2020 22:59:01 -0700 Subject: [PATCH 3/3] Update Misc/NEWS.d/next/Library/2020-04-18-19-40-00.bpo-40325.KWSvix.rst Fix stutter Co-Authored-By: Tim Peters --- .../next/Library/2020-04-18-19-40-00.bpo-40325.KWSvix.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 0b468488456e60..3df5fade6676a7 100644 --- 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 @@ -1 +1 @@ -Deprecatated support for set objects in random.sample(). +Deprecated support for set objects in random.sample().