代码之家  ›  专栏  ›  技术社区  ›  The Unfun Cat

如何在假设中使用复合策略(hypothesis.errors.invalidargument:expected searchstrategy but got function)

  •  1
  • The Unfun Cat  · 技术社区  · 6 年前

    这个例子是 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
    

    失败了:

    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
    

    我做错什么了?

    1 回复  |  直到 6 年前
        1
  •  0
  •   The Unfun Cat    6 年前

    您需要调用复合函数。这在文档中没有解释,但是在 2016 blog post .

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