Skip to content

liblaf.conf.converters

Expose converter helpers used by liblaf.conf fields and variables.

Most applications will reach for the higher-level field_* helpers first. This module is the lower-level escape hatch for cases where you want to pass a converter directly to Field or Var.

The exported helpers either leave values unchanged or delegate validation to Pydantic models and type adapters.

Functions:

identity

identity[T](x: T) -> T

Return a value unchanged.

Parameters:

  • x (T) –

    Value to return.

Returns:

  • T

    The same value object. This is the default converter for fields and

  • T

    variables that already receive values in their final Python form.

Source code in src/liblaf/conf/converters/_misc.py
def identity[T](x: T) -> T:
    """Return a value unchanged.

    Args:
        x: Value to return.

    Returns:
        The same value object. This is the default converter for fields and
        variables that already receive values in their final Python form.
    """
    return x

pydantic_model_validate

pydantic_model_validate[T: BaseModel](
    model: type[T],
) -> Converter[T]

Return a converter that validates Python objects against a model.

Parameters:

  • model (type[T]) –

    The Pydantic model class used to validate incoming objects.

Returns:

  • Converter[T]

    A callable suitable for Field(converter=...) or Var(...) that

  • Converter[T]

    delegates to model.model_validate.

Source code in src/liblaf/conf/converters/_pydantic.py
def pydantic_model_validate[T: pydantic.BaseModel](model: type[T]) -> Converter[T]:
    """Return a converter that validates Python objects against a model.

    Args:
        model: The Pydantic model class used to validate incoming objects.

    Returns:
        A callable suitable for `Field(converter=...)` or `Var(...)` that
        delegates to `model.model_validate`.
    """
    return model.model_validate

pydantic_model_validate_json

pydantic_model_validate_json[T: BaseModel](
    model: type[T],
) -> Converter[T]

Return a converter that validates JSON strings against a model.

Parameters:

  • model (type[T]) –

    The Pydantic model class used to parse JSON payloads.

Returns:

  • Converter[T]

    A callable that delegates to model.model_validate_json.

Source code in src/liblaf/conf/converters/_pydantic.py
def pydantic_model_validate_json[T: pydantic.BaseModel](model: type[T]) -> Converter[T]:
    """Return a converter that validates JSON strings against a model.

    Args:
        model: The Pydantic model class used to parse JSON payloads.

    Returns:
        A callable that delegates to `model.model_validate_json`.
    """
    return model.model_validate_json

pydantic_model_validate_strings

pydantic_model_validate_strings[T: BaseModel](
    model: type[T],
) -> Converter[T]

Return a converter that validates string inputs against a model.

Parameters:

  • model (type[T]) –

    The Pydantic model class used to coerce string-based inputs.

Returns:

  • Converter[T]

    A callable that delegates to model.model_validate_strings.

Source code in src/liblaf/conf/converters/_pydantic.py
def pydantic_model_validate_strings[T: pydantic.BaseModel](
    model: type[T],
) -> Converter[T]:
    """Return a converter that validates string inputs against a model.

    Args:
        model: The Pydantic model class used to coerce string-based inputs.

    Returns:
        A callable that delegates to `model.model_validate_strings`.
    """
    return model.model_validate_strings

pydantic_type_adapter_validate_json

pydantic_type_adapter_validate_json[T](
    type_: type[T],
) -> Converter[T]

Return a converter that validates JSON strings for an arbitrary type.

Parameters:

  • type_ (type[T]) –

    The target Python type validated by [pydantic.TypeAdapter][].

Returns:

  • Converter[T]

    A callable that parses JSON strings into type_ values.

Source code in src/liblaf/conf/converters/_pydantic.py
def pydantic_type_adapter_validate_json[T](type_: type[T]) -> Converter[T]:
    """Return a converter that validates JSON strings for an arbitrary type.

    Args:
        type_: The target Python type validated by
            [`pydantic.TypeAdapter`][].

    Returns:
        A callable that parses JSON strings into `type_` values.
    """
    return pydantic.TypeAdapter(type_).validate_json

pydantic_type_adapter_validate_python

pydantic_type_adapter_validate_python[T](
    type_: type[T],
) -> Converter[T]

Return a converter that validates Python objects for an arbitrary type.

Parameters:

  • type_ (type[T]) –

    The target Python type validated by [pydantic.TypeAdapter][].

Returns:

  • Converter[T]

    A callable that validates already-parsed Python objects.

Source code in src/liblaf/conf/converters/_pydantic.py
def pydantic_type_adapter_validate_python[T](type_: type[T]) -> Converter[T]:
    """Return a converter that validates Python objects for an arbitrary type.

    Args:
        type_: The target Python type validated by
            [`pydantic.TypeAdapter`][].

    Returns:
        A callable that validates already-parsed Python objects.
    """
    return pydantic.TypeAdapter(type_).validate_python

pydantic_type_adapter_validate_strings

pydantic_type_adapter_validate_strings[T](
    type_: type[T],
) -> Converter[T]

Return a converter that validates string inputs for an arbitrary type.

Parameters:

  • type_ (type[T]) –

    The target Python type validated by [pydantic.TypeAdapter][].

Returns:

  • Converter[T]

    A callable that coerces string inputs into type_ values.

Source code in src/liblaf/conf/converters/_pydantic.py
def pydantic_type_adapter_validate_strings[T](type_: type[T]) -> Converter[T]:
    """Return a converter that validates string inputs for an arbitrary type.

    Args:
        type_: The target Python type validated by
            [`pydantic.TypeAdapter`][].

    Returns:
        A callable that coerces string inputs into `type_` values.
    """
    return pydantic.TypeAdapter(type_).validate_strings