Skip to content

Commit 3ad3c36

Browse files
Update docs and emit deprecation warning for ValueError.
1 parent 6ff20ab commit 3ad3c36

File tree

5 files changed

+39
-13
lines changed

5 files changed

+39
-13
lines changed

Doc/library/random.rst

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,14 @@ Functions for integers
136136
slightly uneven distributions.
137137

138138
.. deprecated:: 3.10
139-
Accepting non-integer arguments is deprecated.
139+
The automatic conversion of non-integer types to equivalent integers is
140+
deprecated. Currently ``randrange(10.0)`` is losslessly converted to
141+
``randrange(10)``. In the future, this will raise a :exc:`TypeError`.
142+
143+
.. deprecated:: 3.10
144+
The exception raised for non-integral values such as ``randrange(10.5)``
145+
or ``randrange('10')`` will be changed from :exc:`ValueError` to
146+
:exc:`TypeError`.
140147

141148

142149
.. function:: randint(a, b)

Doc/whatsnew/3.10.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -442,8 +442,9 @@ Deprecated
442442
as appropriate to help identify code which needs updating during
443443
this transition.
444444

445-
* Deprecated support of non-integer arguments in :func:`random.randrange`.
446-
(Contributed by Serhiy Storchaka in :issue:`37319`.)
445+
* Non-integer arguments to :func:`random.randrange` are deprecated.
446+
The :exc:`ValueError` is deprecated in favor of a :exc:`TypeError`.
447+
(Contributed by Serhiy Storchaka and Raymond Hettinger in :issue:`37319`.)
447448

448449
* The various ``load_module()`` methods of :mod:`importlib` have been
449450
documented as deprecated since Python 3.6, but will now also trigger

Lib/random.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,12 @@ def randrange(self, start, stop=None, step=1):
303303
except TypeError:
304304
istart = int(start)
305305
if istart != start:
306+
_warn('randrange() will raise TypeError in the future',
307+
DeprecationWarning, 2)
306308
raise ValueError("non-integer arg 1 for randrange()")
307-
_warn('non-integer arg 1 for randrange()',
309+
_warn('non-integer arguments to randrange() have been deprecated '
310+
'since Python 3.10 and will be removed in a subsequent '
311+
'version',
308312
DeprecationWarning, 2)
309313
if stop is None:
310314
if istart > 0:
@@ -317,17 +321,25 @@ def randrange(self, start, stop=None, step=1):
317321
except TypeError:
318322
istop = int(stop)
319323
if istop != stop:
324+
_warn('randrange() will raise TypeError in the future',
325+
DeprecationWarning, 2)
320326
raise ValueError("non-integer stop for randrange()")
321-
_warn('non-integer stop for randrange()',
327+
_warn('non-integer arguments to randrange() have been deprecated '
328+
'since Python 3.10 and will be removed in a subsequent '
329+
'version',
322330
DeprecationWarning, 2)
323331
width = istop - istart
324332
try:
325333
istep = _index(step)
326334
except TypeError:
327335
istep = int(step)
328336
if istep != step:
337+
_warn('randrange() will raise TypeError in the future',
338+
DeprecationWarning, 2)
329339
raise ValueError("non-integer step for randrange()")
330-
_warn('non-integer step for randrange()',
340+
_warn('non-integer arguments to randrange() have been deprecated '
341+
'since Python 3.10 and will be removed in a subsequent '
342+
'version',
331343
DeprecationWarning, 2)
332344
# Fast path.
333345
if istep == 1:

Lib/test/test_random.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -537,20 +537,20 @@ def test_randrange_errors(self):
537537
raises(0, 100, -12)
538538
self.assertWarns(DeprecationWarning, raises, 3, 3, 1.0)
539539
# Non-integer start/stop
540-
raises(3.14159)
540+
self.assertWarns(DeprecationWarning, raises, 3.14159)
541541
self.assertWarns(DeprecationWarning, self.gen.randrange, 3.0)
542542
self.assertWarns(DeprecationWarning, self.gen.randrange, Fraction(3, 1))
543-
raises('3')
544-
raises(0, 2.71828)
543+
self.assertWarns(DeprecationWarning, raises, '3')
544+
self.assertWarns(DeprecationWarning, raises, 0, 2.71828)
545545
self.assertWarns(DeprecationWarning, self.gen.randrange, 0, 2.0)
546546
self.assertWarns(DeprecationWarning, self.gen.randrange, 0, Fraction(2, 1))
547-
raises(0, '2')
547+
self.assertWarns(DeprecationWarning, raises, 0, '2')
548548
# Zero and non-integer step
549549
raises(0, 42, 0)
550-
raises(0, 42, 3.14159)
550+
self.assertWarns(DeprecationWarning, raises, 0, 42, 3.14159)
551551
self.assertWarns(DeprecationWarning, self.gen.randrange, 0, 42, 3.0)
552552
self.assertWarns(DeprecationWarning, self.gen.randrange, 0, 42, Fraction(3, 1))
553-
raises(0, 42, '3')
553+
self.assertWarns(DeprecationWarning, raises, 0, 42, '3')
554554

555555
def test_randbelow_logic(self, _log=log, int=int):
556556
# check bitcount transition points: 2**i and 2**(i+1)-1
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
1-
Deprecated support of non-integer arguments in :func:`random.randrange`.
1+
Harmonized :func:`random.randrange` argument handling to better match
2+
that used by :func:`range`.
3+
4+
* The integer test/conversion in ``randrange()`` now uses :func:`operator.index`.
5+
* Non-integer arguments to ``randrange()`` are deprecated.
6+
* The ``ValueError`` is deprecated in favor of a ``TypeError``.
7+
* It now runs a little faster than before.

0 commit comments

Comments
 (0)