Wednesday, 22 May 2019

booleans().example() always returns True

To reproduce:

In [1]: from hypothesis import strategies as st

In [2]: bool_st = st.booleans()

In [3]: all(bool_st.example() for _ in range(1000))
Out[3]: True

Why does st.booleans().example() always return True? My understanding is that the example method should return examples of what could be output by a strategy and does so somewhat at random.

Relatedly, st.sampled_from(...) seems to never return the first item in the iterable:

In [1]: from hypothesis import strategies as st

In [2]: from collections import Counter

In [3]: samp_st = st.sampled_from(list(range(10)))

In [4]: examples = [samp_st.example() for _ in range(1000)]

In [5]: cnt = Counter(examples)

In [6]: cnt.most_common()
Out[6]: [(1, 512), (2, 282), (3, 119), (4, 55), (5, 22), (6, 5), (7, 4), (8, 1)]

So what's going on here?

I'm aware that the example method documentation says the method "shouldn't be taken too seriously" (see here). But this offers very little in the way of explanation and it would be nice to get more insight into why this is happening.



from booleans().example() always returns True

No comments:

Post a Comment