2

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.

David Sanders
  • 4,069
  • 1
  • 24
  • 38
  • 3
    You might want to explain the context of your question and code snippet. – Klaus D. May 14 '19 at 02:48
  • 1
    I don't know what `st.booleans().example()` is, but [being considered true is the default for most objects.](https://docs.python.org/3/library/stdtypes.html#truth-value-testing) – user2357112 May 14 '19 at 05:00
  • My first assumption was "st.booleans() holds a state and we should not recreate it each time, save it to a variable", but it is wrong, because, e.g. `[st.integers().example() for _ in range(20)]` produces different numbers. Another assumption that `[st.booleans().example() for _ in range(20)]` is similar to `[bool(st.integers().example()) for _ in range(20)]` and therefore it is almost always `True`, though I cannot neither prove nor disprove this. (these are only thoughts, I did not use `hypothesis` before) – awesoon May 14 '19 at 05:34

1 Answers1

1

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.

Zac Hatfield-Dodds
  • 2,455
  • 6
  • 19
  • Then what about `st.lists(st.booleans()).example()`? That usually returns `[]` which is the simplest possible example. – David Sanders May 20 '19 at 16:18
  • Also, is there a particular area of code in the hypothesis library that would make it clear that this is the explanation for this behavior? I tried to poke around and identify something like that previously but couldn't really find my way. – David Sanders May 20 '19 at 18:28
  • Ah, great I see it. My eyes just didn't pick that section out when I skimmed over it previously. – David Sanders May 23 '19 at 21:19