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)
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)
def set_attribute(self, property_value): validate_type(property_type.type, property_value, property_name) setattr(self.__class__, '_' + property_name, property_value)
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')
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')