예제 #1
0
def validate_method_arguments(func, args, kwargs):
    types_dict = func.__annotations__.copy()
    types_dict.pop('return', None)
    # Validate argument types
    for index, (attr, attr_type) in enumerate(types_dict.items()):
        if attr in kwargs:
            validate_type(attr_type.type, kwargs[attr], attr)
        elif len(args) - 1 >= index:
            validate_type(attr_type.type, args[index], attr)
        else:
            validate_type(attr_type.type, None, attr)
예제 #2
0
def validate_method_arguments(func, args, kwargs):
    # typing.get_type_hints return example from before:
    # {'amount': <class 'float'>, 'account_id': <class 'str'>,
    # 'reference': typing.Union[str, NoneType], 'return': <class 'NoneType'>}
    types_dict = typing.get_type_hints(func)
    types_dict.pop('return', None)
    # Validate argument types
    for index, (attr, attr_type) in enumerate(types_dict.items()):
        if attr in kwargs:
            validate_type(attr_type, kwargs[attr], attr)
        elif len(args) - 1 >= index:
            validate_type(attr_type, args[index], attr)
        else:
            validate_type(attr_type, None, attr)
예제 #3
0
 def set_attribute(self, property_value):
     validate_type(property_type.type, property_value, property_name)
     setattr(self.__class__, '_' + property_name, property_value)
예제 #4
0
def validate_return(func, return_value):
    types_dict = func.__annotations__
    return_type = types_dict['return']
    # Validate return type
    if return_value:
        validate_type(return_type.type, return_value, 'return_value')
예제 #5
0
def validate_return(func, return_value):
    types_dict = typing.get_type_hints(func)
    return_type = types_dict['return']
    # Validate return type
    if return_value:
        validate_type(return_type, return_value, 'return_value')