我正在尝试使用假设模块生成包含不同 python 类型作为值的字典。
对于列表,我可以简单地使用表达式来做到这一点
from hypothesis import given
import hypothesis.strategies as st
@given(
st.lists(
st.from_type(type)
.flatmap(st.from_type)
.filter(lambda x: not isinstance(x, (type(None)))),
min_size=2,
unique_by=lambda x: type(x),
)
)
def test_something(dictionary):
...
这给了我[int, str, ...]
(每个条目不同的python类型)。但是对于字典,我没有unique_by
值。
@given(
st.dictionaries(
st.text(min_size=1, max_size=10),
st.from_type(type).flatmap(st.from_type)
.filter(lambda x: not isinstance(x, (type(None), bool))),
min_size=2,
)
)
def test_something(dictionary):
...
这导致例如{'a': int, 'b': int, ...}
→所有条目的值类型相同。
有没有一种简单的方法来生成{'a': int, 'b': str, ..}
(至少有两种不同的python类型dict.values()
)?