コード例 #1
0
ファイル: __returns.py プロジェクト: kamaal44/pyvalid
 def decorator_wrapper(*func_args, **func_kwargs):
     from pyvalid.validators import Validator
     returns_val = func(*func_args, **func_kwargs)
     if is_enabled() and self.allowed_return_values:
         is_valid = False
         for allowed_val in self.allowed_return_values:
             if isinstance(allowed_val, (Validator, MethodType)):
                 if isinstance(allowed_val, Validator):
                     is_valid = allowed_val(returns_val)
                 elif (isinstance(allowed_val, MethodType) and
                         hasattr(allowed_val, '__func__') and
                         isinstance(allowed_val.__func__, Validator)):
                     is_valid = allowed_val(returns_val)
             elif isinstance(allowed_val, type):
                 is_valid = isinstance(
                     returns_val, allowed_val
                 )
             else:
                 is_valid = returns_val == allowed_val
             if is_valid:
                 break
         if not is_valid:
             raise InvalidReturnTypeError(
                 func, returns_val, self.allowed_return_values
             )
     return returns_val
コード例 #2
0
ファイル: __returns.py プロジェクト: uzumaxy/pyvalid
        def decorator_wrapper(*func_args, **func_kwargs):
            from pyvalid.validators import Validator

            returns_val = func(*func_args, **func_kwargs)
            if is_enabled() and self.accepted_returns_values:
                is_valid = False
                for accepted_val in self.accepted_returns_values:
                    if isinstance(accepted_val, (Validator, MethodType)):
                        if isinstance(accepted_val, Validator):
                            is_valid = accepted_val(returns_val)
                        elif (
                            isinstance(accepted_val, MethodType)
                            and hasattr(accepted_val, "__func__")
                            and isinstance(accepted_val.__func__, Validator)
                        ):
                            is_valid = accepted_val(returns_val)
                    elif isinstance(accepted_val, type):
                        is_valid = isinstance(returns_val, accepted_val)
                    else:
                        is_valid = returns_val == accepted_val
                    if is_valid:
                        break
                if not is_valid:
                    raise InvalidReturnType(type(returns_val), func.__name__)
            return returns_val
コード例 #3
0
 def decorator_wrapper(*func_args, **func_kwargs):
     perform_validation = all((is_enabled(), self.accepted_arg_values
                               or self.accepted_kwargs_values))
     if perform_validation:
         # Forget all information about function arguments.
         self.accepted_args[:] = list()
         self.optional_args[:] = list()
         # Collect information about fresh arguments.
         args_info = getargspec(func)
         self.__scan_func(args_info)
         # Validate function arguments.
         self.__validate_args(func.__name__, func_args, func_kwargs)
     # Call function.
     return func(*func_args, **func_kwargs)
コード例 #4
0
ファイル: __accepts.py プロジェクト: uzumaxy/pyvalid
 def decorator_wrapper(*func_args, **func_kwargs):
     perform_validation = all((
         is_enabled(),
         self.accepted_arg_values or self.accepted_kwargs_values
     ))
     if perform_validation:
         # Forget all information about function arguments.
         self.accepted_args[:] = list()
         self.optional_args[:] = list()
         # Collect information about fresh arguments.
         args_info = getargspec(func)
         self.__scan_func(args_info)
         # Validate function arguments.
         self.__validate_args(func.__name__, func_args, func_kwargs)
     # Call function.
     return func(*func_args, **func_kwargs)