Skip to content

Support recursive forward references in st.from_type #4542

@Liam-DeVoe

Description

@Liam-DeVoe

It would be nice to support the following:

from hypothesis import strategies as st
from typing import Union

A = list[Union["A", str]]
# currently: hypothesis.errors.ResolutionFailed: Could not resolve ForwardRef('A') to a strategy
st.from_type(A).example()

Conceptually, this is equivalent to a = st.deferred(lambda: st.lists(a | st.text())).

Considerations:

  • redundant (but valid) self-references, like A = A | str, which should resolve to str. We may need to normalize cycles beforehand.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementit's not broken, but we want it to be better

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions