[Python-checkins] bpo-37319: Improve documentation, code and tests of randrange. (GH-19112)

serhiy-storchaka webhook-mailer at python.org
Mon Jan 25 16:02:39 EST 2021


https://github.com/python/cpython/commit/f066bd94b9225a5a3c4ade5fc3ff81e3c49b7b32
commit: f066bd94b9225a5a3c4ade5fc3ff81e3c49b7b32
branch: master
author: Serhiy Storchaka <storchaka at gmail.com>
committer: serhiy-storchaka <storchaka at gmail.com>
date: 2021-01-25T23:02:04+02:00
summary:

bpo-37319: Improve documentation, code and tests of randrange. (GH-19112)

files:
M Doc/library/random.rst
M Doc/whatsnew/3.10.rst
M Lib/random.py
M Lib/test/test_random.py
M Misc/NEWS.d/3.10.0a4.rst

diff --git a/Doc/library/random.rst b/Doc/library/random.rst
index 07ee011521429..9d85c2b9958eb 100644
--- a/Doc/library/random.rst
+++ b/Doc/library/random.rst
@@ -141,8 +141,9 @@ Functions for integers
       ``randrange(10)``.  In the future, this will raise a :exc:`TypeError`.
 
    .. deprecated:: 3.10
-      The exception raised for non-integral values such as ``range(10.5)``
-      will be changed from :exc:`ValueError` to :exc:`TypeError`.
+      The exception raised for non-integral values such as ``randrange(10.5)``
+      or ``randrange('10')`` will be changed from :exc:`ValueError` to
+      :exc:`TypeError`.
 
 .. function:: randint(a, b)
 
diff --git a/Doc/whatsnew/3.10.rst b/Doc/whatsnew/3.10.rst
index a6c3fbbff916e..3026a1ac0e542 100644
--- a/Doc/whatsnew/3.10.rst
+++ b/Doc/whatsnew/3.10.rst
@@ -530,6 +530,10 @@ Deprecated
   as appropriate to help identify code which needs updating during
   this transition.
 
+* Non-integer arguments to :func:`random.randrange` are deprecated.
+  The :exc:`ValueError` is deprecated in favor of a :exc:`TypeError`.
+  (Contributed by Serhiy Storchaka and Raymond Hettinger in :issue:`37319`.)
+
 * The various ``load_module()`` methods of :mod:`importlib` have been
   documented as deprecated since Python 3.6, but will now also trigger
   a :exc:`DeprecationWarning`. Use
diff --git a/Lib/random.py b/Lib/random.py
index 30186fc7a3b50..187b0a016947a 100644
--- a/Lib/random.py
+++ b/Lib/random.py
@@ -303,17 +303,15 @@ def randrange(self, start, stop=None, step=_ONE):
         try:
             istart = _index(start)
         except TypeError:
-            if int(start) == start:
-                istart = int(start)
-                _warn('Float arguments to randrange() have been deprecated\n'
-                      'since Python 3.10 and will be removed in a subsequent '
-                      'version.',
-                      DeprecationWarning, 2)
-            else:
+            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)
         if stop is None:
             # We don't check for "step != 1" because it hasn't been
             # type checked and converted to an integer yet.
@@ -327,31 +325,29 @@ def randrange(self, start, stop=None, step=_ONE):
         try:
             istop = _index(stop)
         except TypeError:
-            if int(stop) == stop:
-                istop = int(stop)
-                _warn('Float arguments to randrange() have been deprecated\n'
-                      'since Python 3.10 and will be removed in a subsequent '
-                      'version.',
-                      DeprecationWarning, 2)
-            else:
+            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)
+        width = istop - istart
         try:
             istep = _index(step)
         except TypeError:
-            if int(step) == step:
-                istep = int(step)
-                _warn('Float arguments to randrange() have been deprecated\n'
-                      'since Python 3.10 and will be removed in a subsequent '
-                      'version.',
-                      DeprecationWarning, 2)
-            else:
+            istep = int(step)
+            if istep != step:
                 _warn('randrange() will raise TypeError in the future',
                       DeprecationWarning, 2)
                 raise ValueError("non-integer step for randrange()")
-        width = istop - istart
+            _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)
diff --git a/Lib/test/test_random.py b/Lib/test/test_random.py
index 35ae4e6c3c80c..66908868a6e9e 100644
--- a/Lib/test/test_random.py
+++ b/Lib/test/test_random.py
@@ -509,11 +509,24 @@ def test_randrange_errors(self):
         raises(-721)
         raises(0, 100, -12)
         # Non-integer start/stop
-        raises(3.14159)
-        raises(0, 2.71828)
+        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)
-        raises(0, 42, 3.14159)
+        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
diff --git a/Misc/NEWS.d/3.10.0a4.rst b/Misc/NEWS.d/3.10.0a4.rst
index 882e03d82169a..57da9254587b4 100644
--- a/Misc/NEWS.d/3.10.0a4.rst
+++ b/Misc/NEWS.d/3.10.0a4.rst
@@ -605,12 +605,12 @@ deprecated in Python 3.7. Patch by Erlend E. Aasland
 .. nonce: Cfl1eR
 .. section: Library
 
-Harmonized random.randrange() argument handling to match range().
+Harmonized :func:`random.randrange` argument handling to match :func:`range`.
 
-* The integer test and conversion in randrange() now uses
-  operator.index().
-* Non-integer arguments to randrange() are deprecated.
-* The *ValueError* is deprecated in favor of a *TypeError*.
+* The integer test and conversion in ``randrange()`` now uses
+  :func:`operator.index`.
+* Non-integer arguments to ``randrange()`` are deprecated.
+* The ``ValueError`` is deprecated in favor of a ``TypeError``.
 * It now runs a little faster than before.
 
 (Contributed by Raymond Hettinger and Serhiy Storchaka.)



More information about the Python-checkins mailing list