Skip to content
Merged
Prev Previous commit
Next Next commit
Raise TypeError instead of ValueError.
  • Loading branch information
serhiy-storchaka committed Oct 31, 2020
commit 2e8cfda3f88732ef03b6002ed23eef536826321b
4 changes: 4 additions & 0 deletions Doc/library/random.rst
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ Functions for integers
values. Formerly it used a style like ``int(random()*n)`` which could produce
slightly uneven distributions.

.. versionchanged:: 3.10
Formerly :exc:`ValueError` could be raised for non-integer arguments
instead of :exc:`TypeError`.

.. deprecated:: 3.10
Accepting non-integer arguments is deprecated.

Expand Down
6 changes: 3 additions & 3 deletions Lib/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ def randrange(self, start, stop=None, step=1):
except TypeError:
istart = int(start)
if istart != start:
raise ValueError("non-integer arg 1 for randrange()")
raise TypeError("non-integer arg 1 for randrange()")
_warn('non-integer arg 1 for randrange()',
DeprecationWarning, 2)
if stop is None:
Expand All @@ -317,7 +317,7 @@ def randrange(self, start, stop=None, step=1):
except TypeError:
istop = int(stop)
if istop != stop:
raise ValueError("non-integer stop for randrange()")
raise TypeError("non-integer stop for randrange()")
_warn('non-integer stop for randrange()',
DeprecationWarning, 2)
width = istop - istart
Expand All @@ -326,7 +326,7 @@ def randrange(self, start, stop=None, step=1):
except TypeError:
istep = int(step)
if istep != step:
raise ValueError("non-integer step for randrange()")
raise TypeError("non-integer step for randrange()")
_warn('non-integer step for randrange()',
DeprecationWarning, 2)
# Fast path.
Expand Down
38 changes: 25 additions & 13 deletions Lib/test/test_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,28 +504,40 @@ def test_randrange_nonunit_step(self):
rint = self.gen.randrange(0, 2, 2)
self.assertEqual(rint, 0)

def test_randrange_non_integers(self):
randrange = self.gen.randrange
with self.assertWarns(DeprecationWarning):
self.assertIn(randrange(3.0), range(3))
with self.assertWarns(DeprecationWarning):
self.assertIn(randrange(0, 3.0), range(0, 3))
with self.assertWarns(DeprecationWarning):
self.assertIn(randrange(0, 42, 1.0), range(0, 42, 1))
with self.assertWarns(DeprecationWarning):
self.assertIn(randrange(0, 42, 3.0), range(0, 42, 3))

def test_randrange_errors(self):
raises = partial(self.assertRaises, ValueError, self.gen.randrange)
randrange = self.gen.randrange
raises = partial(self.assertRaises, ValueError, randrange)
# Empty range
raises(3, 3)
raises(-721)
raises(0, 100, -12)
self.assertWarns(DeprecationWarning, raises, 3, 3, 1.0)
# Non-integer start/stop
raises(3.14159)
self.assertWarns(DeprecationWarning, self.gen.randrange, 3.0)
self.assertWarns(DeprecationWarning, self.gen.randrange, Fraction(3, 1))
raises('3')
raises(0, 2.71828)
self.assertWarns(DeprecationWarning, self.gen.randrange, 0, 2.0)
self.assertWarns(DeprecationWarning, self.gen.randrange, 0, Fraction(2, 1))
raises(0, '2')
self.assertRaises(TypeError, randrange, 3.14159)
self.assertWarns(DeprecationWarning, randrange, 3.0)
self.assertWarns(DeprecationWarning, randrange, Fraction(3, 1))
self.assertRaises(TypeError, randrange, '3')
self.assertRaises(TypeError, randrange, 0, 2.71828)
self.assertWarns(DeprecationWarning, randrange, 0, 2.0)
self.assertWarns(DeprecationWarning, randrange, 0, Fraction(2, 1))
self.assertRaises(TypeError, randrange, 0, '2')
# Zero and non-integer step
raises(0, 42, 0)
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))
raises(0, 42, '3')
self.assertRaises(TypeError, randrange, 0, 42, 3.14159)
self.assertWarns(DeprecationWarning, randrange, 0, 42, 3.0)
self.assertWarns(DeprecationWarning, randrange, 0, 42, Fraction(3, 1))
self.assertRaises(TypeError, randrange, 0, 42, '3')

def test_randbelow_logic(self, _log=log, int=int):
# check bitcount transition points: 2**i and 2**(i+1)-1
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
Deprecated support of non-integer arguments in :func:`random.randrange`.
:exc:`TypeError` is now always raised for wrong type of arguments
(formerly :exc:`ValueError` could be raised for some non-integer arguments).