Repository navigation
Modernize integer test/conversion in randrange() #86388
Description
Activity
Move the int(x)==x test and conversion into the C code for operator.index().
- added3.10 (EOL)end of lifeend of lifestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
on Oct 31, 2020 And, if we were willing to correct the exception type from ValueError to TypeError, the code could be made simpler, faster, and more in line with user expectations.
I had forgotten. It looks like float arguments were allowed:
>>> randrange(10.0, 20.0, 2.0) 16Is this worth going through a deprecation cycle to get the code cleaned-up or should we live with it as is?
It changes the behavior. Currently randrange(10.0) works, but with PR 23064 it would fail.
See bpo-40046 with a ready PR for increasing coverage for the random module. If it would accepted, some tests would fail with PR 23064.
If you want to deprecate accepting float arguments, there was bpo-40046 with a ready PR.
These propositions were rejected by you. Have you reconsidered your decision?
These propositions were rejected by you.
Have you reconsidered your decision?I was reluctant to break any existing code.
Now, I'm unsure and am inclined to harmonize it with range().What do you think?
Should we have ever supported float arguments
for an integer domain function?I think and always thought that integer domain functions should not accept non-integer arguments even with integer value. This is why I submitted numerous patches for deprecating and finally removing support of non-integer arguments in most of integer domain functions. C implemented functions which use PyArg_Parse("i") or PyLong_AsLong() for parsing arguments use now index() instead of int(). They emit a deprecation warning for non-integers in 3.8 and 3.9 and raise type error since 3.10. math.factorial() emits a warning only in 3.9.
bpo-37319 (sorry, I wrote incorrect issue number in msg380085) was initially opened for 3.9, so we could convert warnings into errors in 3.10 or 3.11.
Currently randrange(1e25) can return value larger than 10**25, because int(1e25) == 10000000000000000905969664 > 10**25.
User feedback concur with making the change: https://twitter.com/raymondh/status/1322607969754775552
Yes, the ability to write randrange(1e9) is sometimes nice. And the fact that it might give the number outside the intended range with probability 1e-17 is not really an important argument (people have bad intuitions about very small probabilities). But if we intend to be consistent with range, then of course this must go.
10**9 isn't much harder than 10E9 ;-)
To me, ValueError("non-integer arg 1 for randrange()") (ValueError('bad type') is a bit painful to read. We do sometime fix such bugs, when not documented, in future releases.
Current the doc, "Return a randomly selected element from range(start, stop, step). This is equivalent to choice(range(start, stop, step))", implies that both accept the same values, which most would expect anyway from the names. Being selectively 'generous' in what is accepted is confusing.
For the future: both range and math.factorial raise
TypeError: 'float' object cannot be interpreted as an integer
The consistency is nice. randrange should say the same after deprecation.To put what I said another way: both items are mental paper cuts and I see benefit to both coredevs and users in getting rid of them. That is not to say 'no cost', but that there is a real benefit to be balanced against the real cost.
There is another randrange() oddity. If stop is None, the step argument is ignored:
>>> randrange(100, stop=None, step=10) 4If we want to fully harmonize with range(), then randrange() should only accept positional arguments and should not allow None for the stop argument. That would leave the unoptimized implementation equivalent to:
def randrange(self, /, *args): return self.choice(range(*args))
The actual implementation can retain its fast paths and have a nicer looking signature perhaps using __text_signature__.
- added3.11only security fixesonly security fixesand removed3.10 (EOL)end of lifeend of life
on Oct 19, 2021 Since this is a user-visible change in 3.11, could you add a What's New entry?
- added a commit that references this issue
on May 12, 2022
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields: