Skip to content

target

Class info

Classes

Name Children Inherits
FormCancelledError
promptantic.form.target
The user closed the form without submitting it.
    FormTarget
    promptantic.form.target
    The schema a form is built from, and how its result is validated.
      FormValidationError
      promptantic.form.target
      The finished value doesn't validate.
        Issue
        promptantic.form.target
        One problem with a finished value, at a path into it (``()``: the whole value).

          🛈 DocStrings

          What a form fills: a JSON Schema, or a Python type (pydantic model, dataclass, ...).

          A target gives the schema the form is built from and validates the finished value: JSON Schemas with :mod:jsonschema (Draft 2020-12, formats checked), Python types with pydantic, which also turns the value into an instance.

          FormCancelledError

          Bases: Exception

          The user closed the form without submitting it.

          Source code in src/promptantic/form/target.py
          class FormCancelledError(Exception):
              """The user closed the form without submitting it."""
          

          FormTarget dataclass

          The schema a form is built from, and how its result is validated.

          Source code in src/promptantic/form/target.py
          @dataclass(frozen=True)
          class FormTarget[T]:
              """The schema a form is built from, and how its result is validated."""
          
              schema: Mapping[str, Any]
              validate_value: Callable[[Any], T]
          
              @property
              def title(self) -> str | None:
                  return self.schema.get("title")
          
              def validate(self, value: Any) -> T:
                  """The validated result (a model instance for Python types).
          
                  Raises:
                      FormValidationError: With every issue found, not only the first.
                  """
                  return self.validate_value(value)
          

          validate

          validate(value: Any) -> T
          

          The validated result (a model instance for Python types).

          Raises:

          Type Description
          FormValidationError

          With every issue found, not only the first.

          Source code in src/promptantic/form/target.py
          def validate(self, value: Any) -> T:
              """The validated result (a model instance for Python types).
          
              Raises:
                  FormValidationError: With every issue found, not only the first.
              """
              return self.validate_value(value)
          

          FormValidationError

          Bases: ValueError

          The finished value doesn't validate.

          Source code in src/promptantic/form/target.py
          class FormValidationError(ValueError):
              """The finished value doesn't validate."""
          
              def __init__(self, issues: list[Issue]) -> None:
                  self.issues = issues
                  super().__init__(
                      "; ".join(f"{'/'.join(map(str, i.path)) or '<root>'}: {i.message}" for i in issues)
                  )
          

          Issue dataclass

          One problem with a finished value, at a path into it ((): the whole value).

          Source code in src/promptantic/form/target.py
          @dataclass(frozen=True)
          class Issue:
              """One problem with a finished value, at a path into it (``()``: the whole value)."""
          
              path: Path
              message: str
          

          target_for

          target_for(target: type[T]) -> FormTarget[T]
          
          target_for(target: Mapping[str, Any]) -> FormTarget[dict[str, Any]]
          
          target_for(target: type[Any] | Mapping[str, Any]) -> FormTarget[Any]
          

          The form target for a JSON Schema (as a mapping) or a Python type.

          Source code in src/promptantic/form/target.py
          def target_for(target: type[Any] | Mapping[str, Any]) -> FormTarget[Any]:
              """The form target for a JSON Schema (as a mapping) or a Python type."""
              if isinstance(target, Mapping):
                  return _schema_target(target)
              return _type_target(target)