summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorRaymond Hettinger <rhettinger@users.noreply.github.com>2021-10-16 15:16:53 (GMT)
committerGitHub <noreply@github.com>2021-10-16 15:16:53 (GMT)
commit5afa0a411243210a30526c7459a0ccff5cb88494 (patch)
tree4992ce242cf280b85395af4dfe06ffff2d8c4f45 /Lib
parent15ad52fbf607b6ccec44a38a8a32a5f1fad635ee (diff)
downloadcpython-5afa0a411243210a30526c7459a0ccff5cb88494.zip
cpython-5afa0a411243210a30526c7459a0ccff5cb88494.tar.gz
cpython-5afa0a411243210a30526c7459a0ccff5cb88494.tar.bz2
bpo-42222: Remove deprecated support for non-integer values (GH-28983)
Diffstat (limited to 'Lib')
-rw-r--r--Lib/random.py52
-rw-r--r--Lib/test/test_random.py75
2 files changed, 49 insertions, 78 deletions
diff --git a/Lib/random.py b/Lib/random.py
index 3569d58..92a71e1 100644
--- a/Lib/random.py
+++ b/Lib/random.py
@@ -286,27 +286,17 @@ class Random(_random.Random):
## -------------------- 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.
@@ -316,37 +306,15 @@ class Random(_random.Random):
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:
@@ -356,7 +324,7 @@ class Random(_random.Random):
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 448624b..3c5511d 100644
--- a/Lib/test/test_random.py
+++ b/Lib/test/test_random.py
@@ -481,50 +481,53 @@ class SystemRandom_TestBasicOps(TestBasicOps, unittest.TestCase):
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)
+ raises_value_error(3, 3)
+ raises_value_error(-721)
+ raises_value_error(0, 100, -12)
+
+ # 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))
+ 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')
+
+ # 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))
+ 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.