Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
exceptions.py72 linesDownload Raw Back to marshmallow
1"""Exception classes for marshmallow-related errors."""2 3from __future__ import annotations4 5import typing6 7# Key used for schema-level validation errors8SCHEMA = "_schema"9 10 11class MarshmallowError(Exception):12    """Base class for all marshmallow-related errors."""13 14 15class ValidationError(MarshmallowError):16    """Raised when validation fails on a field or schema.17 18    Validators and custom fields should raise this exception.19 20    :param message: An error message, list of error messages, or dict of21        error messages. If a dict, the keys are subitems and the values are error messages.22    :param field_name: Field name to store the error on.23        If `None`, the error is stored as schema-level error.24    :param data: Raw input data.25    :param valid_data: Valid (de)serialized data.26    """27 28    def __init__(29        self,30        message: str | list | dict,31        field_name: str = SCHEMA,32        data: typing.Mapping[str, typing.Any]33        | typing.Iterable[typing.Mapping[str, typing.Any]]34        | None = None,35        valid_data: list[dict[str, typing.Any]] | dict[str, typing.Any] | None = None,36        **kwargs,37    ):38        self.messages = [message] if isinstance(message, (str, bytes)) else message39        self.field_name = field_name40        self.data = data41        self.valid_data = valid_data42        self.kwargs = kwargs43        super().__init__(message)44 45    def normalized_messages(self):46        if self.field_name == SCHEMA and isinstance(self.messages, dict):47            return self.messages48        return {self.field_name: self.messages}49 50    @property51    def messages_dict(self) -> dict[str, typing.Any]:52        if not isinstance(self.messages, dict):53            raise TypeError(54                "cannot access 'messages_dict' when 'messages' is of type "55                + type(self.messages).__name__56            )57        return self.messages58 59 60class RegistryError(NameError):61    """Raised when an invalid operation is performed on the serializer62    class registry.63    """64 65 66class StringNotCollectionError(MarshmallowError, TypeError):67    """Raised when a string is passed when a list of strings is expected."""68 69 70class FieldInstanceResolutionError(MarshmallowError, TypeError):71    """Raised when schema to instantiate is neither a Schema class nor an instance."""72 
codekingpro/portable-devtools · Team Ai