booleans().example() 总是 returns True
booleans().example() always returns True
重现:
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
为什么 st.booleans().example()
总是 return True
?我的理解是 example
方法应该 return 可以通过策略输出的示例,并且在某种程度上是随机的。
相关地,st.sampled_from(...)
似乎永远不会 return 可迭代对象中的第一项:
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)]
这是怎么回事?
我知道 example
方法文档说的是方法 "shouldn't be taken too seriously"(请参阅 here)。但这提供的解释方式很少,如果能更深入地了解发生这种情况的原因会很好。
简单:.example()
方法避免显示策略中可能最简单的示例,因为该示例通常是微不足道的。诚然,这对 st.booleans().example()
没那么有用,但这就是原因!
对于您的评论,lists(...).example()
由于我们的唯一性检测的局限性而不断生成空列表 - 有关更多详细信息,请参阅问题 1864、1982 和 PR 1961;一旦我们有好的方法,我们会尽快修复它。
重现:
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
为什么 st.booleans().example()
总是 return True
?我的理解是 example
方法应该 return 可以通过策略输出的示例,并且在某种程度上是随机的。
相关地,st.sampled_from(...)
似乎永远不会 return 可迭代对象中的第一项:
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)]
这是怎么回事?
我知道 example
方法文档说的是方法 "shouldn't be taken too seriously"(请参阅 here)。但这提供的解释方式很少,如果能更深入地了解发生这种情况的原因会很好。
简单:.example()
方法避免显示策略中可能最简单的示例,因为该示例通常是微不足道的。诚然,这对 st.booleans().example()
没那么有用,但这就是原因!
对于您的评论,lists(...).example()
由于我们的唯一性检测的局限性而不断生成空列表 - 有关更多详细信息,请参阅问题 1864、1982 和 PR 1961;一旦我们有好的方法,我们会尽快修复它。