def dispatch( self, req_json: Dict[str, Any] ) -> Tuple[Any, int, Union[Headers, Dict[str, str], Tuple[str], List[Tuple[str]]]]: if not self.validate(req_json): raise InvalidRequestError( data={'message': 'Invalid JSON: {0}'.format(req_json)}) params = req_json.get('params', {}) view_func = self.view_funcs.get(req_json['method']) if not view_func: raise MethodNotFoundError(data={ 'message': 'Method not found: {0}'.format(req_json['method']) }) try: if isinstance(params, (tuple, set, list)): resp_view = view_func(*params) elif isinstance(params, dict): resp_view = view_func(**params) else: raise InvalidParamsError( data={ 'message': 'Parameter structures are by-position ' '(tuple, set, list) or by-name (dict): {0}'.format( params) }) # TODO: Improve the checker to return type view_fun_annotations = get_type_hints(view_func) view_fun_return = view_fun_annotations.pop('return', None) if resp_view is not None and view_fun_return is None: raise TypeError( 'return type of {} must be a type; got {} instead'.format( qualified_name(resp_view), qualified_name(view_fun_return))) except TypeError as e: current_app.logger.error('invalid type checked for: %s', view_func.__name__) current_app.logger.exception(e) raise InvalidParamsError(data={'message': str(e)}) return self.make_response(req_json, resp_view)
def register_custom_type(self, cls: type, marshaller: Optional[Callable[ [Any], Any]] = default_marshaller, unmarshaller: Union[Callable[[Any, Any], None], Callable[[Any], Any], None] = default_unmarshaller, *, typename: str = None, wrap_state: bool = True) -> None: """ Register a marshaller and/or unmarshaller for the given class. The state object returned by the marshaller and passed to the unmarshaller can be any serializable type. Usually a dictionary mapping of attribute names to values is used. .. warning:: Registering marshallers/unmarshallers for any custom type will override any serializer specific encoding/decoding hooks (respectively) already in place! :param cls: the class to register :param marshaller: a callable that takes the object to be marshalled as the argument and returns a state object :param unmarshaller: a callable that either: * takes an uninitialized instance of ``cls`` and its state object as arguments and restores the state of the object * takes a state object and returns a new instance of ``cls`` :param typename: a unique identifier for the type (defaults to the ``module:varname`` reference to the class) :param wrap_state: ``True`` to wrap the marshalled state before serialization so that it can be recognized later for unmarshalling, ``False`` to serialize it as is """ assert check_argument_types() typename = typename or qualified_name(cls) if marshaller: self.marshallers[cls] = typename, marshaller, wrap_state self.custom_type_codec.register_object_encoder_hook(self) if unmarshaller and self.custom_type_codec is not None: if len(signature(unmarshaller).parameters) == 1: cls = None self.unmarshallers[typename] = cls, unmarshaller self.custom_type_codec.register_object_decoder_hook(self)
def register_custom_type( self, cls: type, marshaller: Optional[Callable[[Any], Any]] = default_marshaller, unmarshaller: Union[Callable[[Any, Any], None], Callable[[Any], Any], None] = default_unmarshaller, *, typename: str = None, wrap_state: bool = True) -> None: """ Register a marshaller and/or unmarshaller for the given class. The state object returned by the marshaller and passed to the unmarshaller can be any serializable type. Usually a dictionary mapping of attribute names to values is used. .. warning:: Registering marshallers/unmarshallers for any custom type will override any serializer specific encoding/decoding hooks (respectively) already in place! :param cls: the class to register :param marshaller: a callable that takes the object to be marshalled as the argument and returns a state object :param unmarshaller: a callable that either: * takes an uninitialized instance of ``cls`` and its state object as arguments and restores the state of the object * takes a state object and returns a new instance of ``cls`` :param typename: a unique identifier for the type (defaults to the ``module:varname`` reference to the class) :param wrap_state: ``True`` to wrap the marshalled state before serialization so that it can be recognized later for unmarshalling, ``False`` to serialize it as is """ assert check_argument_types() typename = typename or qualified_name(cls) if marshaller: self.marshallers[cls] = typename, marshaller, wrap_state self.custom_type_codec.register_object_encoder_hook(self) if unmarshaller and self.custom_type_codec is not None: target_cls = cls # type: Optional[type] if len(signature(unmarshaller).parameters) == 1: target_cls = None self.unmarshallers[typename] = target_cls, unmarshaller self.custom_type_codec.register_object_decoder_hook(self)
def get_qualified_name(self) -> str: return typeguard.qualified_name(self._spec_class)
def test_qualified_name(inputval, expected): assert qualified_name(inputval) == expected