使用假设在 fixed_dictionaries 中生成两个等长列表

Generate two lists of equal length in fixed_dictionaries using Hypothesis

我正在尝试使用 fixed_dictionaries 策略生成样本数据,其中两个键的值列表必须等长,例如:

{'ids': [1, 2, 3],
 'words': ['foo', 'bar', 'baz']}

如何强制执行此约束?我想我可以将一个定义为另一个的复合,但我不确定该怎么做。类似于:

import hypothesis.strategies as st

ids = st.lists(elements=st.integers())

@st.composite
def words(draw, elements=st.text()):
    draw(sample_ids) # ???

这是解决此问题的一种方法:

@composite
def my_dicts(draw):
    size = draw(st.integers(min_value=0))
    ids = st.lists(min_size=size, max_size=size)
    words = st.lists(min_size=size, max_size=size)
    dicts = st.fixed_dictionaries({'ids': ids, 'words': words})

    return draw(st.lists(dicts))