modelity.exc
- exception modelity.exc.ModelError(typ: type, errors: tuple[Error, ...])
Bases:
ModelityErrorCommon 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.
- typ: type
The type for which this error has happened.
Changed in version 0.28.0: Moved from
ParsingErrorclass and now made available for all subclasses for ease of use.
- exception modelity.exc.ModelityError
Bases:
ExceptionBase class for every Modelity-specific exception.
- exception modelity.exc.ParsingError(typ: type, errors: tuple[Error, ...])
Bases:
ModelErrorException raised at parsing stage when input data could not be parsed into model instance.
When this exception is raised, no model is created.
- exception modelity.exc.UnsupportedTypeError(typ: type)
Bases:
ModelityErrorRaised when model is declared with a field of a type that is not supported by the current version of Modelity library.
- 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:
ModelityErrorException 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.Errorinstance during the parsing or validation stage (depending on the hook type; seemodelity.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
selfplaceholder.
- code: str
Error code.
By default,
modelity.error.ErrorCode.USER_ERRORis 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.
- skip: bool
Skipping flag.
Some validators (e.g.
modelity.hooks.model_prevalidator()) can return booleanTrueto skip other validators for the current model instance. This flag allows to enable that feature.
- exception modelity.exc.ValidationError(model: Any, errors: tuple[Error, ...])
Bases:
ModelErrorException 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.
- typ: type
The type for which this error has happened.
Changed in version 0.28.0: Moved from
ParsingErrorclass and now made available for all subclasses for ease of use.