gitversioned.utils.pydantic¶
Pydantic helpers for GitVersioned.
Provides reusable validation and type-coercion helpers for Pydantic models. Integrates directly with the Pydantic core schema to handle robust parsing of configurations and environment variables, including string-to-list splitting and truthy/falsy string coercion.
EnsureList
¶
Bases: list[TypeVarT], Generic[TypeVarT]
A list subclass integrating directly with Pydantic Core Schema.
Preprocesses inputs (such as comma-separated strings and nested iterables) and applies inner type coercion before final schema validation.
Example
from pydantic import BaseModel class MyModel(BaseModel): ... items: EnsureList[int] model = MyModel(items="1, 2, 3") model.items [1, 2, 3]
Source code in src/gitversioned/utils/pydantic.py
__get_pydantic_core_schema__(source_type, handler)
classmethod
¶
Create a schema that hooks a pre-validator into the Pydantic pipeline.
Extracts the inner type constraint and constructs a validator that runs prior to Pydantic's core schema validation.
:param source_type: The original type annotation. :param handler: The Pydantic core schema handler. :return: The constructed Pydantic core schema.
Source code in src/gitversioned/utils/pydantic.py
coerce_bool(value)
¶
Normalize truthy/falsy strings to actual booleans.
Example
coerce_bool("yes") True coerce_bool("0") False coerce_bool(5) 5
:param value: The value to coerce. :return: The boolean equivalent if recognized, otherwise the original value.
Source code in src/gitversioned/utils/pydantic.py
coerce_list(value, item_pre_coercer=None)
¶
Recursively transform input into a list.
Splits comma-separated strings and applies an optional pre-coercer function to individual items.
Example
coerce_list("a, b, c") ['a', 'b', 'c'] coerce_list("yes, no", coerce_bool) [True, False]
:param value: The value to coerce into a list. :param item_pre_coercer: Optional function to apply to each item. :return: A list of processed items.
Source code in src/gitversioned/utils/pydantic.py
coerce_path(value)
¶
Normalize string paths to Path objects.
Example
isinstance(coerce_path("/tmp/path "), Path) True
:param value: The value to coerce into a path. :return: A Path object if the input is a string, otherwise the original value.