Skip to content

Models

Requires the pydantic extra.

rn_forge.commons.lang.models

Strict pydantic models for externally authored documents.

Provides:

  • :class:StrictModel — a BaseModel that is strict, frozen and forbids unknown keys, with :meth:StrictModel.parse as its one entry point.
  • :func:parse_model — the same parse for any BaseModel subclass.
  • :class:ModelValidationError — every failure at once, each named by its dotted path.
  • :class:FieldError — one of those failures.

Requires the pydantic extra. Not imported by rn_forge.commons's curated __init__.py; import this module directly.

Example::

from rn_forge.commons.lang.models import StrictModel

class Repository(StrictModel):
    name: str
    archetype: str = "python-tool"

class ProjectConfig(StrictModel):
    repository: Repository

ProjectConfig.parse(
    {"repository": {"archtype": 1}}, source="config.toml"
)
# ModelValidationError: config.toml: 2 invalid key(s)
#   repository.name: Field required
#   repository.archtype: Extra inputs are not permitted

FieldError(path, message, kind) dataclass

One validation failure.

Parameters:

Name Type Description Default
path str

The dotted path of the offending key (repository.name, or packages.0 for a list element). Empty for the document itself.

required
message str

pydantic's description of the failure.

required
kind str

pydantic's stable error type (missing, extra_forbidden, string_type, …).

required

ModelValidationError(model, errors, source=None)

Bases: AppException

A document failed validation; :attr:errors lists every failure.

Parameters:

Name Type Description Default
model type[BaseModel]

The model the document was validated against.

required
errors tuple[FieldError, ...]

Every failure, in pydantic's reporting order.

required
source str | None

Where the document came from (a path, say), for the message.

None

check(value, message, *message_args, **kwargs) classmethod

Raise this exception if value is falsy.

Parameters:

Name Type Description Default
value Any

The value to test.

required
message str

Exception message (supports positional format args).

required
*message_args Any

Forwarded to message.format().

()
**kwargs Any

Forwarded as error_code / error_data to the constructor.

{}

Example::

AppException.check(path.exists(), "Missing file: {}", path, error_code=404)

StrictModel

Bases: BaseModel

A pydantic model for configuration documents.

Strict (no "1" → 1 coercion), frozen, and every key the model does not declare is an error. Subclasses may widen :attr:model_config.

parse(data, *, source=None) classmethod

Validate data against this model; see :func:parse_model.

Raises:

Type Description
ModelValidationError

data does not satisfy this model.

parse_model(model, data, *, source=None)

Validate data against model, reporting every failure at once.

Parameters:

Name Type Description Default
model type[M]

Any pydantic model class.

required
data Mapping[str, Any]

The parsed document.

required
source str | None

Where data came from, named in the error message.

None

Returns:

Type Description
M

The validated model instance.

Raises:

Type Description
ModelValidationError

data does not satisfy model.