Пример #1
0
    def register_unstructure_hook(self, cls: Any, func: Callable[[T],
                                                                 Any]) -> None:
        """Register a class-to-primitive converter function for a class.

        The converter function should take an instance of the class and return
        its Python equivalent.
        """
        if attrs_has(cls):
            resolve_types(cls)
        if is_union_type(cls):
            self._unstructure_func.register_func_list([(lambda t: t == cls,
                                                        func)])
        else:
            self._unstructure_func.register_cls_list([(cls, func)])
Пример #2
0
    def register_structure_hook(self, cl: Any, func: Callable[[Any, Type[T]],
                                                              T]):
        """Register a primitive-to-class converter function for a type.

        The converter function should take two arguments:
          * a Python object to be converted,
          * the type to convert to

        and return the instance of the class. The type may seem redundant, but
        is sometimes needed (for example, when dealing with generic classes).
        """
        if attrs_has(cl):
            resolve_types(cl)
        if is_union_type(cl):
            self._union_struct_registry[cl] = func
        else:
            self._structure_func.register_cls_list([(cl, func)])