def test_state_union_optional():
    """Test that state can be defined with Union and Optional vars."""

    class UnionState(BaseState):
        int_float: int | float = 0
        opt_int: int | None
        c3: Custom3 | None
        c3i: Custom3  # implicitly required
        c3r: Custom3 = Custom3(c2r=Custom2(c1r=Custom1(foo="")))
        custom_union: Custom1 | Custom2 | Custom3 = Custom1(foo="")

    assert str(UnionState.c3.c2) == f'{UnionState.c3!s}?.["c2"]'  # pyright: ignore [reportOptionalMemberAccess]
    assert str(UnionState.c3.c2.c1) == f'{UnionState.c3!s}?.["c2"]?.["c1"]'  # pyright: ignore [reportOptionalMemberAccess]
    assert (
        str(UnionState.c3.c2.c1.foo) == f'{UnionState.c3!s}?.["c2"]?.["c1"]?.["foo"]'  # pyright: ignore [reportOptionalMemberAccess]
    )
    assert (
        str(UnionState.c3.c2.c1r.foo) == f'{UnionState.c3!s}?.["c2"]?.["c1r"]["foo"]'  # pyright: ignore [reportOptionalMemberAccess]
    )
    assert str(UnionState.c3.c2r.c1) == f'{UnionState.c3!s}?.["c2r"]["c1"]'  # pyright: ignore [reportOptionalMemberAccess]
    assert (
        str(UnionState.c3.c2r.c1.foo) == f'{UnionState.c3!s}?.["c2r"]["c1"]?.["foo"]'  # pyright: ignore [reportOptionalMemberAccess]
    )
    assert (
        str(UnionState.c3.c2r.c1r.foo) == f'{UnionState.c3!s}?.["c2r"]["c1r"]["foo"]'  # pyright: ignore [reportOptionalMemberAccess]
    )
    assert str(UnionState.c3i.c2) == f'{UnionState.c3i!s}?.["c2"]'
    assert str(UnionState.c3r.c2) == f'{UnionState.c3r!s}["c2"]'
    assert UnionState.custom_union.foo is not None  # pyright: ignore [reportAttributeAccessIssue]
    assert UnionState.custom_union.c1 is not None  # pyright: ignore [reportAttributeAccessIssue]
    assert UnionState.custom_union.c1r is not None  # pyright: ignore [reportAttributeAccessIssue]
    assert UnionState.custom_union.c2 is not None  # pyright: ignore [reportAttributeAccessIssue]
    assert UnionState.custom_union.c2r is not None  # pyright: ignore [reportAttributeAccessIssue]
    assert types.is_optional(UnionState.opt_int._var_type)  # pyright: ignore [reportAttributeAccessIssue, reportOptionalMemberAccess]
    assert types.is_union(UnionState.int_float._var_type)  # pyright: ignore [reportAttributeAccessIssue]
Test that state can be defined with Union and Optional vars.
tests/units/test_state.py