booleans().example() always returns True

David Sanders

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.

Zac Hatfield-Dodds

Simple: the .example() method avoids showing the simplest possible example from a strategy, because that example is usually trivial. Admittedly this is less useful for st.booleans().example(), but that's why!

For your comments, lists(...).example() keeps producing the empty list due to a limitation in our uniqueness detection - see issues 1864, 1982, and PR 1961 for more details; we'll fix it as soon as we have a good way to do so.

You can find the code for .example() here.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

TOP Ranking

HotTag

Archive