Skip to content

sequential

Class info

🛈 DocStrings

Ask for a form one field at a time (prompt-toolkit), where a full TUI can't run.

Walks the same form tree as the Textual renderer: each field is a prompt that checks its input as it's typed (the field's own constraints), choices and union variants are numbered lists, lists and maps ask whether to add another entry, and optional objects ask whether to fill them in at all. After the last field the whole value is validated; on problems they're listed and the form is asked again, with everything entered so far as the defaults.

fill_sequential async

fill_sequential(
    target: FormTarget[Any] | type[Any] | Mapping[str, Any],
    *,
    title: str | None = None,
    initial: Mapping[str, Any] | None = None,
    session: PromptSession[str] | None = None,
    echo: Callable[[str], None] = print,
) -> Any

Ask for every field in turn and return the validated result.

Source code in src/promptantic/sequential.py
async def fill_sequential(
    target: FormTarget[Any] | type[Any] | Mapping[str, Any],
    *,
    title: str | None = None,
    initial: Mapping[str, Any] | None = None,
    session: PromptSession[str] | None = None,
    echo: Callable[[str], None] = print,
) -> Any:
    """Ask for every field in turn and return the validated result."""
    form_target = target if isinstance(target, FormTarget) else target_for(target)
    asker = _Asker(session or PromptSession(), echo)
    root = build_form(form_target.schema)
    if heading := title or form_target.title:
        echo(heading)
    value: Any = dict(initial) if initial is not None else UNSET
    while True:
        try:
            value = await asker.ask(root, "", value, required=True, depth=0)
        except EOFError, KeyboardInterrupt:  # Ctrl-D / Ctrl-C
            raise FormCancelledError from None
        try:
            return form_target.validate({} if value is UNSET else value)
        except FormValidationError as exc:
            echo("Please fix:")
            for issue in exc.issues:
                where = ".".join(map(str, issue.path)) or "value"
                echo(f"  {where}: {issue.message}")