def test_mutually_recursive_types_with_typevar(data): # The previously-failing example from the issue A = Dict[str, "B"] # noqa: F821 - an undefined name is the whole point! B = Union[List[str], A] with pytest.raises(ResolutionFailed, match=r"Could not resolve ForwardRef\('B'\)"): data.draw(st.from_type(A)) with utils.temp_registered( ForwardRef("B"), lambda _: st.deferred(lambda: b_strategy), ): b_strategy = st.from_type(B) data.draw(b_strategy) data.draw(st.from_type(A)) data.draw(st.from_type(B))
def test_mutually_recursive_types_with_typevar_alternate(data): # It's not particularly clear why this version passed when the previous # test failed, but different behaviour means we add both to the suite. C = Union[List[str], "D"] # noqa: F821 - an undefined name is the whole point! D = Dict[str, C] with pytest.raises(ResolutionFailed, match=r"Could not resolve ForwardRef\('D'\)"): data.draw(st.from_type(C)) with utils.temp_registered( ForwardRef("D"), lambda _: st.deferred(lambda: d_strategy), ): d_strategy = st.from_type(D) data.draw(d_strategy) data.draw(st.from_type(C)) data.draw(st.from_type(D))
def test_bound_type_cheking_only_forward_ref_wrong_type(): """We should check ``ForwardRef`` parameter name correctly.""" with utils.temp_registered(ForwardRef("WrongType"), st.just(1)): with pytest.raises(ResolutionFailed): st.builds(typechecking_only_fun).example()
def test_bound_type_cheking_only_forward_ref(): """We should fallback to registering explicit ``ForwardRef`` when we have to.""" with utils.temp_registered(ForwardRef("ExcInfo"), st.just(1)): st.builds(typechecking_only_fun).example()