Repository navigation
Closed
Description
Bug report
Bug description:
I just encountered the situation where I used random.sample but both the population and counts arguments were empty (my algorithm had nothing left to choose from). So, basically this situation:
>>> random.sample([], 1, counts=[])
Traceback (most recent call last):
File "<python-input-1>", line 1, in <module>
random.sample([], 1, counts=[])
~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^
File "/path/to/lib/python3.14/random.py", line 424, in sample
total = cum_counts.pop()
IndexError: pop from empty listInstead of the IndexError, I expected a ValueError, similar to the following situations:
>>> random.sample([], 1)
Traceback (most recent call last):
File "<python-input-2>", line 1, in <module>
random.sample([], 1)
~~~~~~~~~~~~~^^^^^^^
File "/path/to/lib/python3.14/random.py", line 434, in sample
raise ValueError("Sample larger than population or is negative")
ValueError: Sample larger than population or is negative
>>>
>>> random.sample([1], 2, counts=[1])
Traceback (most recent call last):
File "<python-input-3>", line 1, in <module>
random.sample([1], 2, counts=[1])
~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^
File "/path/to/lib/python3.14/random.py", line 429, in sample
selections = self.sample(range(total), k=k)
File "/path/to/lib/python3.14/random.py", line 434, in sample
raise ValueError("Sample larger than population or is negative")
ValueError: Sample larger than population or is negativeThe docs mention that
If the sample size is larger than the population size, a ValueError is raised.
In addition, I would expect the following to work:
>>> random.sample([], 0, counts=[])
Traceback (most recent call last):
File "<python-input-4>", line 1, in <module>
random.sample([], 0, counts=[])
~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^
File "/path/to/lib/python3.14/random.py", line 424, in sample
total = cum_counts.pop()
IndexError: pop from empty listsimilar to how it works when counts is not specified:
>>> random.sample([], 0)
[]Not sure though what CPython's backwards-compatibility policy has to say here, since changing the exception type – or, in the second case, removing the exception altogether – might actually break someone's code...
Tested with:
Python 3.14.0a5 (main, Feb 12 2025, 14:51:40) [Clang 19.1.6 ] on linux
cpython-3.14.0a5-linux-x86_64-gnu
CPython versions tested on:
3.14
Operating systems tested on:
Linux
Activity
rhettinger commentedon Feb 18, 2025
Neither of these cases was tested or intended behavior, so it would be reasonable to fix them both. I'll work on a PR soonish. Thanks for the report.
Because of the possibility of breaking code, I'm -0 on backporting the edit.
Dominik1123 commentedon Feb 18, 2025
Somewhat related is the situation when
k=0and allcountsare zero:According to the docs:
So, extrapolating this to zero counts, this would be
random.sample('abc', k=0, counts=[0,0,0])is equivalent torandom.sample([], k=0)which, however, returns[].StanFromIreland commentedon Feb 18, 2025
I propose we do
cpython/Lib/random.py
Line 421 in 8207454
cpython/Lib/random.py
Line 422 in 8207454
And add tests, what do you think @rhettinger ? I see you are planning to do this
Dominik1123 commentedon Feb 18, 2025
What about (random.py#L424)
That would also address the
random.sample('abc', k=0, counts=[0,0,0]) == random.sample([], k=0)case.If
total == 0 and k > 0this also wouldn't change the type of exception as it would becomeValueError("Sample larger than population or is negative")instead.pythongh-130285: Support zero counts in random.sample()
gh-130285: Fix handling of zero or empty counts in random.sample() (g…
pythongh-130285: Fix handling of zero or empty counts in random.sampl…
pythongh-130285: Fix handling of zero or empty counts in random.sampl…
[3.12] gh-130285: Fix handling of zero or empty counts in random.samp…
[3.13] gh-130285: Fix handling of zero or empty counts in random.samp…