def test_reflect_simplest_function(): module_file = reflect_module_file(module_four_path) loaded_module = module_file.load() function = reflect_function(loaded_module.simplest_function) assert not function.has_return_annotation() assert function.invoke() is None
def test_bind_parameter_type_function(): arguments = [ "--var-keyword-one=really", "are", "--positional-or-keyword=dragons", "the", "--keyword-only=best", ] expected = { "positional_or_keyword": "dragons", "var_positional": ("are", "the"), "keyword_only": "best", "var_keyword": { "var_keyword_one": "really" }, } reflected_function = reflect_function(parameter_type_function) parsed_arguments = parse_arguments(arguments) print(parsed_arguments) bound_function = bind_arguments(reflected_function, parsed_arguments, RESOLVER) result = bound_function.invoke() assert expected == result
def test_reflect_string_function(): module_file = reflect_module_file(module_four_path) loaded_module = module_file.load() function = reflect_function(loaded_module.string_function) assert function.has_return_annotation() assert function.return_annotation is str assert function.invoke() == "string"
def test_bind_arguments_empty(): expected = None reflected_function = reflect_function(simple_function) bound_function = bind_arguments(reflected_function, [], RESOLVER) result = bound_function.invoke() assert expected == result
def test_reflect_identity_function(): module_file = reflect_module_file(module_four_path) loaded_module = module_file.load() function = reflect_function(loaded_module.identity_function) assert not function.has_return_annotation() assert function.invoke("value") == "value" for parameter in function.parameters: assert not parameter.has_default() assert not parameter.has_annotation() assert parameter.kind == ParameterKind.PositionalOrKeyword
def test_reflect_parameter_kind_function(): module_file = reflect_module_file(module_four_path) loaded_module = module_file.load() function = reflect_function(loaded_module.parameter_kind_function) assert not function.has_return_annotation() parameters = function.parameters if version_info.major >= 3 and version_info.minor >= 8: assert parameters[0].kind == ParameterKind.PositionalOnly assert parameters[1].kind == ParameterKind.PositionalOrKeyword assert parameters[2].kind == ParameterKind.VarPositional assert parameters[3].kind == ParameterKind.KeywordOnly assert parameters[4].kind == ParameterKind.VarKeyword else: assert parameters[0].kind == ParameterKind.PositionalOrKeyword assert parameters[1].kind == ParameterKind.VarPositional assert parameters[2].kind == ParameterKind.KeywordOnly assert parameters[3].kind == ParameterKind.VarKeyword # In current versions of python you can only specify positional_or_keyword # parameters using their positional variant in this complex scenario. assert function.invoke( "value1", "value2", "value3", "value4", keyword_only="value5", var_keyword1="value6", var_keyword2="value7", ) == ( "value1", "value2", "value3", "value4", "value5", {"var_keyword1": "value6", "var_keyword2": "value7"}, ) for parameter in parameters: assert not parameter.has_default() assert not parameter.has_annotation()