Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hypothesis's fake Random disagrees with true Random when sampling empty sequences #3765

Closed
DRMacIver opened this issue Oct 10, 2023 · 0 comments
Labels
bug something is clearly wrong here

Comments

@DRMacIver
Copy link
Member

The following code doesn't work correctly when using Hypothesis's fake random number generator:

from hypothesis import given, strategies as st

@given(rnd=st.randoms(use_true_random=False))
def test_sample_from_empty(rnd):
    assert rnd.sample([], 0) == []

It fails with:

E           hypothesis.errors.InvalidArgument: Cannot sample from a length-zero sequence.
E           Falsifying example: test_sample_from_empty(
E               rnd=HypothesisRandom(generated data),
E           )

But the following test passes:

from random import Random

def test_sample_from_empty():
    rnd = Random()
    assert rnd.sample([], 0) == []

The issue is that although you can't sample a non-zero number of elements from an empty sequence, it's perfectly valid to sample 0 elements from an empty sequence, and the normal Random class supports this fine but our emulation of it does not.

@Zac-HD Zac-HD added the bug something is clearly wrong here label Oct 10, 2023
@Zac-HD Zac-HD closed this as completed in 8a379f3 Oct 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug something is clearly wrong here
Projects
None yet
Development

No branches or pull requests

2 participants