Skip to content

Commit 68fec31

Browse files
authored
gh-86388 Remove deprecated behaviors in randrange() (#92677)
1 parent f67d71b commit 68fec31

File tree

5 files changed

+73
-102
lines changed

5 files changed

+73
-102
lines changed

Doc/library/random.rst

+13-14
Original file line numberDiff line numberDiff line change
@@ -123,27 +123,26 @@ Functions for integers
123123
.. function:: randrange(stop)
124124
randrange(start, stop[, step])
125125

126-
Return a randomly selected element from ``range(start, stop, step)``. This is
127-
equivalent to ``choice(range(start, stop, step))``, but doesn't actually build a
128-
range object.
126+
Return a randomly selected element from ``range(start, stop, step)``.
129127

130-
The positional argument pattern matches that of :func:`range`. Keyword arguments
131-
should not be used because the function may use them in unexpected ways.
128+
This is roughly equivalent to ``choice(range(start, stop, step))`` but
129+
supports arbitrarily large ranges and is optimized for common cases.
130+
131+
The positional argument pattern matches the :func:`range` function.
132+
133+
Keyword arguments should not be used because they can interpreted
134+
in unexpected ways. For example ``range(start=100)`` is interpreted
135+
as ``range(0, 100, 1)``.
132136

133137
.. versionchanged:: 3.2
134138
:meth:`randrange` is more sophisticated about producing equally distributed
135139
values. Formerly it used a style like ``int(random()*n)`` which could produce
136140
slightly uneven distributions.
137141

138-
.. deprecated:: 3.10
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`.
142+
.. versionchanged:: 3.12
143+
Automatic conversion of non-integer types is no longer supported.
144+
Calls such as ``randrange(10.0)`` and ``randrange(Fraction(10, 1))``
145+
now raise a :exc:`TypeError`.
147146

148147
.. function:: randint(a, b)
149148

Doc/whatsnew/3.12.rst

+8-2
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,6 @@ Deprecated
102102
Removed
103103
=======
104104

105-
106-
107105
Porting to Python 3.12
108106
======================
109107

@@ -120,6 +118,14 @@ Changes in the Python API
120118
contain ASCII letters and digits and underscore.
121119
(Contributed by Serhiy Storchaka in :gh:`91760`.)
122120

121+
* Removed randrange() functionality deprecated since Python 3.10. Formerly,
122+
randrange(10.0) losslessly converted to randrange(10). Now, it raises a
123+
TypeError. Also, the exception raised for non-integral values such as
124+
randrange(10.5) or randrange('10') has been changed from ValueError to
125+
TypeError. This also prevents bugs where ``randrange(1e25)`` would silently
126+
select from a larger range than ``randrange(10**25)``.
127+
(Originally suggested by Serhiy Storchaka gh-86388.)
128+
123129

124130
Build Changes
125131
=============

Lib/random.py

+10-43
Original file line numberDiff line numberDiff line change
@@ -282,67 +282,34 @@ def randbytes(self, n):
282282
## -------------------- integer methods -------------------
283283

284284
def randrange(self, start, stop=None, step=_ONE):
285-
"""Choose a random item from range(start, stop[, step]).
285+
"""Choose a random item from range(stop) or range(start, stop[, step]).
286286
287-
This fixes the problem with randint() which includes the
288-
endpoint; in Python this is usually not what you want.
287+
Roughly equivalent to ``choice(range(start, stop, step))`` but
288+
supports arbitrarily large ranges and is optimized for common cases.
289289
290290
"""
291291

292292
# This code is a bit messy to make it fast for the
293293
# common case while still doing adequate error checking.
294-
try:
295-
istart = _index(start)
296-
except TypeError:
297-
istart = int(start)
298-
if istart != start:
299-
_warn('randrange() will raise TypeError in the future',
300-
DeprecationWarning, 2)
301-
raise ValueError("non-integer arg 1 for randrange()")
302-
_warn('non-integer arguments to randrange() have been deprecated '
303-
'since Python 3.10 and will be removed in a subsequent '
304-
'version',
305-
DeprecationWarning, 2)
294+
istart = _index(start)
306295
if stop is None:
307296
# We don't check for "step != 1" because it hasn't been
308297
# type checked and converted to an integer yet.
309298
if step is not _ONE:
310-
raise TypeError('Missing a non-None stop argument')
299+
raise TypeError("Missing a non-None stop argument")
311300
if istart > 0:
312301
return self._randbelow(istart)
313302
raise ValueError("empty range for randrange()")
314303

315-
# stop argument supplied.
316-
try:
317-
istop = _index(stop)
318-
except TypeError:
319-
istop = int(stop)
320-
if istop != stop:
321-
_warn('randrange() will raise TypeError in the future',
322-
DeprecationWarning, 2)
323-
raise ValueError("non-integer stop for randrange()")
324-
_warn('non-integer arguments to randrange() have been deprecated '
325-
'since Python 3.10 and will be removed in a subsequent '
326-
'version',
327-
DeprecationWarning, 2)
304+
# Stop argument supplied.
305+
istop = _index(stop)
328306
width = istop - istart
329-
try:
330-
istep = _index(step)
331-
except TypeError:
332-
istep = int(step)
333-
if istep != step:
334-
_warn('randrange() will raise TypeError in the future',
335-
DeprecationWarning, 2)
336-
raise ValueError("non-integer step for randrange()")
337-
_warn('non-integer arguments to randrange() have been deprecated '
338-
'since Python 3.10 and will be removed in a subsequent '
339-
'version',
340-
DeprecationWarning, 2)
307+
istep = _index(step)
341308
# Fast path.
342309
if istep == 1:
343310
if width > 0:
344311
return istart + self._randbelow(width)
345-
raise ValueError("empty range for randrange() (%d, %d, %d)" % (istart, istop, width))
312+
raise ValueError(f"empty range in randrange({start}, {stop})")
346313

347314
# Non-unit step argument supplied.
348315
if istep > 0:
@@ -352,7 +319,7 @@ def randrange(self, start, stop=None, step=_ONE):
352319
else:
353320
raise ValueError("zero step for randrange()")
354321
if n <= 0:
355-
raise ValueError("empty range for randrange()")
322+
raise ValueError(f"empty range in randrange({start}, {stop}, {step})")
356323
return istart + istep * self._randbelow(n)
357324

358325
def randint(self, a, b):

Lib/test/test_random.py

+37-43
Original file line numberDiff line numberDiff line change
@@ -485,50 +485,44 @@ def test_randrange_nonunit_step(self):
485485
self.assertEqual(rint, 0)
486486

487487
def test_randrange_errors(self):
488-
raises = partial(self.assertRaises, ValueError, self.gen.randrange)
488+
raises_value_error = partial(self.assertRaises, ValueError, self.gen.randrange)
489+
raises_type_error = partial(self.assertRaises, TypeError, self.gen.randrange)
490+
489491
# Empty range
490-
raises(3, 3)
491-
raises(-721)
492-
raises(0, 100, -12)
493-
# Non-integer start/stop
494-
self.assertWarns(DeprecationWarning, raises, 3.14159)
495-
self.assertWarns(DeprecationWarning, self.gen.randrange, 3.0)
496-
self.assertWarns(DeprecationWarning, self.gen.randrange, Fraction(3, 1))
497-
self.assertWarns(DeprecationWarning, raises, '3')
498-
self.assertWarns(DeprecationWarning, raises, 0, 2.71828)
499-
self.assertWarns(DeprecationWarning, self.gen.randrange, 0, 2.0)
500-
self.assertWarns(DeprecationWarning, self.gen.randrange, 0, Fraction(2, 1))
501-
self.assertWarns(DeprecationWarning, raises, 0, '2')
502-
# Zero and non-integer step
503-
raises(0, 42, 0)
504-
self.assertWarns(DeprecationWarning, raises, 0, 42, 0.0)
505-
self.assertWarns(DeprecationWarning, raises, 0, 0, 0.0)
506-
self.assertWarns(DeprecationWarning, raises, 0, 42, 3.14159)
507-
self.assertWarns(DeprecationWarning, self.gen.randrange, 0, 42, 3.0)
508-
self.assertWarns(DeprecationWarning, self.gen.randrange, 0, 42, Fraction(3, 1))
509-
self.assertWarns(DeprecationWarning, raises, 0, 42, '3')
510-
self.assertWarns(DeprecationWarning, self.gen.randrange, 0, 42, 1.0)
511-
self.assertWarns(DeprecationWarning, raises, 0, 0, 1.0)
512-
513-
def test_randrange_argument_handling(self):
514-
randrange = self.gen.randrange
515-
with self.assertWarns(DeprecationWarning):
516-
randrange(10.0, 20, 2)
517-
with self.assertWarns(DeprecationWarning):
518-
randrange(10, 20.0, 2)
519-
with self.assertWarns(DeprecationWarning):
520-
randrange(10, 20, 1.0)
521-
with self.assertWarns(DeprecationWarning):
522-
randrange(10, 20, 2.0)
523-
with self.assertWarns(DeprecationWarning):
524-
with self.assertRaises(ValueError):
525-
randrange(10.5)
526-
with self.assertWarns(DeprecationWarning):
527-
with self.assertRaises(ValueError):
528-
randrange(10, 20.5)
529-
with self.assertWarns(DeprecationWarning):
530-
with self.assertRaises(ValueError):
531-
randrange(10, 20, 1.5)
492+
raises_value_error(3, 3)
493+
raises_value_error(-721)
494+
raises_value_error(0, 100, -12)
495+
496+
# Zero step
497+
raises_value_error(0, 42, 0)
498+
raises_type_error(0, 42, 0.0)
499+
raises_type_error(0, 0, 0.0)
500+
501+
# Non-integer stop
502+
raises_type_error(3.14159)
503+
raises_type_error(3.0)
504+
raises_type_error(Fraction(3, 1))
505+
raises_type_error('3')
506+
raises_type_error(0, 2.71827)
507+
raises_type_error(0, 2.0)
508+
raises_type_error(0, Fraction(2, 1))
509+
raises_type_error(0, '2')
510+
raises_type_error(0, 2.71827, 2)
511+
512+
# Non-integer start
513+
raises_type_error(2.71827, 5)
514+
raises_type_error(2.0, 5)
515+
raises_type_error(Fraction(2, 1), 5)
516+
raises_type_error('2', 5)
517+
raises_type_error(2.71827, 5, 2)
518+
519+
# Non-integer step
520+
raises_type_error(0, 42, 3.14159)
521+
raises_type_error(0, 42, 3.0)
522+
raises_type_error(0, 42, Fraction(3, 1))
523+
raises_type_error(0, 42, '3')
524+
raises_type_error(0, 42, 1.0)
525+
raises_type_error(0, 0, 1.0)
532526

533527
def test_randrange_step(self):
534528
# bpo-42772: When stop is None, the step argument was being ignored.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Removed randrange() functionality deprecated since Python 3.10. Formerly,
2+
randrange(10.0) losslessly converted to randrange(10). Now, it raises a
3+
TypeError. Also, the exception raised for non-integral values such as
4+
randrange(10.5) or randrange('10') has been changed from ValueError to
5+
TypeError.

0 commit comments

Comments
 (0)