Skip to content

Commit 5afa0a4

Browse files
authored
bpo-42222: Remove deprecated support for non-integer values (GH-28983)
1 parent 15ad52f commit 5afa0a4

File tree

4 files changed

+54
-87
lines changed

4 files changed

+54
-87
lines changed

Doc/library/random.rst

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -135,15 +135,10 @@ Functions for integers
135135
values. Formerly it used a style like ``int(random()*n)`` which could produce
136136
slightly uneven distributions.
137137

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`.
138+
.. versionchanged:: 3.11
139+
Automatic conversion of non-integer types is no longer supported.
140+
Calls such as ``randrange(10.0)`` and ``randrange(Fraction(10, 1))``
141+
now raise a :exc:`TypeError`.
147142

148143
.. function:: randint(a, b)
149144

Lib/random.py

Lines changed: 10 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -286,27 +286,17 @@ def randbytes(self, n):
286286
## -------------------- integer methods -------------------
287287

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

296297
# This code is a bit messy to make it fast for the
297298
# common case while still doing adequate error checking.
298-
try:
299-
istart = _index(start)
300-
except TypeError:
301-
istart = int(start)
302-
if istart != start:
303-
_warn('randrange() will raise TypeError in the future',
304-
DeprecationWarning, 2)
305-
raise ValueError("non-integer arg 1 for randrange()")
306-
_warn('non-integer arguments to randrange() have been deprecated '
307-
'since Python 3.10 and will be removed in a subsequent '
308-
'version',
309-
DeprecationWarning, 2)
299+
istart = _index(start)
310300
if stop is None:
311301
# We don't check for "step != 1" because it hasn't been
312302
# type checked and converted to an integer yet.
@@ -316,37 +306,15 @@ def randrange(self, start, stop=None, step=_ONE):
316306
return self._randbelow(istart)
317307
raise ValueError("empty range for randrange()")
318308

319-
# stop argument supplied.
320-
try:
321-
istop = _index(stop)
322-
except TypeError:
323-
istop = int(stop)
324-
if istop != stop:
325-
_warn('randrange() will raise TypeError in the future',
326-
DeprecationWarning, 2)
327-
raise ValueError("non-integer stop for randrange()")
328-
_warn('non-integer arguments to randrange() have been deprecated '
329-
'since Python 3.10 and will be removed in a subsequent '
330-
'version',
331-
DeprecationWarning, 2)
309+
# Stop argument supplied.
310+
istop = _index(stop)
332311
width = istop - istart
333-
try:
334-
istep = _index(step)
335-
except TypeError:
336-
istep = int(step)
337-
if istep != step:
338-
_warn('randrange() will raise TypeError in the future',
339-
DeprecationWarning, 2)
340-
raise ValueError("non-integer step for randrange()")
341-
_warn('non-integer arguments to randrange() have been deprecated '
342-
'since Python 3.10 and will be removed in a subsequent '
343-
'version',
344-
DeprecationWarning, 2)
312+
istep = _index(step)
345313
# Fast path.
346314
if istep == 1:
347315
if width > 0:
348316
return istart + self._randbelow(width)
349-
raise ValueError("empty range for randrange() (%d, %d, %d)" % (istart, istop, width))
317+
raise ValueError(f"empty range in randrange({start}, {stop}, {step})")
350318

351319
# Non-unit step argument supplied.
352320
if istep > 0:
@@ -356,7 +324,7 @@ def randrange(self, start, stop=None, step=_ONE):
356324
else:
357325
raise ValueError("zero step for randrange()")
358326
if n <= 0:
359-
raise ValueError("empty range for randrange()")
327+
raise ValueError(f"empty range in randrange({start}, {stop}, {step})")
360328
return istart + istep * self._randbelow(n)
361329

362330
def randint(self, a, b):

Lib/test/test_random.py

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -481,50 +481,53 @@ def test_randrange_nonunit_step(self):
481481
self.assertEqual(rint, 0)
482482

483483
def test_randrange_errors(self):
484-
raises = partial(self.assertRaises, ValueError, self.gen.randrange)
484+
raises_value_error = partial(self.assertRaises, ValueError, self.gen.randrange)
485+
raises_type_error = partial(self.assertRaises, TypeError, self.gen.randrange)
486+
485487
# Empty range
486-
raises(3, 3)
487-
raises(-721)
488-
raises(0, 100, -12)
489-
# Non-integer start/stop
490-
self.assertWarns(DeprecationWarning, raises, 3.14159)
491-
self.assertWarns(DeprecationWarning, self.gen.randrange, 3.0)
492-
self.assertWarns(DeprecationWarning, self.gen.randrange, Fraction(3, 1))
493-
self.assertWarns(DeprecationWarning, raises, '3')
494-
self.assertWarns(DeprecationWarning, raises, 0, 2.71828)
495-
self.assertWarns(DeprecationWarning, self.gen.randrange, 0, 2.0)
496-
self.assertWarns(DeprecationWarning, self.gen.randrange, 0, Fraction(2, 1))
497-
self.assertWarns(DeprecationWarning, raises, 0, '2')
498-
# Zero and non-integer step
499-
raises(0, 42, 0)
500-
self.assertWarns(DeprecationWarning, raises, 0, 42, 0.0)
501-
self.assertWarns(DeprecationWarning, raises, 0, 0, 0.0)
502-
self.assertWarns(DeprecationWarning, raises, 0, 42, 3.14159)
503-
self.assertWarns(DeprecationWarning, self.gen.randrange, 0, 42, 3.0)
504-
self.assertWarns(DeprecationWarning, self.gen.randrange, 0, 42, Fraction(3, 1))
505-
self.assertWarns(DeprecationWarning, raises, 0, 42, '3')
506-
self.assertWarns(DeprecationWarning, self.gen.randrange, 0, 42, 1.0)
507-
self.assertWarns(DeprecationWarning, raises, 0, 0, 1.0)
488+
raises_value_error(3, 3)
489+
raises_value_error(-721)
490+
raises_value_error(0, 100, -12)
491+
492+
# Zero step
493+
raises_value_error(0, 42, 0)
494+
495+
# Non-integer start/stop/step
496+
raises_type_error(3.14159)
497+
raises_type_error(3.0)
498+
raises_type_error(Fraction(3, 1))
499+
raises_type_error('3')
500+
raises_type_error(0, 2.71827)
501+
raises_type_error(0, 2.0)
502+
raises_type_error(0, Fraction(2, 1))
503+
raises_type_error(0, '2')
504+
505+
# Non-integer step
506+
raises_type_error(0, 42, 1.0)
507+
raises_type_error(0, 0, 1.0)
508+
raises_type_error(0, 42, 3.14159)
509+
raises_type_error(0, 42, 3.0)
510+
raises_type_error(0, 42, Fraction(3, 1))
511+
raises_type_error(0, 42, '3')
512+
raises_type_error(0, 42, 1.0)
513+
raises_type_error(0, 0, 1.0)
508514

509515
def test_randrange_argument_handling(self):
510516
randrange = self.gen.randrange
511-
with self.assertWarns(DeprecationWarning):
517+
with self.assertRaises(TypeError):
512518
randrange(10.0, 20, 2)
513-
with self.assertWarns(DeprecationWarning):
519+
with self.assertRaises(TypeError):
514520
randrange(10, 20.0, 2)
515-
with self.assertWarns(DeprecationWarning):
521+
with self.assertRaises(TypeError):
516522
randrange(10, 20, 1.0)
517-
with self.assertWarns(DeprecationWarning):
523+
with self.assertRaises(TypeError):
518524
randrange(10, 20, 2.0)
519-
with self.assertWarns(DeprecationWarning):
520-
with self.assertRaises(ValueError):
521-
randrange(10.5)
522-
with self.assertWarns(DeprecationWarning):
523-
with self.assertRaises(ValueError):
524-
randrange(10, 20.5)
525-
with self.assertWarns(DeprecationWarning):
526-
with self.assertRaises(ValueError):
527-
randrange(10, 20, 1.5)
525+
with self.assertRaises(TypeError):
526+
randrange(10.5)
527+
with self.assertRaises(TypeError):
528+
randrange(10, 20.5)
529+
with self.assertRaises(TypeError):
530+
randrange(10, 20, 1.5)
528531

529532
def test_randrange_step(self):
530533
# bpo-42772: When stop is None, the step argument was being ignored.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Removed deprecated support for float arguments in *randrange()*.

0 commit comments

Comments
 (0)