[docs]classStructureHandlerNotFoundError(Exception):""" Error raised when structuring cannot find a handler for converting inputs into :attr:`type_`. """def__init__(self,message:str,type_:type)->None:super().__init__(message,type_)self.message=messageself.type_=type_def__str__(self)->str:returnself.message
[docs]classIterableValidationNote(str):"""Attached as a note to an exception when an iterable element fails structuring."""index:Union[int,str]# Ints for list indices, strs for dict keystype:Anydef__new__(cls,string:str,index:Union[int,str],type:Any)->Self:instance=str.__new__(cls,string)instance.index=indexinstance.type=typereturninstancedef__getnewargs__(self)->tuple[str,Union[int,str],Any]:return(str(self),self.index,self.type)
[docs]classIterableValidationError(BaseValidationError):"""Raised when structuring an iterable."""
[docs]defgroup_exceptions(self,)->tuple[list[tuple[Exception,IterableValidationNote]],list[Exception]]:"""Split the exceptions into two groups: with and without validation notes."""excs_with_notes=[]other_excs=[]forsubexcinself.exceptions:ifhasattr(subexc,"__notes__"):fornoteinsubexc.__notes__:ifnote.__class__isIterableValidationNote:excs_with_notes.append((subexc,note))breakelse:other_excs.append(subexc)else:other_excs.append(subexc)returnexcs_with_notes,other_excs
[docs]classAttributeValidationNote(str):"""Attached as a note to an exception when an attribute fails structuring."""name:strtype:Anydef__new__(cls,string:str,name:str,type:Any)->Self:instance=str.__new__(cls,string)instance.name=nameinstance.type=typereturninstancedef__getnewargs__(self)->tuple[str,str,Any]:return(str(self),self.name,self.type)
[docs]classClassValidationError(BaseValidationError):"""Raised when validating a class if any attributes are invalid."""
[docs]defgroup_exceptions(self,)->tuple[list[tuple[Exception,AttributeValidationNote]],list[Exception]]:"""Split the exceptions into two groups: with and without validation notes."""excs_with_notes=[]other_excs=[]forsubexcinself.exceptions:ifhasattr(subexc,"__notes__"):fornoteinsubexc.__notes__:ifnote.__class__isAttributeValidationNote:excs_with_notes.append((subexc,note))breakelse:other_excs.append(subexc)else:other_excs.append(subexc)returnexcs_with_notes,other_excs
[docs]classForbiddenExtraKeysError(Exception):""" Raised when `forbid_extra_keys` is activated and such extra keys are detected during structuring. The attribute `extra_fields` is a sequence of those extra keys, which were the cause of this error, and `cl` is the class which was structured with those extra keys. """def__init__(self,message:Optional[str],cl:type,extra_fields:set[str])->None:self.message=messageself.cl=clself.extra_fields=extra_fieldssuper().__init__(message,cl,extra_fields)def__str__(self)->str:return(self.messageorf"Extra fields in constructor for {self.cl.__name__}: "f"{', '.join(sorted(self.extra_fields))}")