From 8cf5d5438111a5214f11fce0a19466a202e70888 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Fri, 15 Oct 2021 11:11:32 -0500 Subject: [PATCH 01/10] Remove deprecated non-integer support --- Lib/random.py | 61 +++++++++++++-------------------------------------- 1 file changed, 15 insertions(+), 46 deletions(-) diff --git a/Lib/random.py b/Lib/random.py index 3569d58cc42430..828eeec7c61979 100644 --- a/Lib/random.py +++ b/Lib/random.py @@ -97,7 +97,6 @@ SG_MAGICCONST = 1.0 + _log(4.5) BPF = 53 # Number of bits in a float RECIP_BPF = 2 ** -BPF -_ONE = 1 class Random(_random.Random): @@ -285,7 +284,7 @@ def randbytes(self, n): ## -------------------- integer methods ------------------- - def randrange(self, start, stop=None, step=_ONE): + def randrange(self, start, stop=None, step=1): """Choose a random item from range(start, stop[, step]). This fixes the problem with randint() which includes the @@ -295,68 +294,38 @@ def randrange(self, start, stop=None, step=_ONE): # This code is a bit messy to make it fast for the # common case while still doing adequate error checking. - try: - istart = _index(start) - except TypeError: - istart = int(start) - if istart != start: - _warn('randrange() will raise TypeError in the future', - DeprecationWarning, 2) - raise ValueError("non-integer arg 1 for randrange()") - _warn('non-integer arguments to randrange() have been deprecated ' - 'since Python 3.10 and will be removed in a subsequent ' - 'version', - DeprecationWarning, 2) + istart = int(start) + if istart != start: + raise ValueError("non-integer arg 1 for randrange()") if stop is None: - # We don't check for "step != 1" because it hasn't been - # type checked and converted to an integer yet. - if step is not _ONE: - raise TypeError('Missing a non-None stop argument') if istart > 0: return self._randbelow(istart) raise ValueError("empty range for randrange()") # stop argument supplied. - try: - istop = _index(stop) - except TypeError: - istop = int(stop) - if istop != stop: - _warn('randrange() will raise TypeError in the future', - DeprecationWarning, 2) - raise ValueError("non-integer stop for randrange()") - _warn('non-integer arguments to randrange() have been deprecated ' - 'since Python 3.10 and will be removed in a subsequent ' - 'version', - DeprecationWarning, 2) + istop = int(stop) + if istop != stop: + raise ValueError("non-integer stop for randrange()") width = istop - istart - try: - istep = _index(step) - except TypeError: - istep = int(step) - if istep != step: - _warn('randrange() will raise TypeError in the future', - DeprecationWarning, 2) - raise ValueError("non-integer step for randrange()") - _warn('non-integer arguments to randrange() have been deprecated ' - 'since Python 3.10 and will be removed in a subsequent ' - 'version', - DeprecationWarning, 2) - # Fast path. - if istep == 1: - if width > 0: - return istart + self._randbelow(width) + if step == 1 and width > 0: + return istart + self._randbelow(width) + if step == 1: raise ValueError("empty range for randrange() (%d, %d, %d)" % (istart, istop, width)) # Non-unit step argument supplied. + istep = int(step) + if istep != step: + raise ValueError("non-integer step for randrange()") if istep > 0: n = (width + istep - 1) // istep elif istep < 0: n = (width + istep + 1) // istep else: raise ValueError("zero step for randrange()") + if n <= 0: raise ValueError("empty range for randrange()") + return istart + istep * self._randbelow(n) def randint(self, a, b): From 738e0904f7b5e97d70ae82d9c79ebc9804173364 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Fri, 15 Oct 2021 11:28:32 -0500 Subject: [PATCH 02/10] Add doc updates --- Doc/library/random.rst | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/Doc/library/random.rst b/Doc/library/random.rst index e444f9573246a6..a3f7245cf022fc 100644 --- a/Doc/library/random.rst +++ b/Doc/library/random.rst @@ -135,15 +135,9 @@ Functions for integers values. Formerly it used a style like ``int(random()*n)`` which could produce slightly uneven distributions. - .. deprecated:: 3.10 - The automatic conversion of non-integer types to equivalent integers is - deprecated. Currently ``randrange(10.0)`` is losslessly converted to - ``randrange(10)``. In the future, this will raise a :exc:`TypeError`. - - .. deprecated:: 3.10 - The exception raised for non-integral values such as ``randrange(10.5)`` - or ``randrange('10')`` will be changed from :exc:`ValueError` to - :exc:`TypeError`. + .. versionchanged:: 3.11 + Automatic conversion of non-integer types is no longer supported. + Float arguments such as ``randrange(10.0)`` now raise a :exc:`TypeError`. .. function:: randint(a, b) From cc8934654fe60fdd76a85856da122627d55193ba Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Fri, 15 Oct 2021 11:30:44 -0500 Subject: [PATCH 03/10] Add blurb --- .../NEWS.d/next/Library/2021-10-15-11-30-11.bpo-42222.hdHyac.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2021-10-15-11-30-11.bpo-42222.hdHyac.rst diff --git a/Misc/NEWS.d/next/Library/2021-10-15-11-30-11.bpo-42222.hdHyac.rst b/Misc/NEWS.d/next/Library/2021-10-15-11-30-11.bpo-42222.hdHyac.rst new file mode 100644 index 00000000000000..9b29fa44d7711e --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-10-15-11-30-11.bpo-42222.hdHyac.rst @@ -0,0 +1 @@ +Removed deprecated support for float arguments in *randrange()*. From eb76fbd95562b3a6e4fef0cb53be84ace4928e5c Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Fri, 15 Oct 2021 11:48:11 -0500 Subject: [PATCH 04/10] index() version --- Lib/random.py | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/Lib/random.py b/Lib/random.py index 828eeec7c61979..5ee55c24958173 100644 --- a/Lib/random.py +++ b/Lib/random.py @@ -97,6 +97,7 @@ SG_MAGICCONST = 1.0 + _log(4.5) BPF = 53 # Number of bits in a float RECIP_BPF = 2 ** -BPF +_ONE = 1 class Random(_random.Random): @@ -284,7 +285,7 @@ def randbytes(self, n): ## -------------------- integer methods ------------------- - def randrange(self, start, stop=None, step=1): + def randrange(self, start, stop=None, step=_ONE): """Choose a random item from range(start, stop[, step]). This fixes the problem with randint() which includes the @@ -294,38 +295,35 @@ def randrange(self, start, stop=None, step=1): # This code is a bit messy to make it fast for the # common case while still doing adequate error checking. - istart = int(start) - if istart != start: - raise ValueError("non-integer arg 1 for randrange()") + istart = _index(start) if stop is None: + # We don't check for "step != 1" because it hasn't been + # type checked and converted to an integer yet. + if step is not _ONE: + raise TypeError('Missing a non-None stop argument') if istart > 0: return self._randbelow(istart) raise ValueError("empty range for randrange()") - # stop argument supplied. - istop = int(stop) - if istop != stop: - raise ValueError("non-integer stop for randrange()") + # Stop argument supplied. + istop = _index(stop) width = istop - istart - if step == 1 and width > 0: - return istart + self._randbelow(width) - if step == 1: + istep = _index(step) + # Fast path. + if istep == 1: + if width > 0: + return istart + self._randbelow(width) raise ValueError("empty range for randrange() (%d, %d, %d)" % (istart, istop, width)) # Non-unit step argument supplied. - istep = int(step) - if istep != step: - raise ValueError("non-integer step for randrange()") if istep > 0: n = (width + istep - 1) // istep elif istep < 0: n = (width + istep + 1) // istep else: raise ValueError("zero step for randrange()") - if n <= 0: raise ValueError("empty range for randrange()") - return istart + istep * self._randbelow(n) def randint(self, a, b): From e509f7f816ff5c0ea310a3d554fa3aa5855f7786 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Fri, 15 Oct 2021 12:50:16 -0500 Subject: [PATCH 05/10] Update tests --- Lib/test/test_random.py | 68 ++++++++++++++++++++--------------------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/Lib/test/test_random.py b/Lib/test/test_random.py index 448624b79be5db..e74debba0927df 100644 --- a/Lib/test/test_random.py +++ b/Lib/test/test_random.py @@ -481,50 +481,50 @@ def test_randrange_nonunit_step(self): self.assertEqual(rint, 0) def test_randrange_errors(self): - raises = partial(self.assertRaises, ValueError, self.gen.randrange) + raises_value_error = partial(self.assertRaises, ValueError, self.gen.randrange) + raises_type_error = partial(self.assertRaises, TypeError, self.gen.randrange) # Empty range - raises(3, 3) - raises(-721) - raises(0, 100, -12) + raises_value_error(3, 3) + raises_value_error(-721) + raises_value_error(0, 100, -12) # Non-integer start/stop - self.assertWarns(DeprecationWarning, raises, 3.14159) - self.assertWarns(DeprecationWarning, self.gen.randrange, 3.0) - self.assertWarns(DeprecationWarning, self.gen.randrange, Fraction(3, 1)) - self.assertWarns(DeprecationWarning, raises, '3') - self.assertWarns(DeprecationWarning, raises, 0, 2.71828) - self.assertWarns(DeprecationWarning, self.gen.randrange, 0, 2.0) - self.assertWarns(DeprecationWarning, self.gen.randrange, 0, Fraction(2, 1)) - self.assertWarns(DeprecationWarning, raises, 0, '2') + + raises_type_error(3.14159) + raises_type_error(3.0) + raises_type_error(Fraction(3, 1)) + raises_type_error('3') + raises_type_error(0, 2.71827) + raises_type_error(0, 2.0) + raises_type_error(0, Fraction(2, 1)) + raises_type_error(0, '2') + # Zero and non-integer step - raises(0, 42, 0) - self.assertWarns(DeprecationWarning, raises, 0, 42, 0.0) - self.assertWarns(DeprecationWarning, raises, 0, 0, 0.0) - self.assertWarns(DeprecationWarning, raises, 0, 42, 3.14159) - self.assertWarns(DeprecationWarning, self.gen.randrange, 0, 42, 3.0) - self.assertWarns(DeprecationWarning, self.gen.randrange, 0, 42, Fraction(3, 1)) - self.assertWarns(DeprecationWarning, raises, 0, 42, '3') - self.assertWarns(DeprecationWarning, self.gen.randrange, 0, 42, 1.0) - self.assertWarns(DeprecationWarning, raises, 0, 0, 1.0) + raises_value_error(0, 42, 0) + raises_type_error(0, 42, 0.0) + raises_type_error(0, 0, 0.0) + raises_type_error(0, 42, 3.14159) + raises_type_error(0, 42, 3.0) + raises_type_error(0, 42, Fraction(3, 1)) + raises_type_error(0, 42, '3') + raises_type_error(0, 42, 1.0) + raises_type_error(0, 0, 1.0) def test_randrange_argument_handling(self): randrange = self.gen.randrange - with self.assertWarns(DeprecationWarning): + with self.assertRaises(TypeError): randrange(10.0, 20, 2) - with self.assertWarns(DeprecationWarning): + with self.assertRaises(TypeError): randrange(10, 20.0, 2) - with self.assertWarns(DeprecationWarning): + with self.assertRaises(TypeError): randrange(10, 20, 1.0) - with self.assertWarns(DeprecationWarning): + with self.assertRaises(TypeError): randrange(10, 20, 2.0) - with self.assertWarns(DeprecationWarning): - with self.assertRaises(ValueError): - randrange(10.5) - with self.assertWarns(DeprecationWarning): - with self.assertRaises(ValueError): - randrange(10, 20.5) - with self.assertWarns(DeprecationWarning): - with self.assertRaises(ValueError): - randrange(10, 20, 1.5) + with self.assertRaises(TypeError): + randrange(10.5) + with self.assertRaises(TypeError): + randrange(10, 20.5) + with self.assertRaises(TypeError): + randrange(10, 20, 1.5) def test_randrange_step(self): # bpo-42772: When stop is None, the step argument was being ignored. From a87fcacff6535fca73f95e05d7990be77e0f6c97 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Fri, 15 Oct 2021 15:14:04 -0500 Subject: [PATCH 06/10] Tweek wording for the versionchanged entry --- Doc/library/random.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Doc/library/random.rst b/Doc/library/random.rst index a3f7245cf022fc..0ac0fe72dc3929 100644 --- a/Doc/library/random.rst +++ b/Doc/library/random.rst @@ -137,7 +137,8 @@ Functions for integers .. versionchanged:: 3.11 Automatic conversion of non-integer types is no longer supported. - Float arguments such as ``randrange(10.0)`` now raise a :exc:`TypeError`. + Calls such as ``randrange(10.0)`` and ``randrange(Fraction(10, 1))`` + now raise a :exc:`TypeError`. .. function:: randint(a, b) From 59a734b5b696ab30bca64e393c86b0fed7e3a36f Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Fri, 15 Oct 2021 15:16:49 -0500 Subject: [PATCH 07/10] Improve test organization --- Lib/test/test_random.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/Lib/test/test_random.py b/Lib/test/test_random.py index e74debba0927df..3c5511d8c7f1a3 100644 --- a/Lib/test/test_random.py +++ b/Lib/test/test_random.py @@ -483,12 +483,16 @@ def test_randrange_nonunit_step(self): def test_randrange_errors(self): raises_value_error = partial(self.assertRaises, ValueError, self.gen.randrange) raises_type_error = partial(self.assertRaises, TypeError, self.gen.randrange) + # Empty range raises_value_error(3, 3) raises_value_error(-721) raises_value_error(0, 100, -12) - # Non-integer start/stop + # Zero step + raises_value_error(0, 42, 0) + + # Non-integer start/stop/step raises_type_error(3.14159) raises_type_error(3.0) raises_type_error(Fraction(3, 1)) @@ -498,10 +502,9 @@ def test_randrange_errors(self): raises_type_error(0, Fraction(2, 1)) raises_type_error(0, '2') - # Zero and non-integer step - raises_value_error(0, 42, 0) - raises_type_error(0, 42, 0.0) - raises_type_error(0, 0, 0.0) + # Non-integer step + raises_type_error(0, 42, 1.0) + raises_type_error(0, 0, 1.0) raises_type_error(0, 42, 3.14159) raises_type_error(0, 42, 3.0) raises_type_error(0, 42, Fraction(3, 1)) From 8fe5c51827dec6adda3cb49ef684b9899286d049 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Fri, 15 Oct 2021 16:32:57 -0500 Subject: [PATCH 08/10] Improve docstring --- Lib/random.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Lib/random.py b/Lib/random.py index 5ee55c24958173..3008f5a8df8020 100644 --- a/Lib/random.py +++ b/Lib/random.py @@ -286,10 +286,10 @@ def randbytes(self, n): ## -------------------- integer methods ------------------- def randrange(self, start, stop=None, step=_ONE): - """Choose a random item from range(start, stop[, step]). + """Choose a random item from range(stop) or range(start, stop[, step]). - This fixes the problem with randint() which includes the - endpoint; in Python this is usually not what you want. + Roughly equivalent to: choice(range(start, stop, step)) + but runs faster and supports a larger range. """ From 9a014eab1825faac9e8ed2406756d59845dbf388 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Sat, 16 Oct 2021 00:40:37 -0500 Subject: [PATCH 09/10] More detailed error messages --- Lib/random.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/random.py b/Lib/random.py index 3008f5a8df8020..c7ed82186cc046 100644 --- a/Lib/random.py +++ b/Lib/random.py @@ -313,7 +313,7 @@ def randrange(self, start, stop=None, step=_ONE): if istep == 1: if width > 0: return istart + self._randbelow(width) - raise ValueError("empty range for randrange() (%d, %d, %d)" % (istart, istop, width)) + raise ValueError(f"empty range in randrange({start}, {stop}, {step})") # Non-unit step argument supplied. if istep > 0: @@ -323,7 +323,7 @@ def randrange(self, start, stop=None, step=_ONE): else: raise ValueError("zero step for randrange()") if n <= 0: - raise ValueError("empty range for randrange()") + raise ValueError(f"empty range in randrange({start}, {stop}, {step})") return istart + istep * self._randbelow(n) def randint(self, a, b): From e7a217f6430ef2b034d46f9aaf4e55e4e0a4e003 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Sat, 16 Oct 2021 09:53:59 -0500 Subject: [PATCH 10/10] Improve docstring --- Lib/random.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Lib/random.py b/Lib/random.py index c7ed82186cc046..92a71e14c480bd 100644 --- a/Lib/random.py +++ b/Lib/random.py @@ -288,8 +288,9 @@ def randbytes(self, n): def randrange(self, start, stop=None, step=_ONE): """Choose a random item from range(stop) or range(start, stop[, step]). - Roughly equivalent to: choice(range(start, stop, step)) - but runs faster and supports a larger range. + Roughly equivalent to ``choice(range(start, stop, step))`` + but supports arbitrarily large ranges and is optimized + for common cases. """