0

If Python hypothesis strategies are too deeply nested, using draw will not create an actual example, but a LazyStrategy. This can be quite problematic at times because the resulting object behaves very differently from an actual example.

Is there a way to enforce eager evaluations of strategies, such that calling draw always returns an actual example of the corresponding strategy?

For example:

from hypothesis import strategies as st

@st.composite
def my_composite_strategy(draw):
    some_example = draw(some_very_complex_deeply_nested_strategy)
    print(type(some_example))
    ...

Will print <class 'hypothesis.strategies._internal.lazy.LazyStrategy'> if executed. What I want instead is <class 'my_module.MyObjectIWriteAStrategyFor'>, so I can use some_example really as if it was a real object.

SmCaterpillar
  • 6,683
  • 7
  • 42
  • 70
  • Ah maybe the reason is that somehwere int he `some_very_complex_deeply_nested_strategy` in a `composite` instead of an example an `st:builds` is returned – SmCaterpillar Jan 29 '22 at 12:39

1 Answers1

1

As per https://github.com/HypothesisWorks/hypothesis/issues/3224, your some_very_complex_deeply_nested_strategy is returning a strategy rather than the object you want

Zac Hatfield-Dodds
  • 2,455
  • 6
  • 19