Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Deprecate float arguments and the ValueError
  • Loading branch information
rhettinger committed Oct 31, 2020
commit ed2f6fdecb72405550b7e3a8ae104231c5818350
33 changes: 30 additions & 3 deletions Lib/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,16 @@ def randrange(self, start, stop=None, step=1):
try:
istart = _index(start)
except TypeError:
raise ValueError("non-integer arg 1 for randrange()")
if int(start) == start:
istart = int(start)
_warn('Float arguments to randrange() have been deprecated\n'
'since Python 3.10 and will be removed in a subsequent '
'version.',
DeprecationWarning, 2)
else:
_warn('randrange() will raise TypeError in the future',
DeprecationWarning, 2)
raise ValueError("non-integer arg 1 for randrange()")
if stop is None:
if istart > 0:
return self._randbelow(istart)
Expand All @@ -311,7 +320,16 @@ def randrange(self, start, stop=None, step=1):
try:
istop = _index(stop)
except TypeError:
raise ValueError("non-integer stop for randrange()")
if int(stop) == stop:
istop = int(stop)
_warn('Float arguments to randrange() have been deprecated\n'
'since Python 3.10 and will be removed in a subsequent '
'version.',
DeprecationWarning, 2)
else:
_warn('randrange() will raise TypeError in the future',
DeprecationWarning, 2)
raise ValueError("non-integer stop for randrange()")
width = istop - istart
if step == 1 and width > 0:
return istart + self._randbelow(width)
Expand All @@ -322,7 +340,16 @@ def randrange(self, start, stop=None, step=1):
try:
istep = _index(step)
except TypeError:
raise ValueError("non-integer step for randrange()")
if int(step) == step:
istep = int(step)
_warn('Float arguments to randrange() have been deprecated\n'
'since Python 3.10 and will be removed in a subsequent '
'version.',
DeprecationWarning, 2)
else:
_warn('randrange() will raise TypeError in the future',
DeprecationWarning, 2)
raise ValueError("non-integer step for randrange()")
if istep > 0:
n = (width + istep - 1) // istep
elif istep < 0:
Expand Down