Skip to content
Prev Previous commit
Next Next commit
Revert "Raise TypeError instead of ValueError."
This reverts commit 2e8cfda.
  • Loading branch information
serhiy-storchaka committed Dec 24, 2020
commit 6ff20ab2e58dd6164a5fbf136edacad0a7893c7d
4 changes: 0 additions & 4 deletions Doc/library/random.rst
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,6 @@ 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 TypeError("non-integer arg 1 for randrange()")
raise ValueError("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 TypeError("non-integer stop for randrange()")
raise ValueError("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 TypeError("non-integer step for randrange()")
raise ValueError("non-integer step for randrange()")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While we're at it, the exception type should be converted to TypeError.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we raise it with custom message or reraise the exception raised by index()?

_warn('non-integer step for randrange()',
DeprecationWarning, 2)
# Fast path.
Expand Down
38 changes: 13 additions & 25 deletions Lib/test/test_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,40 +529,28 @@ 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):
randrange = self.gen.randrange
raises = partial(self.assertRaises, ValueError, randrange)
raises = partial(self.assertRaises, ValueError, self.gen.randrange)
# Empty range
raises(3, 3)
raises(-721)
raises(0, 100, -12)
self.assertWarns(DeprecationWarning, raises, 3, 3, 1.0)
# Non-integer start/stop
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')
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')
# Zero and non-integer step
raises(0, 42, 0)
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')
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')

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,3 +1 @@
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).