modelity.exc

exception modelity.exc.ModelError(typ: type, errors: tuple[Error, ...])

Bases: ModelityError

Common base class for errors raised during either data parsing or model validation stages.

It can be used by library clients to catch both parsing and validation errors in one place, which can help avoid unexpected leaking of exceptions the user was not aware of.

Parameters:
  • typ – The type for which this error has happened.

  • errors – Tuple of errors to initialize exception with.

__init__(typ: type, errors: tuple[Error, ...])
errors: tuple[Error, ...]

Tuple with either parsing, or validation errors.

typ: type

The type for which this error has happened.

Changed in version 0.28.0: Moved from ParsingError class and now made available for all subclasses for ease of use.

property typ_name: str

Return the name of the type.

exception modelity.exc.ModelityError

Bases: Exception

Base class for every Modelity-specific exception.

__message_template__: str | None = None

Message template string.

Used as default error message when specified. Properties of the current exception object can be provided via self placeholder.

__str__() str

Return str(self).

exception modelity.exc.ParsingError(typ: type, errors: tuple[Error, ...])

Bases: ModelError

Exception raised at parsing stage when input data could not be parsed into model instance.

When this exception is raised, no model is created.

__str__() str

Return str(self).

exception modelity.exc.UnsupportedTypeError(typ: type)

Bases: ModelityError

Raised when model is declared with a field of a type that is not supported by the current version of Modelity library.

__init__(typ: type)
__message_template__: str | None = 'unsupported type used: {self.typ!r}'

Message template string.

Used as default error message when specified. Properties of the current exception object can be provided via self placeholder.

typ: type

The type that is not supported.

exception modelity.exc.UserError(msg: str, *, code: str = 'modelity.USER_ERROR', loc: Loc | None = None, value: Any = Unset, data: dict | None = None, skip: bool = False)

Bases: ModelityError

Exception raised by user-defined hooks to report a single error.

When raised inside a hook, this exception is intercepted by Modelity and converted into a modelity.error.Error instance during the parsing or validation stage (depending on the hook type; see modelity.hooks).

Using this exception is optional - hooks may also report errors using other supported mechanisms.

Added in version 0.30.0.

__init__(msg: str, *, code: str = 'modelity.USER_ERROR', loc: Loc | None = None, value: Any = Unset, data: dict | None = None, skip: bool = False)
__message_template__: str | None = '{self.msg} [code={self.code!r}, loc={self.loc!r}, value={self.value!r}, data={self.data!r}, skip={self.skip!r}]'

Message template string.

Used as default error message when specified. Properties of the current exception object can be provided via self placeholder.

code: str

Error code.

By default, modelity.error.ErrorCode.USER_ERROR is used.

data: dict | None

Additional error data.

Optional dictionary with extra context (e.g. {"min": 0, "max": 10}).

loc: Loc | None

Error location.

If not set, then the current location from the hook context is used.

msg: str

Error message.

skip: bool

Skipping flag.

Some validators (e.g. modelity.hooks.model_prevalidator()) can return boolean True to skip other validators for the current model instance. This flag allows to enable that feature.

value: Any

Invalid input value.

If not set, then the current value from the hook context is used.

exception modelity.exc.ValidationError(model: Any, errors: tuple[Error, ...])

Bases: ModelError

Exception raised at model validation stage when one or model specific constraint are broken.

This exception may only be raised for existing models.

Parameters:
  • model

    The model for which validation has failed.

    This will be the root model, i.e. the one for which modelity.model.Model.validate() method was called.

  • errors – Tuple containing all validation errors.

__init__(model: Any, errors: tuple[Error, ...])
__str__() str

Return str(self).

errors: tuple[Error, ...]

Tuple with either parsing, or validation errors.

model: Any

The model object for which validation has failed.

typ: type

The type for which this error has happened.

Changed in version 0.28.0: Moved from ParsingError class and now made available for all subclasses for ease of use.