Skip to content

patch

patch

Private schema-aware configuration patch primitives.

Classes:

Name Description
PatchAssignment

One canonical configuration assignment with source precedence.

CompiledConfigPatch

A configuration patch compiled for one exact Pydantic model type.

PatchAssignment(path, value, origin, precedence) dataclass

One canonical configuration assignment with source precedence.

CompiledConfigPatch(target_model, assignments) dataclass

Bases: Generic[ModelT]

A configuration patch compiled for one exact Pydantic model type.

Methods:

Name Description
from_mapping

Compile a mapping with an explicit policy for traversable unknown fields.

apply_to_full_model

Apply to full current values while retaining sparse field presence.

from_mapping(target_model, source, *, origin, precedence, unknown_fields) staticmethod

Compile a mapping with an explicit policy for traversable unknown fields.

Collections are atomic patch leaves. Pydantic validates their contents when the patch is applied.

Source code in src/nemo_safe_synthesizer/config/patch.py
@staticmethod
def from_mapping(
    target_model: type[ModelT],
    source: Mapping[str, object],
    *,
    origin: str,
    precedence: int,
    unknown_fields: UnknownFieldBehavior,
) -> CompiledConfigPatch[ModelT]:
    """Compile a mapping with an explicit policy for traversable unknown fields.

    Collections are atomic patch leaves. Pydantic validates their contents
    when the patch is applied.
    """
    _require_model_type(target_model)
    if unknown_fields not in ("ignore", "reject"):
        raise ValueError(f"Unsupported unknown-field behavior {unknown_fields!r}.")
    compatible_source = _mapping_without_extras(target_model, source) if unknown_fields == "ignore" else source
    assignments = _mapping_assignments(target_model, compatible_source, origin=origin, precedence=precedence)
    return CompiledConfigPatch.from_paths(target_model, assignments)

apply_to_full_model(base)

Apply to full current values while retaining sparse field presence.

Source code in src/nemo_safe_synthesizer/config/patch.py
def apply_to_full_model(self, base: ModelT) -> ModelT:
    """Apply to full current values while retaining sparse field presence."""
    _require_exact_model(self.target_model, base)
    patch_values = self.materialize()
    values = base.model_dump()
    _merge_model_mapping(values, self.target_model, patch_values)
    result = self.target_model.model_validate(values)
    _restore_model_fields_set(result, base, patch_values)
    return result