diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2021-01-25 23:02:04 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-25 23:02:04 +0200 |
commit | f066bd94b9225a5a3c4ade5fc3ff81e3c49b7b32 (patch) | |
tree | b2d22c54e628f087d86e9dfb0330ede92eb4a260 /Lib | |
parent | bpo-42869: Avoid an HTTP redirection. (GH-24174) (diff) | |
download | cpython-f066bd94b9225a5a3c4ade5fc3ff81e3c49b7b32.tar.gz cpython-f066bd94b9225a5a3c4ade5fc3ff81e3c49b7b32.tar.bz2 cpython-f066bd94b9225a5a3c4ade5fc3ff81e3c49b7b32.zip |
bpo-37319: Improve documentation, code and tests of randrange. (GH-19112)
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/random.py | 44 | ||||
-rw-r--r-- | Lib/test/test_random.py | 19 |
2 files changed, 36 insertions, 27 deletions
diff --git a/Lib/random.py b/Lib/random.py index 30186fc7a3b..187b0a01694 100644 --- a/Lib/random.py +++ b/Lib/random.py @@ -303,17 +303,15 @@ class Random(_random.Random): try: istart = _index(start) except TypeError: - 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: + istart = int(start) + if istart != start: _warn('randrange() will raise TypeError in the future', DeprecationWarning, 2) raise ValueError("non-integer arg 1 for randrange()") - + _warn('non-integer arguments to randrange() have been deprecated ' + 'since Python 3.10 and will be removed in a subsequent ' + 'version', + DeprecationWarning, 2) if stop is None: # We don't check for "step != 1" because it hasn't been # type checked and converted to an integer yet. @@ -327,31 +325,29 @@ class Random(_random.Random): try: istop = _index(stop) except TypeError: - 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: + istop = int(stop) + if istop != stop: _warn('randrange() will raise TypeError in the future', DeprecationWarning, 2) raise ValueError("non-integer stop for randrange()") - + _warn('non-integer arguments to randrange() have been deprecated ' + 'since Python 3.10 and will be removed in a subsequent ' + 'version', + DeprecationWarning, 2) + width = istop - istart try: istep = _index(step) except TypeError: - 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: + istep = int(step) + if istep != step: _warn('randrange() will raise TypeError in the future', DeprecationWarning, 2) raise ValueError("non-integer step for randrange()") - width = istop - istart + _warn('non-integer arguments to randrange() have been deprecated ' + 'since Python 3.10 and will be removed in a subsequent ' + 'version', + DeprecationWarning, 2) + # Fast path. if istep == 1: if width > 0: return istart + self._randbelow(width) diff --git a/Lib/test/test_random.py b/Lib/test/test_random.py index 35ae4e6c3c8..66908868a6e 100644 --- a/Lib/test/test_random.py +++ b/Lib/test/test_random.py @@ -509,11 +509,24 @@ class SystemRandom_TestBasicOps(TestBasicOps, unittest.TestCase): raises(-721) raises(0, 100, -12) # Non-integer start/stop - raises(3.14159) - raises(0, 2.71828) + self.assertWarns(DeprecationWarning, raises, 3.14159) + self.assertWarns(DeprecationWarning, self.gen.randrange, 3.0) + self.assertWarns(DeprecationWarning, self.gen.randrange, Fraction(3, 1)) + self.assertWarns(DeprecationWarning, raises, '3') + self.assertWarns(DeprecationWarning, raises, 0, 2.71828) + self.assertWarns(DeprecationWarning, self.gen.randrange, 0, 2.0) + self.assertWarns(DeprecationWarning, self.gen.randrange, 0, Fraction(2, 1)) + self.assertWarns(DeprecationWarning, raises, 0, '2') # Zero and non-integer step raises(0, 42, 0) - raises(0, 42, 3.14159) + self.assertWarns(DeprecationWarning, raises, 0, 42, 0.0) + self.assertWarns(DeprecationWarning, raises, 0, 0, 0.0) + self.assertWarns(DeprecationWarning, raises, 0, 42, 3.14159) + self.assertWarns(DeprecationWarning, self.gen.randrange, 0, 42, 3.0) + self.assertWarns(DeprecationWarning, self.gen.randrange, 0, 42, Fraction(3, 1)) + self.assertWarns(DeprecationWarning, raises, 0, 42, '3') + self.assertWarns(DeprecationWarning, self.gen.randrange, 0, 42, 1.0) + self.assertWarns(DeprecationWarning, raises, 0, 0, 1.0) def test_randrange_argument_handling(self): randrange = self.gen.randrange |