8000 bpo-37319: Improve documentation, code and tests of randrange. (GH-19… · python/cpython@f066bd9 · GitHub
[go: up one dir, main page]

Skip to content

Commit f066bd9

Browse files
bpo-37319: Improve documentation, code and tests of randrange. (GH-19112)
1 parent eb9983c commit f066bd9

File tree

5 files changed

+48
-34
lines changed

5 files changed

+48
-34
lines changed

Doc/library/random.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,9 @@ Functions for integers
141141
``randrange(10)``. In the future, this will raise a :exc:`TypeError`.
142142

143143
.. deprecated:: 3.10
144-
The exception raised for non-integral values such as ``range(10.5)``
145-
will be changed from :exc:`ValueError` to :exc:`TypeError`.
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`.
146147

147148
.. function:: randint(a, b)
148149

Doc/whatsnew/3.10.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,10 @@ Deprecated
530530
as appropriate to help identify code which needs updating during
531531
this transition.
532532
533+
* Non-integer arguments to :func:`random.randrange` are deprecated.
534+
The :exc:`ValueError` is deprecated in favor of a :exc:`TypeError`.
535+
(Contributed by Serhiy Storchaka and Raymond Hettinger in :issue:`37319`.)
536+
533537
* The various ``load_module()`` methods of :mod:`importlib` have been
534538
documented as deprecated since Python 3.6, but will now also trigger
535539
a :exc:`DeprecationWarning`. Use

Lib/random.py

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -303,17 +303,15 @@ def randrange(self, start, stop=None, step=_ONE):
303303
try:
304304
istart = _index(start)
305305
except TypeError:
306-
if int(start) == start:
307-
istart = int(start)
308-
_warn('Float arguments to randrange() have been deprecated\n'
309-
'since Python 3.10 and will be removed in a subsequent '
310-
'version.',
311-
DeprecationWarning, 2)
312-
else:
306+
istart = int(start)
307+
if istart != start:
313308
_warn('randrange() will raise TypeError in the future',
314309
DeprecationWarning, 2)
315310
raise ValueError("non-integer arg 1 for randrange()")
316-
311+
_warn('non-integer arguments to randrange() have been deprecated '
312+
'since Python 3.10 and will be removed in a subsequent '
313+
'version',
314+
DeprecationWarning, 2)
317315
if stop is None:
318316
# We don't check for "step != 1" because it hasn't been
319317
# type checked and converted to an integer yet.
@@ -327,31 +325,29 @@ def randrange(self, start, stop=None, step=_ONE):
327325
try:
328326
istop = _index(stop)
329327
except TypeError:
330-
if int(stop) == stop:
331-
istop = int(stop)
332-
_warn('Float arguments to randrange() have been deprecated\n'
333-
'since Python 3.10 and will be removed in a subsequent '
334-
'version.',
335-
DeprecationWarning, 2)
336-
else:
328+
istop = int(stop)
329+
if istop != stop:
337330
_warn('randrange() will raise TypeError in the future',
338331
DeprecationWarning, 2)
339332
raise ValueError("non-integer stop for randrange()")
340-
333+
_warn('non-integer arguments to randrange() have been deprecated '
334+
'since Python 3.10 and will be removed in a subsequent '
335+
'version',
336+
DeprecationWarning, 2)
337+
width = istop - istart
341338
try:
342339
istep = _index(step)
343340
except TypeError:
344-
if int(step) == step:
345-
istep = int(step)
346-
_warn('Float arguments to randrange() have been deprecated\n'
347-
'since Python 3.10 and will be removed in a subsequent '
348-
'version.',
349-
DeprecationWarning, 2)
350-
else:
341+
istep = int(step)
342+
if istep != step:
351343
_warn('randrange() will raise TypeError in the future',
352344
DeprecationWarning, 2)
353345
raise ValueError("non-integer step for randrange()")
354-
width = istop - istart
346+
_warn('non-integer arguments to randrange() have been deprecated '
347+
'since Python 3.10 and will be removed in a subsequent '
348+
'version',
349+
DeprecationWarning, 2)
350+
# Fast path.
355351
if istep == 1:
356352
if width > 0:
357353
return istart + self._randbelow(width)

Lib/test/test_random.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -509,11 +509,24 @@ def test_randrange_errors(self):
509509
raises(-721)
510510
raises(0, 100, -12)
511511
# Non-integer start/stop
512-
raises(3.14159)
513-
raises(0, 2.71828)
512+
self.assertWarns(DeprecationWarning, raises, 3.14159)
513+
self.assertWarns(DeprecationWarning, self.gen.randrange, 3.0)
514+
self.assertWarns(DeprecationWarning, self.gen.randrange, Fraction(3, 1))
515+
self.assertWarns(DeprecationWarning, raises, '3')
516+
self.assertWarns(DeprecationWarning, raises, 0, 2.71828)
517+
self.assertWarns(DeprecationWarning, self.gen.randrange, 0, 2.0)
518+
self.assertWarns(DeprecationWarning, self.gen.randrange, 0, Fraction(2, 1))
519+
self.assertWarns(DeprecationWarning, raises, 0, '2')
514520
# Zero and non-integer step
515521
raises(0, 42, 0)
516-
raises(0, 42, 3.14159)
522+
self.assertWarns(DeprecationWarning, raises, 0, 42, 0.0)
523+
self.assertWarns(DeprecationWarning, raises, 0, 0, 0.0)
524+
self.assertWarns(DeprecationWarning, raises, 0, 42, 3.14159)
525+
self.assertWarns(DeprecationWarning, self.gen.randrange, 0, 42, 3.0)
526+
self.assertWarns(DeprecationWarning, self.gen.randrange, 0, 42, Fraction(3, 1))
527+
self.assertWarns(DeprecationWarning, raises, 0, 42, '3')
528+
self.assertWarns(DeprecationWarning, self.gen.randrange, 0, 42, 1.0)
529+
self.assertWarns(DeprecationWarning, raises, 0, 0, 1.0)
517530

518531
def test_randrange_argument_handling(self):
519532
randrange = self.gen.randrange

Misc/NEWS.d/3.10.0a4.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -605,12 +605,12 @@ deprecated in Python 3.7. Patch by Erlend E. Aasland
605605
.. nonce: Cfl1eR
606606
.. section: Library
607607
608-
Harmonized random.randrange() argument handling to match range().
608+
Harmonized :func:`random.randrange` argument handling to match :func:`range`.
609609

610-
* The integer test and conversion in randrange() now uses
611-
operator.index().
612-
* Non-integer arguments to randrange() are deprecated.
613-
* The *ValueError* is deprecated in favor of a *TypeError*.
610+
* The integer test and conversion in ``randrange()`` now uses
611+
:func:`operator.index`.
612+
* Non-integer arguments to ``randrange()`` are deprecated.
613+
* The ``ValueError`` is deprecated in favor of a ``TypeError``.
614614
* It now runs a little faster than before.
615615

616616
(Contributed by Raymond Hettinger and Serhiy Storchaka.)

0 commit comments

Comments
 (0)
0