Test a function that uses *args and **kwargs.
()
| 97 | |
| 98 | |
| 99 | def test_varargs_function(): |
| 100 | """Test a function that uses *args and **kwargs.""" |
| 101 | |
| 102 | func_schema = function_schema(varargs_function, strict_json_schema=False) |
| 103 | # Check JSON schema structure |
| 104 | assert isinstance(func_schema.params_json_schema, dict) |
| 105 | assert func_schema.params_json_schema.get("title") == "varargs_function_args" |
| 106 | |
| 107 | # Valid input including *args in 'numbers' and **kwargs in 'kwargs' |
| 108 | valid_input = { |
| 109 | "x": 10, |
| 110 | "numbers": [1.1, 2.2, 3.3], |
| 111 | "flag": True, |
| 112 | "kwargs": {"extra1": "hello", "extra2": 42}, |
| 113 | } |
| 114 | parsed = func_schema.params_pydantic_model(**valid_input) |
| 115 | args, kwargs_dict = func_schema.to_call_args(parsed) |
| 116 | |
| 117 | result = varargs_function(*args, **kwargs_dict) |
| 118 | # result should be (10, (1.1, 2.2, 3.3), True, {"extra1": "hello", "extra2": 42}) |
| 119 | assert result[0] == 10 |
| 120 | assert result[1] == (1.1, 2.2, 3.3) |
| 121 | assert result[2] is True |
| 122 | assert result[3] == {"extra1": "hello", "extra2": 42} |
| 123 | |
| 124 | # Missing 'x' should raise error |
| 125 | with pytest.raises(ValidationError): |
| 126 | func_schema.params_pydantic_model(**{"numbers": [1.1, 2.2]}) |
| 127 | |
| 128 | # 'flag' can be omitted because it has a default |
| 129 | valid_input_no_flag = {"x": 7, "numbers": [9.9], "kwargs": {"some_key": "some_value"}} |
| 130 | parsed2 = func_schema.params_pydantic_model(**valid_input_no_flag) |
| 131 | args2, kwargs_dict2 = func_schema.to_call_args(parsed2) |
| 132 | result2 = varargs_function(*args2, **kwargs_dict2) |
| 133 | # result2 should be (7, (9.9,), False, {'some_key': 'some_value'}) |
| 134 | assert result2 == (7, (9.9,), False, {"some_key": "some_value"}) |
| 135 | |
| 136 | |
| 137 | class Foo(TypedDict): |
nothing calls this directly
no test coverage detected