"""Strategy for using class-specific (un)structuring methods."""frominspectimportsignaturefromtypingimportAny,Callable,Optional,TypeVarfrom..importBaseConverterT=TypeVar("T")
[docs]defuse_class_methods(converter:BaseConverter,structure_method_name:Optional[str]=None,unstructure_method_name:Optional[str]=None,)->None:""" Configure the converter such that dedicated methods are used for (un)structuring the instance of a class if such methods are available. The default (un)structuring will be applied if such an (un)structuring methods cannot be found. :param converter: The `Converter` on which this strategy is applied. You can use :class:`cattrs.BaseConverter` or any other derived class. :param structure_method_name: Optional string with the name of the class method which should be used for structuring. If not provided, no class method will be used for structuring. :param unstructure_method_name: Optional string with the name of the class method which should be used for unstructuring. If not provided, no class method will be used for unstructuring. If you want to (un)structured nested objects, just append a converter parameter to your (un)structuring methods and you will receive the converter there. .. versionadded:: 23.2.0 """ifstructure_method_name:defmake_class_method_structure(cl:type[T])->Callable[[Any,type[T]],T]:fn=getattr(cl,structure_method_name)n_parameters=len(signature(fn).parameters)ifn_parameters==1:returnlambdav,_:fn(v)ifn_parameters==2:returnlambdav,_:fn(v,converter)raiseTypeError("Provide a class method with one or two arguments.")converter.register_structure_hook_factory(lambdat:hasattr(t,structure_method_name),make_class_method_structure)ifunstructure_method_name:defmake_class_method_unstructure(cl:type[T])->Callable[[T],T]:fn=getattr(cl,unstructure_method_name)n_parameters=len(signature(fn).parameters)ifn_parameters==1:returnfnifn_parameters==2:returnlambdaself_:fn(self_,converter)raiseTypeError("Provide a method with no or one argument.")converter.register_unstructure_hook_factory(lambdat:hasattr(t,unstructure_method_name),make_class_method_unstructure)