7

This example is a variation of the one in the docs:

import hypothesis.strategies as st

from hypothesis import given

@st.composite
def s(draw):
    x = draw(st.text(), min_size=1)
    y = draw(st.text(alphabet=x))
    return (x, y)



@given(s1=s, s2=s)
def test_subtraction(s1, s2):

    print(s1, s2)

    assert 0

It fails:

E hypothesis.errors.InvalidArgument: Expected SearchStrategy but got <function accept.<locals>.s at 0x7fd7e5c05620> (type=function)

/mnt/work/unfuncat/software/anaconda/lib/python3.6/site-packages/hypothesis/internal/validation.py:40: InvalidArgument

What am I doing wrong?

The Unfun Cat
  • 29,987
  • 31
  • 114
  • 156

1 Answers1

9

You need to call the composite functions. This is not explained in the docs, but there is an example in a 2016 blog post.

@given(s1=s(), s2=s()) # <===== change
def test_subtraction(s1, s2):

    print(s1, s2)

    assert 0
The Unfun Cat
  • 29,987
  • 31
  • 114
  • 156
  • 3
    There's also [an example in the docs](https://hypothesis.readthedocs.io/en/latest/data.html#composite-strategies) (`list_and_index`), but I agree this should be explicitly stated in the text. – Zac Hatfield-Dodds Jun 16 '18 at 18:44
  • It is shown how to produce one example in the REPL. It is not clear to me that means that you have to use function calls in the given statements. Regular strategies are used without function calls. I think it would be helpful to write that composites are different :) – The Unfun Cat Jun 17 '18 at 06:58