From 5a85396298ae48e99e1bf954f68127b2176b746c Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Wed, 11 May 2022 09:51:40 -0500 Subject: [PATCH 1/9] Remove deprecated support for non-integer values (GH-31098)" This restores commit 6baa98e538b2e26f16eaaf462f99496e98d2cfb1. --- Doc/library/random.rst | 13 +++---- Lib/random.py | 52 ++++++--------------------- Lib/test/test_random.py | 80 +++++++++++++++++++---------------------- 3 files changed, 51 insertions(+), 94 deletions(-) diff --git a/Doc/library/random.rst b/Doc/library/random.rst index 72881b56a4b18c..83ebec112c7ba7 100644 --- a/Doc/library/random.rst +++ b/Doc/library/random.rst @@ -135,15 +135,10 @@ 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. + Calls such as ``randrange(10.0)`` and ``randrange(Fraction(10, 1))`` + now raise a :exc:`TypeError`. .. function:: randint(a, b) diff --git a/Lib/random.py b/Lib/random.py index 1f3530e880fce2..2066137ba2c7cd 100644 --- a/Lib/random.py +++ b/Lib/random.py @@ -282,27 +282,17 @@ 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 supports arbitrarily large ranges and is optimized + for common cases. """ # 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 = _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. @@ -312,37 +302,15 @@ def randrange(self, start, stop=None, step=_ONE): 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) + # Stop argument supplied. + istop = _index(stop) 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) + 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)) + raise ValueError(f"empty range in randrange({start}, {stop}, {step})") # Non-unit step argument supplied. if istep > 0: @@ -352,7 +320,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): diff --git a/Lib/test/test_random.py b/Lib/test/test_random.py index 32e7868ba4de76..fcf17a949c2a62 100644 --- a/Lib/test/test_random.py +++ b/Lib/test/test_random.py @@ -485,50 +485,44 @@ 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) - # 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') - # 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) - - def test_randrange_argument_handling(self): - randrange = self.gen.randrange - with self.assertWarns(DeprecationWarning): - randrange(10.0, 20, 2) - with self.assertWarns(DeprecationWarning): - randrange(10, 20.0, 2) - with self.assertWarns(DeprecationWarning): - randrange(10, 20, 1.0) - with self.assertWarns(DeprecationWarning): - 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) + raises_value_error(3, 3) + raises_value_error(-721) + raises_value_error(0, 100, -12) + + # Zero step + raises_value_error(0, 42, 0) + raises_type_error(0, 42, 0.0) + raises_type_error(0, 0, 0.0) + + # Non-integer stop + 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') + raises_type_error(0, 2.71827, 2) + + # Non-integer start + raises_type_error(2.71827, 5) + raises_type_error(2.0, 5) + raises_type_error(Fraction(2, 1), 5) + raises_type_error('2', 5) + raises_type_error(2.71827, 5, 2) + + # Non-integer step + 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_step(self): # bpo-42772: When stop is None, the step argument was being ignored. From 8715eee98442812f9a4795795acec41e2f0c1a26 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Wed, 11 May 2022 10:07:58 -0500 Subject: [PATCH 2/9] Add whatsnew entry --- Doc/whatsnew/3.12.rst | 6 +++++- .../Library/2022-05-11-10-06-31.gh-issue-86388.7ivUtT.rst | 5 +++++ 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2022-05-11-10-06-31.gh-issue-86388.7ivUtT.rst diff --git a/Doc/whatsnew/3.12.rst b/Doc/whatsnew/3.12.rst index 461d9db793400d..05ff7b476f6a03 100644 --- a/Doc/whatsnew/3.12.rst +++ b/Doc/whatsnew/3.12.rst @@ -102,7 +102,11 @@ Deprecated Removed ======= - +* Removed randrange() functionality deprecated since Python 3.10. Formerly, +randrange(10.0) losslessly converted to randrange(10). Now, it raises a +TypeError. Also, the exception raised for non-integral values such as +randrange(10.5) or randrange('10') has been changed from ValueError to +TypeError. (gh-86388) Porting to Python 3.12 ====================== diff --git a/Misc/NEWS.d/next/Library/2022-05-11-10-06-31.gh-issue-86388.7ivUtT.rst b/Misc/NEWS.d/next/Library/2022-05-11-10-06-31.gh-issue-86388.7ivUtT.rst new file mode 100644 index 00000000000000..13eb5d122b28ae --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-05-11-10-06-31.gh-issue-86388.7ivUtT.rst @@ -0,0 +1,5 @@ +Removed randrange() functionality deprecated since Python 3.10. Formerly, +randrange(10.0) losslessly converted to randrange(10). Now, it raises a +TypeError. Also, the exception raised for non-integral values such as +randrange(10.5) or randrange('10') has been changed from ValueError to +TypeError. From 0cecfcb7efed135edac842073728a6af27cfaba9 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Wed, 11 May 2022 10:17:32 -0500 Subject: [PATCH 3/9] Update versionchanged. --- Doc/library/random.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/random.rst b/Doc/library/random.rst index 83ebec112c7ba7..2d61fc66bd92fe 100644 --- a/Doc/library/random.rst +++ b/Doc/library/random.rst @@ -135,7 +135,7 @@ Functions for integers values. Formerly it used a style like ``int(random()*n)`` which could produce slightly uneven distributions. - .. versionchanged:: 3.11 + .. versionchanged:: 3.12 Automatic conversion of non-integer types is no longer supported. Calls such as ``randrange(10.0)`` and ``randrange(Fraction(10, 1))`` now raise a :exc:`TypeError`. From ddd9b1bbdcb34727f3297336f0fffb968b854b33 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Wed, 11 May 2022 10:22:50 -0500 Subject: [PATCH 4/9] Nicer line wrapping --- Lib/random.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Lib/random.py b/Lib/random.py index 2066137ba2c7cd..35b60fe163c79a 100644 --- a/Lib/random.py +++ b/Lib/random.py @@ -284,9 +284,8 @@ 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 supports arbitrarily large ranges and is optimized - for common cases. + Roughly equivalent to ``choice(range(start, stop, step))`` but + supports arbitrarily large ranges and is optimized for common cases. """ From e68d46e1249a8e52f6c1a514b967c7e49d490998 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Wed, 11 May 2022 10:27:23 -0500 Subject: [PATCH 5/9] Make the quoting style consistent --- Lib/random.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/random.py b/Lib/random.py index 35b60fe163c79a..3cf82a7ddfb0fc 100644 --- a/Lib/random.py +++ b/Lib/random.py @@ -296,7 +296,7 @@ def randrange(self, start, stop=None, step=_ONE): # 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') + raise TypeError("Missing a non-None stop argument") if istart > 0: return self._randbelow(istart) raise ValueError("empty range for randrange()") From d7e0f55d939ffae09700b9fa8ed077e699b42e7c Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Wed, 11 May 2022 10:41:53 -0500 Subject: [PATCH 6/9] Sync the main docs with the docstring --- Doc/library/random.rst | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/Doc/library/random.rst b/Doc/library/random.rst index 2d61fc66bd92fe..613fbce0fdf20d 100644 --- a/Doc/library/random.rst +++ b/Doc/library/random.rst @@ -123,12 +123,16 @@ Functions for integers .. function:: randrange(stop) randrange(start, stop[, step]) - Return a randomly selected element from ``range(start, stop, step)``. This is - equivalent to ``choice(range(start, stop, step))``, but doesn't actually build a - range object. + Return a randomly selected element from ``range(start, stop, step)``. - The positional argument pattern matches that of :func:`range`. Keyword arguments - should not be used because the function may use them in unexpected ways. + This is roughly equivalent to ``choice(range(start, stop, step))`` but + supports arbitrarily large ranges and is optimized for common cases. + + The positional argument pattern matches the :func:`range` function. + + Keyword arguments should not be used because they can interpreted + in unexpected ways. For example ``range(start=100)`` is interpreted + as ``range(0, 100, 1)``. .. versionchanged:: 3.2 :meth:`randrange` is more sophisticated about producing equally distributed From 1d5f011bc47207ff7f65c33fa48d449f0aaab0b9 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Wed, 11 May 2022 16:10:25 -0500 Subject: [PATCH 7/9] Accept reviewer edits --- Doc/whatsnew/3.12.rst | 13 +++++++------ Lib/random.py | 2 +- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/Doc/whatsnew/3.12.rst b/Doc/whatsnew/3.12.rst index 05ff7b476f6a03..1bf293858fdb9c 100644 --- a/Doc/whatsnew/3.12.rst +++ b/Doc/whatsnew/3.12.rst @@ -102,12 +102,6 @@ Deprecated Removed ======= -* Removed randrange() functionality deprecated since Python 3.10. Formerly, -randrange(10.0) losslessly converted to randrange(10). Now, it raises a -TypeError. Also, the exception raised for non-integral values such as -randrange(10.5) or randrange('10') has been changed from ValueError to -TypeError. (gh-86388) - Porting to Python 3.12 ====================== @@ -124,6 +118,13 @@ Changes in the Python API contain ASCII letters and digits and underscore. (Contributed by Serhiy Storchaka in :gh:`91760`.) +* Removed randrange() functionality deprecated since Python 3.10. Formerly, + randrange(10.0) losslessly converted to randrange(10). Now, it raises a + TypeError. Also, the exception raised for non-integral values such as + randrange(10.5) or randrange('10') has been changed from ValueError to + TypeError. (gh-86388) + + Build Changes ============= diff --git a/Lib/random.py b/Lib/random.py index 3cf82a7ddfb0fc..a2dfcb574bd5eb 100644 --- a/Lib/random.py +++ b/Lib/random.py @@ -309,7 +309,7 @@ def randrange(self, start, stop=None, step=_ONE): if istep == 1: if width > 0: return istart + self._randbelow(width) - raise ValueError(f"empty range in randrange({start}, {stop}, {step})") + raise ValueError(f"empty range in randrange({start}, {stop})") # Non-unit step argument supplied. if istep > 0: From 423013138a7001664860fcc3b05fb4b807b715f9 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Wed, 11 May 2022 16:24:27 -0500 Subject: [PATCH 8/9] Add a motivation. --- Doc/whatsnew/3.12.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Doc/whatsnew/3.12.rst b/Doc/whatsnew/3.12.rst index 1bf293858fdb9c..4194fd1814b68b 100644 --- a/Doc/whatsnew/3.12.rst +++ b/Doc/whatsnew/3.12.rst @@ -122,7 +122,8 @@ Changes in the Python API randrange(10.0) losslessly converted to randrange(10). Now, it raises a TypeError. Also, the exception raised for non-integral values such as randrange(10.5) or randrange('10') has been changed from ValueError to - TypeError. (gh-86388) + TypeError. This also prevents bugs where ``randrange(1e25)`` would silently + select from a larger range than ``randrange(10**25)``. (gh-86388) From abed0db71ded3bddf096c7677830b0c922b2ec01 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Wed, 11 May 2022 21:57:46 -0500 Subject: [PATCH 9/9] Add credit --- Doc/whatsnew/3.12.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/whatsnew/3.12.rst b/Doc/whatsnew/3.12.rst index 4194fd1814b68b..b4b20fcdbe565d 100644 --- a/Doc/whatsnew/3.12.rst +++ b/Doc/whatsnew/3.12.rst @@ -123,8 +123,8 @@ Changes in the Python API TypeError. Also, the exception raised for non-integral values such as randrange(10.5) or randrange('10') has been changed from ValueError to TypeError. This also prevents bugs where ``randrange(1e25)`` would silently - select from a larger range than ``randrange(10**25)``. (gh-86388) - + select from a larger range than ``randrange(10**25)``. + (Originally suggested by Serhiy Storchaka gh-86388.) Build Changes