"""Exception classes for marshmallow-related errors."""from__future__importannotationsimporttyping# Key used for schema-level validation errorsSCHEMA="_schema"classMarshmallowError(Exception):"""Base class for all marshmallow-related errors."""
[docs]classValidationError(MarshmallowError):"""Raised when validation fails on a field or schema. Validators and custom fields should raise this exception. :param message: An error message, list of error messages, or dict of error messages. If a dict, the keys are subitems and the values are error messages. :param field_name: Field name to store the error on. If `None`, the error is stored as schema-level error. :param data: Raw input data. :param valid_data: Valid (de)serialized data. """def__init__(self,message:str|list|dict,field_name:str=SCHEMA,data:typing.Mapping[str,typing.Any]|typing.Iterable[typing.Mapping[str,typing.Any]]|None=None,valid_data:list[dict[str,typing.Any]]|dict[str,typing.Any]|None=None,**kwargs,):self.messages=[message]ifisinstance(message,(str,bytes))elsemessageself.field_name=field_nameself.data=dataself.valid_data=valid_dataself.kwargs=kwargssuper().__init__(message)defnormalized_messages(self):ifself.field_name==SCHEMAandisinstance(self.messages,dict):returnself.messagesreturn{self.field_name:self.messages}@propertydefmessages_dict(self)->dict[str,typing.Any]:ifnotisinstance(self.messages,dict):raiseTypeError("cannot access 'messages_dict' when 'messages' is of type "+type(self.messages).__name__)returnself.messages
classRegistryError(NameError):"""Raised when an invalid operation is performed on the serializer class registry. """classStringNotCollectionError(MarshmallowError,TypeError):"""Raised when a string is passed when a list of strings is expected."""classFieldInstanceResolutionError(MarshmallowError,TypeError):"""Raised when schema to instantiate is neither a Schema class nor an instance."""