def _decorator_to_arg(node, resources): arg_name = def_value = by_value = None if not len(node.args) == 3: raise errors.TranslateError(_errormessages.invalid_param_decorator) # this is a decorator param definition. First parameter is the string for the name. if utils.is_node_ast_str(node.args[0]): arg_name = utils.get_value_from_str_node(node.args[0]) # second is the default value try: def_value = utils.get_value_from_node(node.args[1], resources) except errors.TranslateError: # def_value won't get assigned anything if an error occurs, which will trigger the exception later. pass # third is whether to pass by ref or by value valid_types = [ast.Name, ast.Attribute] if isinstance(node.args[2], tuple(valid_types)): by_value_str = utils.get_variable_name_from_node(node.args[2]) by_value_str = getattr(_decorators.NivsParam, by_value_str.split('.')[-1], by_value_str) by_value = BooleanValue(by_value_str).value elif 'NameConstant' in dir(ast) and utils.is_node_ast_nameconstant( node.args[2]): by_value = utils.get_value_from_nameconstant_node(node.args[2]) if arg_name is None or def_value is None or by_value is None: raise errors.TranslateError(_errormessages.invalid_param_decorator) return _param(arg_name, def_value, by_value)
def test_false_check_returns_true_bool(): if sys.version_info >= (3, 8): node = ast.Constant(value=False) res = utils.is_node_ast_nameconstant(node) assert res is True
def test_complex_2_3_check_returns_false_bool(): if sys.version_info >= (3, 8): node = ast.Constant(value=complex(2, 3)) res = utils.is_node_ast_nameconstant(node) assert res is False
def test_str_empty_check_returns_false_bool(): if sys.version_info >= (3, 8): node = ast.Constant(value="") res = utils.is_node_ast_nameconstant(node) assert res is False
def _validate_restrictions(node): if not isinstance(node.value, ast.Attribute) and \ not utils.is_node_ast_nameconstant(node.value) and \ not utils.is_node_ast_num(node.value): raise TranslateError(_errormessages.invalid_return_type)