test_kwargs_and_defaults.py revision 11986
111986Sandreas.sandberg@arm.comimport pytest 211986Sandreas.sandberg@arm.comfrom pybind11_tests import (kw_func0, kw_func1, kw_func2, kw_func3, kw_func4, args_function, 311986Sandreas.sandberg@arm.com args_kwargs_function, kw_func_udl, kw_func_udl_z, KWClass) 411986Sandreas.sandberg@arm.com 511986Sandreas.sandberg@arm.com 611986Sandreas.sandberg@arm.comdef test_function_signatures(doc): 711986Sandreas.sandberg@arm.com assert doc(kw_func0) == "kw_func0(arg0: int, arg1: int) -> str" 811986Sandreas.sandberg@arm.com assert doc(kw_func1) == "kw_func1(x: int, y: int) -> str" 911986Sandreas.sandberg@arm.com assert doc(kw_func2) == "kw_func2(x: int=100, y: int=200) -> str" 1011986Sandreas.sandberg@arm.com assert doc(kw_func3) == "kw_func3(data: str='Hello world!') -> None" 1111986Sandreas.sandberg@arm.com assert doc(kw_func4) == "kw_func4(myList: List[int]=[13, 17]) -> str" 1211986Sandreas.sandberg@arm.com assert doc(kw_func_udl) == "kw_func_udl(x: int, y: int=300) -> str" 1311986Sandreas.sandberg@arm.com assert doc(kw_func_udl_z) == "kw_func_udl_z(x: int, y: int=0) -> str" 1411986Sandreas.sandberg@arm.com assert doc(args_function) == "args_function(*args) -> tuple" 1511986Sandreas.sandberg@arm.com assert doc(args_kwargs_function) == "args_kwargs_function(*args, **kwargs) -> tuple" 1611986Sandreas.sandberg@arm.com assert doc(KWClass.foo0) == "foo0(self: m.KWClass, arg0: int, arg1: float) -> None" 1711986Sandreas.sandberg@arm.com assert doc(KWClass.foo1) == "foo1(self: m.KWClass, x: int, y: float) -> None" 1811986Sandreas.sandberg@arm.com 1911986Sandreas.sandberg@arm.com 2011986Sandreas.sandberg@arm.comdef test_named_arguments(msg): 2111986Sandreas.sandberg@arm.com assert kw_func0(5, 10) == "x=5, y=10" 2211986Sandreas.sandberg@arm.com 2311986Sandreas.sandberg@arm.com assert kw_func1(5, 10) == "x=5, y=10" 2411986Sandreas.sandberg@arm.com assert kw_func1(5, y=10) == "x=5, y=10" 2511986Sandreas.sandberg@arm.com assert kw_func1(y=10, x=5) == "x=5, y=10" 2611986Sandreas.sandberg@arm.com 2711986Sandreas.sandberg@arm.com assert kw_func2() == "x=100, y=200" 2811986Sandreas.sandberg@arm.com assert kw_func2(5) == "x=5, y=200" 2911986Sandreas.sandberg@arm.com assert kw_func2(x=5) == "x=5, y=200" 3011986Sandreas.sandberg@arm.com assert kw_func2(y=10) == "x=100, y=10" 3111986Sandreas.sandberg@arm.com assert kw_func2(5, 10) == "x=5, y=10" 3211986Sandreas.sandberg@arm.com assert kw_func2(x=5, y=10) == "x=5, y=10" 3311986Sandreas.sandberg@arm.com 3411986Sandreas.sandberg@arm.com with pytest.raises(TypeError) as excinfo: 3511986Sandreas.sandberg@arm.com # noinspection PyArgumentList 3611986Sandreas.sandberg@arm.com kw_func2(x=5, y=10, z=12) 3711986Sandreas.sandberg@arm.com assert msg(excinfo.value) == """ 3811986Sandreas.sandberg@arm.com kw_func2(): incompatible function arguments. The following argument types are supported: 3911986Sandreas.sandberg@arm.com 1. (x: int=100, y: int=200) -> str 4011986Sandreas.sandberg@arm.com 4111986Sandreas.sandberg@arm.com Invoked with: 4211986Sandreas.sandberg@arm.com """ 4311986Sandreas.sandberg@arm.com 4411986Sandreas.sandberg@arm.com assert kw_func4() == "{13 17}" 4511986Sandreas.sandberg@arm.com assert kw_func4(myList=[1, 2, 3]) == "{1 2 3}" 4611986Sandreas.sandberg@arm.com 4711986Sandreas.sandberg@arm.com assert kw_func_udl(x=5, y=10) == "x=5, y=10" 4811986Sandreas.sandberg@arm.com assert kw_func_udl_z(x=5) == "x=5, y=0" 4911986Sandreas.sandberg@arm.com 5011986Sandreas.sandberg@arm.com 5111986Sandreas.sandberg@arm.comdef test_arg_and_kwargs(): 5211986Sandreas.sandberg@arm.com args = 'arg1_value', 'arg2_value', 3 5311986Sandreas.sandberg@arm.com assert args_function(*args) == args 5411986Sandreas.sandberg@arm.com 5511986Sandreas.sandberg@arm.com args = 'a1', 'a2' 5611986Sandreas.sandberg@arm.com kwargs = dict(arg3='a3', arg4=4) 5711986Sandreas.sandberg@arm.com assert args_kwargs_function(*args, **kwargs) == (args, kwargs) 58